From 6c731892f24b62d6cfe65dd213fd57b62de1ecfd Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 2 Oct 2024 14:11:13 +0300 Subject: [PATCH] Bump PHP version to `^8.1` and refactor code (#130) --- .github/workflows/bc.yml | 2 +- .github/workflows/build.yml | 2 +- .../workflows/composer-require-checker.yml | 2 +- .github/workflows/mutation.yml | 2 +- .github/workflows/static.yml | 8 ------ .gitignore | 8 +++--- CHANGELOG.md | 1 + README.md | 2 +- composer.json | 6 ++--- phpunit.xml.dist | 26 ++++++++++--------- psalm80.xml | 19 -------------- rector.php | 4 ++- src/Csrf.php | 7 +++-- src/Exception/InvalidLinkTagException.php | 8 +++--- src/Exception/InvalidMetaTagException.php | 8 +++--- src/InjectionContainer/InjectionContainer.php | 2 +- src/LayoutSpecificInjections.php | 4 +-- tests/Support/FakeCsrfToken.php | 2 +- .../Support/RootNamespace/Fake8Controller.php | 7 +++++ .../Support/RootNamespace/FakeController.php | 7 +++++ tests/ViewRendererTest.php | 25 ++++++++++-------- 21 files changed, 76 insertions(+), 76 deletions(-) delete mode 100644 psalm80.xml create mode 100644 tests/Support/RootNamespace/Fake8Controller.php create mode 100644 tests/Support/RootNamespace/FakeController.php diff --git a/.github/workflows/bc.yml b/.github/workflows/bc.yml index f8cc564..85232cc 100644 --- a/.github/workflows/bc.yml +++ b/.github/workflows/bc.yml @@ -30,4 +30,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.0'] + ['8.3'] diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e96052e..05e7b40 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -31,4 +31,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['8.0', '8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3'] diff --git a/.github/workflows/composer-require-checker.yml b/.github/workflows/composer-require-checker.yml index 5473ec9..a857bce 100644 --- a/.github/workflows/composer-require-checker.yml +++ b/.github/workflows/composer-require-checker.yml @@ -31,4 +31,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.0', '8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3'] diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index 8150499..a6873e2 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -27,6 +27,6 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.2'] + ['8.3'] secrets: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index d2a03af..873eeb5 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -31,11 +31,3 @@ jobs: ['ubuntu-latest'] php: >- ['8.1', '8.2', '8.3'] - psalm80: - uses: yiisoft/actions/.github/workflows/psalm.yml@master - with: - psalm-config: psalm80.xml - os: >- - ['ubuntu-latest'] - php: >- - ['8.0'] diff --git a/.gitignore b/.gitignore index 56da114..e882061 100644 --- a/.gitignore +++ b/.gitignore @@ -23,9 +23,7 @@ composer.phar # Mac DS_Store Files .DS_Store -# phpunit itself is not needed -phpunit.phar -# local phpunit config +# PhpUnit +/phpunit.phar /phpunit.xml -# phpunit cache -.phpunit.result.cache +/.phpunit.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index cb57bc1..5e8adc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 7.1.1 under development - Enh #129: Add support for `yiisoft/view` version `^11` (@vjik) +- Enh #130: Bump PHP version to `^8.1` and refactor code (@vjik) ## 7.1.0 July 01, 2024 diff --git a/README.md b/README.md index 33cdb5d..95a6717 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ WEB-specific functionality and compatibility with [PSR-7](https://www.php-fig.or ## Requirements -- PHP 8.0 or higher. +- PHP 8.1 or higher. ## Installation diff --git a/composer.json b/composer.json index e310816..826d37a 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "psr/container": "^1.0|^2.0", "yiisoft/aliases": "^2.0|^3.0", "yiisoft/csrf": "^1.2|^2.0", @@ -42,7 +42,7 @@ "httpsoft/http-message": "^1.0", "maglnet/composer-require-checker": "^4.3", "nyholm/psr7": "^1.5", - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^10.5", "rector/rector": "^1.2", "roave/infection-static-analysis-plugin": "^1.25", "spatie/phpunit-watcher": "^1.23", @@ -50,7 +50,7 @@ "yiisoft/di": "^1.2", "yiisoft/psr-dummy-provider": "^1.0", "yiisoft/test-support": "^3.0", - "yiisoft/yii-debug": "dev-master|dev-php80" + "yiisoft/yii-debug": "dev-master" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cdcd698..c2a8648 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,16 +1,18 @@ - - + colors="true" + displayDetailsOnPhpunitDeprecations="true" +> @@ -21,9 +23,9 @@ - + - ./src + ./src - + diff --git a/psalm80.xml b/psalm80.xml deleted file mode 100644 index d091d59..0000000 --- a/psalm80.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - diff --git a/rector.php b/rector.php index f55daae..8efdb88 100644 --- a/rector.php +++ b/rector.php @@ -5,6 +5,7 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector; +use Rector\Php81\Rector\ClassMethod\NewInInitializerRector; use Rector\Set\ValueObject\LevelSetList; return static function (RectorConfig $rectorConfig): void { @@ -18,10 +19,11 @@ // define sets of rules $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_80, + LevelSetList::UP_TO_PHP_81, ]); $rectorConfig->skip([ ClosureToArrowFunctionRector::class, + NewInInitializerRector::class, ]); }; diff --git a/src/Csrf.php b/src/Csrf.php index 97a04ee..66b0f2b 100644 --- a/src/Csrf.php +++ b/src/Csrf.php @@ -10,8 +10,11 @@ final class Csrf implements Stringable { - public function __construct(private string $token, private string $parameterName, private string $headerName) - { + public function __construct( + private readonly string $token, + private readonly string $parameterName, + private readonly string $headerName, + ) { } public function getToken(): string diff --git a/src/Exception/InvalidLinkTagException.php b/src/Exception/InvalidLinkTagException.php index 12268dd..84a4dcd 100644 --- a/src/Exception/InvalidLinkTagException.php +++ b/src/Exception/InvalidLinkTagException.php @@ -8,12 +8,14 @@ use Yiisoft\FriendlyException\FriendlyExceptionInterface; /** - * InvalidLinkTagException is thrown if the link tag is incorrectly configured during the injection. + * `InvalidLinkTagException` is thrown if the link tag is incorrectly configured during the injection. */ final class InvalidLinkTagException extends RuntimeException implements FriendlyExceptionInterface { - public function __construct(string $message, private mixed $tag) - { + public function __construct( + string $message, + private readonly mixed $tag, + ) { parent::__construct($message); } diff --git a/src/Exception/InvalidMetaTagException.php b/src/Exception/InvalidMetaTagException.php index 6d6bd3f..2b51e2d 100644 --- a/src/Exception/InvalidMetaTagException.php +++ b/src/Exception/InvalidMetaTagException.php @@ -8,12 +8,14 @@ use Yiisoft\FriendlyException\FriendlyExceptionInterface; /** - * InvalidMetaTagException is thrown if the meta tag is incorrectly configured during the injection. + * `InvalidMetaTagException` is thrown if the meta tag is incorrectly configured during the injection. */ final class InvalidMetaTagException extends RuntimeException implements FriendlyExceptionInterface { - public function __construct(string $message, private mixed $tag) - { + public function __construct( + string $message, + private readonly mixed $tag, + ) { parent::__construct($message); } diff --git a/src/InjectionContainer/InjectionContainer.php b/src/InjectionContainer/InjectionContainer.php index 0213632..696c01a 100644 --- a/src/InjectionContainer/InjectionContainer.php +++ b/src/InjectionContainer/InjectionContainer.php @@ -9,7 +9,7 @@ final class InjectionContainer implements InjectionContainerInterface { public function __construct( - private ContainerInterface $container, + private readonly ContainerInterface $container, ) { } diff --git a/src/LayoutSpecificInjections.php b/src/LayoutSpecificInjections.php index 9ee8885..eb4c8a4 100644 --- a/src/LayoutSpecificInjections.php +++ b/src/LayoutSpecificInjections.php @@ -9,10 +9,10 @@ final class LayoutSpecificInjections /** * @var object[] */ - private array $injections; + private readonly array $injections; public function __construct( - private string $layout, + private readonly string $layout, object ...$injections ) { $this->injections = $injections; diff --git a/tests/Support/FakeCsrfToken.php b/tests/Support/FakeCsrfToken.php index 72ecaad..22fdf43 100644 --- a/tests/Support/FakeCsrfToken.php +++ b/tests/Support/FakeCsrfToken.php @@ -9,7 +9,7 @@ final class FakeCsrfToken implements CsrfTokenInterface { - private string $token; + private readonly string $token; public function __construct(?string $token = null) { diff --git a/tests/Support/RootNamespace/Fake8Controller.php b/tests/Support/RootNamespace/Fake8Controller.php new file mode 100644 index 0000000..474aeea --- /dev/null +++ b/tests/Support/RootNamespace/Fake8Controller.php @@ -0,0 +1,7 @@ +assertSame('de_DE locale', (string) $response->getBody()); } - public function dataWithController(): array + public static function dataWithController(): array { + require_once __DIR__ . '/Support/RootNamespace/FakeController.php'; + require_once __DIR__ . '/Support/RootNamespace/Fake8Controller.php'; + return [ - 'controller name, no "controller" / "controllers" namespaces, no subnamespaces' => [new Support\FakeController(), '/fake'], + 'controller name, no "controller" / "controllers" namespaces, no subnamespaces' => [ + new Support\FakeController(), + '/fake', + ], 'controller name, "controller" namespace, 1 subnamespace' => [ new Support\Controller\SubNamespace\FakeController(), '/sub-namespace/fake', @@ -211,11 +218,11 @@ public function dataWithController(): array '/fake', ], 'controller name with root namespace' => [ - $this->getMockBuilder('\stdClass')->setMockClassName('FakeController')->getMock(), + new \FakeController(), '/fake', ], 'controller class contains number' => [ - $this->getMockBuilder('\stdClass')->setMockClassName('Fake8Controller')->getMock(), + new \Fake8Controller(), '/fake8', ], 'namespace contains number' => [ @@ -233,9 +240,7 @@ public function dataWithController(): array ]; } - /** - * @dataProvider dataWithController - */ + #[DataProvider('dataWithController')] public function testWithController(object $controller, string $path): void { $renderer = $this->getRenderer()->withController($controller); @@ -255,7 +260,7 @@ public function testTwiceWithController(): void $this->assertSame($this->getViewsDir() . '/fake', $renderer->getViewPath()); } - public function dataWithIncorrectController(): array + public static function dataWithIncorrectController(): array { return [ 'root namespace' => [new stdClass()], @@ -267,9 +272,7 @@ public function dataWithIncorrectController(): array ]; } - /** - * @dataProvider dataWithIncorrectController - */ + #[DataProvider('dataWithIncorrectController')] public function testWithIncorrectController(object $controller): void { $this->expectException(RuntimeException::class);