Skip to content

Commit

Permalink
Modernise codebase (#8)
Browse files Browse the repository at this point in the history
* Replace hard-coded Guzzle  with PSR implementation
*  Strict types
* Replace GitHub action workflow
*  Add PHPStan baseline
  • Loading branch information
kimpepper authored Jan 12, 2024
1 parent 0f890ef commit f54c3fa
Show file tree
Hide file tree
Showing 33 changed files with 483 additions and 872 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: 🏗 Build

on:
pull_request:
types: [ synchronize, opened, reopened, ready_for_review ]
push:
branches: [ main ]

permissions:
checks: write
pull-requests: write

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
prefer_lowest: ["", "--prefer-lowest"]
php: ["8.1", "8.2", "8.3"]
container:
image: skpr/php-cli:${{ matrix.php }}-dev-v2-edge
options:
--pull always
--user 1001:1001
steps:
- name: Checkout
uses: actions/checkout@v4
with:
show-progress: false
- name: 📦 Composer Update
run: composer update --with-all-dependencies --prefer-dist --no-progress --no-interaction ${{ matrix.prefer_lowest }}
- name: 🧹 PHPCS
run: ./bin/phpcs --report=checkstyle -q | ./bin/cs2pr
- name: 🧹 PHPStan
run: ./bin/phpstan --error-format=github analyse
- name: ⚡ Run Tests
run: ./bin/phpunit --log-junit phpunit-results.xml
- name: 📝 Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: phpunit-results.xml
check_name: Test Results ${{ matrix.php }} ${{ matrix.prefer_lowest }}
34 changes: 0 additions & 34 deletions .github/workflows/continuous-integration.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/composer.lock
/.phpunit.cache
/bin
/vendor
/composer.lock
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ composer require previousnext/bom-weather
### Forecasts

```php
$logger = new NullLogger();
$client = new BomClient($logger);
$client = new BomClient(new Client(), new RequestFactory(), new NullLogger());
$forecast = $client->getForecast('IDN10031');

$issueTime = $forecast->getIssueTime();
Expand Down Expand Up @@ -46,8 +45,7 @@ foreach ($locations as $location) {
### Observations

```php
$logger = new NullLogger();
$client = new BomClient($logger);
$client = new BomClient(new Client(), new RequestFactory(), new NullLogger());
$observationList = $client->getObservationList('IDN60901', '95757');

$refreshMessage = $observationList->getRefreshMessage();
Expand Down
30 changes: 20 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,39 @@
}
],
"require": {
"php": "^7.1",
"ext-curl": "*",
"php": "^8.1",
"ext-xml": "*",
"guzzlehttp/guzzle": "~6.3",
"psr/log": "~1.0",
"symfony/serializer": "~3.4"
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0",
"psr/http-message-implementation": "^1.0",
"psr/log": "^3.0",
"symfony/serializer": "^6.4"
},
"require-dev": {
"drupal/coder": ">=8.2.12",
"phpunit/phpunit": "~6.5",
"symfony/phpunit-bridge": "~3.4"
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"guzzlehttp/psr7": "^2.6",
"http-interop/http-factory-guzzle": "^1.2",
"php-http/mock-client": "^1.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6",
"previousnext/coding-standard": "^0.1.3",
"staabm/annotate-pull-request-from-checkstyle": "^1.8"
},
"autoload": {
"psr-4": {"BomWeather\\": "src/"}
},
"autoload-dev": {
"psr-4": {"BomWeather\\Tests\\": "tests/"}
"psr-4": {"BomWeather\\Tests\\": "tests/src"}
},
"config": {
"bin-dir": "bin/",
"preferred-install": {
"*": "dist"
},
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"php-http/discovery": true
}
}
}
26 changes: 4 additions & 22 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="bom-weather">
<description>PHP CodeSniffer configuration for BOM Weather</description>
<!-- folders to scan -->
<ruleset name="PNX Standard">
<file>./src</file>
<file>./tests</file>
<!-- extensions to scan -->
<arg name="extensions" value="php"/>
<!-- additional arguments -->
<file>./tests/src</file>
<rule ref="PreviousNextDrupal" />
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions" />
<arg name="report" value="full"/>
<!-- inherit from coder -->
<rule ref="./vendor/drupal/coder/coder_sniffer/Drupal"/>
<!-- Additional detailed sniff configuration -->
<!-- You can determine sniff names by running with -s flag -->
<!-- Example 1 - ignore Drupal.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
<rule ref="Drupal.NamingConventions.ValidFunctionName.ScopeNotCamelCaps">
<severity>0</severity>
</rule>
// End example 1 -->
<!-- Example 2 - ignore rule for specific files
<rule ref="Drupal.NamingConventions.ValidVariableName.LowerCamelName">
<exclude-pattern>OfferNormalizerTrait.php</exclude-pattern>
<exclude-pattern>TimeOfUseValidationTrait.php</exclude-pattern>
</rule>
// End example 2 -->
</ruleset>
31 changes: 31 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
parameters:
ignoreErrors:
-
message: "#^Method BomWeather\\\\Util\\\\BaseNormalizer\\:\\:normalize\\(\\) return type with generic class ArrayObject does not specify its types\\: TKey, TValue$#"
count: 1
path: src/Util/BaseNormalizer.php

-
message: "#^Call to method getEndTime\\(\\) on an unknown class BomWeather\\\\Forecast\\\\RegionForecastPeriod\\.$#"
count: 1
path: tests/src/Unit/Forecast/Serializer/ForecastSerializerTest.php

-
message: "#^Call to method getForecast\\(\\) on an unknown class BomWeather\\\\Forecast\\\\RegionForecastPeriod\\.$#"
count: 1
path: tests/src/Unit/Forecast/Serializer/ForecastSerializerTest.php

-
message: "#^Call to method getStartTime\\(\\) on an unknown class BomWeather\\\\Forecast\\\\RegionForecastPeriod\\.$#"
count: 1
path: tests/src/Unit/Forecast/Serializer/ForecastSerializerTest.php

-
message: "#^Parameter \\#1 \\$periods of method BomWeather\\\\Tests\\\\Unit\\\\Forecast\\\\Serializer\\\\ForecastSerializerTest\\:\\:assertRegionPeriods\\(\\) expects array\\<BomWeather\\\\Forecast\\\\RegionForecastPeriod\\>, array\\<BomWeather\\\\Forecast\\\\ForecastPeriod\\> given\\.$#"
count: 1
path: tests/src/Unit/Forecast/Serializer/ForecastSerializerTest.php

-
message: "#^Parameter \\$periods of method BomWeather\\\\Tests\\\\Unit\\\\Forecast\\\\Serializer\\\\ForecastSerializerTest\\:\\:assertRegionPeriods\\(\\) has invalid type BomWeather\\\\Forecast\\\\RegionForecastPeriod\\.$#"
count: 1
path: tests/src/Unit/Forecast/Serializer/ForecastSerializerTest.php
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- phpstan-baseline.neon

parameters:
level: 6
paths:
- src
- tests/src
checkMissingIterableValueType: false
ignoreErrors:
- "#^Call to an undefined method Symfony\\\\Component\\\\Serializer\\\\SerializerInterface\\:\\:denormalize\\(\\)\\.$#"
64 changes: 24 additions & 40 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- TODO set checkForUnintentionallyCoveredCode="true" once https://www.drupal.org/node/2626832 is resolved. -->
<!-- PHPUnit expects functional tests to be run with either a privileged user
or your current system user. See core/tests/README.md and
https://www.drupal.org/node/2116263 for details.
-->
<phpunit bootstrap="tests/bootstrap.php" colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
checkForUnintentionallyCoveredCode="false">
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
<const name="BOOTSTRAP_IS_PHPUNIT" value="true"/>
</php>
<testsuites>
<testsuite name="unit">
<directory>./tests/Unit</directory>
</testsuite>
<testsuite name="functional">
<directory>./tests/Functional</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
</listener>
</listeners>
<!-- Filter for coverage reports. -->
<filter>
<whitelist>
<directory>./src</directory>
<!-- By definition test classes have no tests. -->
<exclude>
<directory suffix="Test.php">./</directory>
<directory suffix="TestBase.php">./</directory>
</exclude>
</whitelist>
</filter>
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests/src</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
Loading

0 comments on commit f54c3fa

Please sign in to comment.