From 07c6c00953c86e067f427bf251f4cf4211f7ebb1 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sun, 22 May 2022 02:02:21 +0200 Subject: [PATCH] Require symfony/dependency-injection v6.0 Since symfony/dependency-injection v6.0 requires PHP 8.0+, also update the codebase. --- .editorconfig | 2 +- .github/workflows/code-style.yml | 11 ++-- .github/workflows/static-analysis.yml | 4 +- .github/workflows/test.yml | 5 +- CODE_OF_CONDUCT.md | 7 +-- Dockerfile | 26 +++++--- LICENSE | 2 +- MIGRATION.md | 24 ++++++++ Makefile | 37 ++++++------ README.md | 46 +++++++------- composer.json | 25 +++++--- docker-compose.yml => docker-compose.yaml | 20 ++----- lib/FactoryRenderer.php | 37 ++++-------- lib/InterfaceResolver.php | 9 +-- .../BasicInterfaceResolver.php | 2 +- lib/MethodRenderer.php | 6 +- lib/ProxyDumper.php | 34 +++++------ phpcs.xml | 2 +- phpunit.xml | 13 ++-- tests/FactoryRendererTest.php | 24 ++++---- tests/IntegrationTest.php | 16 +++-- .../BasicInterfaceResolverTest.php | 16 +++-- tests/MethodRendererTest.php | 60 +++++++++---------- tests/ProxyDumperTest.php | 15 +++-- 24 files changed, 224 insertions(+), 219 deletions(-) create mode 100644 MIGRATION.md rename docker-compose.yml => docker-compose.yaml (61%) diff --git a/.editorconfig b/.editorconfig index 4e4d03e..9ff14ce 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,7 +8,7 @@ insert_final_newline = true indent_style = space indent_size = 4 -[*.yml] +[{*.yaml,*yml,*.neon}] indent_size = 2 [Makefile] diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index 822ac61..b106cd3 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -5,8 +5,7 @@ on: - pull_request jobs: - phpstan: - name: PHPCS + phpcs: runs-on: ubuntu-20.04 steps: - name: Checkout @@ -14,9 +13,9 @@ jobs: - name: Install PHP uses: shivammathur/setup-php@v2 with: + coverage: none php-version: "8.0" ini-values: memory_limit=-1 - tools: composer:v2, phpcs - - - name: Run PHPCS - run: phpcs + tools: phpcs, cs2pr + - name: Run PHP Code Sniffer + run: phpcs -q --report=checkstyle | cs2pr diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index dfda149..a53f31d 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -6,7 +6,7 @@ on: jobs: phpstan: - name: PHPStan + name: phpstan runs-on: ubuntu-20.04 steps: - name: Checkout @@ -28,5 +28,5 @@ jobs: - name: Install dependencies run: composer install --no-interaction --no-progress - - name: Run PHPStan + - name: Analyze run: vendor/bin/phpstan diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b874ddc..dfcf50c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,9 +11,8 @@ jobs: strategy: matrix: php-version: - - "7.2" - - "7.4" - "8.0" + - "8.1" steps: - name: Checkout uses: actions/checkout@v2 @@ -40,7 +39,7 @@ jobs: run: make test-coveralls - name: Upload code coverage - if: ${{ matrix.php-version == '7.2' }} + if: ${{ matrix.php-version == '8.0' }} env: COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index d8e2efa..8e1bb2c 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -61,7 +61,7 @@ representative at an online or offline event. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at -[INSERT CONTACT METHOD]. +. All complaints will be reviewed and investigated promptly and fairly. All community leaders are obligated to respect the privacy and security of the @@ -119,11 +119,11 @@ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at [https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0]. -Community Impact Guidelines were inspired by +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. For answers to common questions about this code of conduct, see the FAQ at -[https://www.contributor-covenant.org/faq][FAQ]. Translations are available +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations]. [homepage]: https://www.contributor-covenant.org @@ -131,4 +131,3 @@ at [https://www.contributor-covenant.org/translations][translations]. [Mozilla CoC]: https://github.com/mozilla/diversity [FAQ]: https://www.contributor-covenant.org/faq [translations]: https://www.contributor-covenant.org/translations - diff --git a/Dockerfile b/Dockerfile index c88a3eb..2ceb1c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,31 @@ ARG PHP_VERSION -FROM php:${PHP_VERSION}-alpine +FROM php:${PHP_VERSION}-cli-buster -RUN apk add --no-cache make $PHPIZE_DEPS &&\ +RUN apt-get update && \ + apt-get install -y autoconf pkg-config && \ + pecl channel-update pecl.php.net && \ pecl install xdebug &&\ - docker-php-ext-enable xdebug &&\ - echo $'\ + docker-php-ext-enable opcache xdebug + +RUN echo '\ xdebug.client_host=host.docker.internal\n\ xdebug.mode=develop\n\ xdebug.start_with_request=yes\n\ ' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini -ENV COMPOSER_ALLOW_SUPERUSER 1 +RUN echo '\ +display_errors=On\n\ +error_reporting=E_ALL\n\ +date.timezone=UTC\n\ +' >> /usr/local/etc/php/conf.d/php.ini -RUN curl -s https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer | php -- --quiet && \ - mv composer.phar /usr/local/bin/composer +ENV COMPOSER_ALLOW_SUPERUSER 1 -ENV PATH /root/.composer/vendor/bin:$PATH +RUN apt-get update && \ + apt-get install unzip && \ + curl -s https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer | php -- --quiet && \ + mv composer.phar /usr/local/bin/composer && \ + echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"\n' >> /root/.bashrc RUN composer global require squizlabs/php_codesniffer diff --git a/LICENSE b/LICENSE index 46a3d96..ddb7f5a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ The olvlvl/symfony-dependency-injection-proxy package is free software. It is released under the terms of the following BSD License. -Copyright (c) 2018-2020 by Olivier Laviale +Copyright (c) 2018-2022 by Olivier Laviale All rights reserved. Redistribution and use in source and binary forms, with or without modification, diff --git a/MIGRATION.md b/MIGRATION.md new file mode 100644 index 0000000..546b2a2 --- /dev/null +++ b/MIGRATION.md @@ -0,0 +1,24 @@ +# Migration + +## v3.x to v4.x + +### New requirements + +- Requires PHP v8.0.2+ +- Requires `symfony/dependency-injection` v6.0+ + +### New features + +N/A + +### Backward Incompatible Changes + +N/A + +### Deprecated Features + +N/A + +### Other Changes + +N/A diff --git a/Makefile b/Makefile index 5edb8f4..977d5ab 100644 --- a/Makefile +++ b/Makefile @@ -3,40 +3,41 @@ PHPUNIT = vendor/bin/phpunit vendor: @composer install +.PHONY: test-dependencies +test-dependencies: vendor test-cleanup + .PHONY: test -test: test-setup +test: test-dependencies @$(PHPUNIT) .PHONY: test-coverage -test-coverage: test-setup +test-coverage: test-dependencies @mkdir -p build/coverage @XDEBUG_MODE=coverage $(PHPUNIT) --coverage-html build/coverage .PHONY: test-coveralls -test-coveralls: test-setup +test-coveralls: test-dependencies @mkdir -p build/logs @XDEBUG_MODE=coverage $(PHPUNIT) --coverage-clover build/logs/clover.xml -.PHONY: test-container-72 -test-container-72: - @docker-compose run --rm app72 sh - @docker-compose down +.PHONY: test-cleanup +test-cleanup: + @rm -rf tests/sandbox/* -.PHONY: test-container-74 -test-container-74: - @docker-compose run --rm app74 sh - @docker-compose down +.PHONY: test-container +test-container: test-container-80 .PHONY: test-container-80 test-container-80: - @docker-compose run --rm app80 sh - @docker-compose down + @-docker-compose run --rm app80 bash + @docker-compose down -v -.PHONY: test-setup -test-setup: vendor $(PHPUNIT_FILENAME) - @rm -f tests/sandbox/* +.PHONY: test-container-81 +test-container-81: + @-docker-compose run --rm app81 bash + @docker-compose down -v .PHONY: lint lint: - @phpcs - @vendor/bin/phpstan + @XDEBUG_MODE=off phpcs -s + @XDEBUG_MODE=off vendor/bin/phpstan diff --git a/README.md b/README.md index e09ff5b..9fc3dc6 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,9 @@ [![Code Quality](https://img.shields.io/scrutinizer/g/olvlvl/symfony-dependency-injection-proxy.svg)](https://scrutinizer-ci.com/g/olvlvl/symfony-dependency-injection-proxy) [![Code Coverage](https://img.shields.io/coveralls/olvlvl/symfony-dependency-injection-proxy.svg)](https://coveralls.io/r/olvlvl/symfony-dependency-injection-proxy) -This package provides a proxy generator for [Symfony's dependency injection component][1] that generates super tiny, -super simple proxies, especially when [compared to Symfony's default implementation][2]. Here are some differences: +This package provides a proxy generator for [Symfony's dependency injection component][1] that +generates super tiny, super simple proxies, especially when [compared to Symfony's default +implementation][2]. Here are some differences: - Can proxy `final` classes. - Can only proxy classes with interfaces. @@ -14,8 +15,8 @@ super simple proxies, especially when [compared to Symfony's default implementat - The package is ~10Kb and doesn't have dependencies, other than `symfony/dependency-injection` of course. - The package can be removed once the proxies have been generated. -> If you're not familiar with proxy services, better have a look at [Symfony's documentation][3] before going any -> further. +If you're not familiar with proxy services, better have a look at [Symfony's documentation][3] +before going any further. @@ -29,9 +30,10 @@ composer require olvlvl/symfony-dependency-injection-proxy ## How it works -The generator works with the following assumptions: the service we want to proxy implements an interface, and services -using that service expect that interface, following the [dependency inversion principle][4]. Now, consider the following -code, where an `ExceptionHandler` service requires a logger implementing `LoggerInterface`: +The generator works with the following assumptions: the service we want to proxy implements an +interface, and services using that service expect that interface, following the [dependency +inversion principle][4]. Now, consider the following code, where an `ExceptionHandler` service +requires a logger implementing `LoggerInterface`: ```php logger = $logger; } // … } ``` -Imagine we're using [Monolog](https://github.com/Seldaek/monolog) as logger, and we have an expansive stream to set up. -Why waste time building the logger for every request when it's seldom used? That's when we mark our service as _lazy_. +Imagine we're using [Monolog](https://github.com/Seldaek/monolog) as logger, and we have an +expansive stream to set up. Why waste time building the logger for every request when it's seldom +used? That's when we mark our service as _lazy_. -The following example demonstrates how we can mark our `Psr\Log\LoggerInterface` service as lazy (we could use PHP code -or XML just the same): +The following example demonstrates how we can mark our `Psr\Log\LoggerInterface` service as lazy (we +could use PHP code or XML just the same): ```yaml services: @@ -75,8 +75,9 @@ services: # … ``` -> We don't have to define our service with a class, we could use `logger` instead of `Psr\Log\LoggerInterface` just -> the same, except we would have to define `class` for the factory one. +**Note:** We don't have to define our service with a class, we could use `logger` instead of +`Psr\Log\LoggerInterface` just > the same, except we would have to define `class` for the factory +one. Now let's see how to build our container. @@ -116,8 +117,9 @@ There you have it. We can use our container as usual and everything is awesome. ### What if my lazy service implements multiple interfaces? -The basic interface resolver will have a hard time figuring out which interface to implement if a service implements -many. For instance, if a service was an instance of `ArrayObject` the following exception would be thrown: +The basic interface resolver will have a hard time figuring out which interface to implement if a +service implements many. For instance, if a service was an instance of `ArrayObject` the following +exception would be thrown: ``` Don't know which interface to choose from for ArrayObject: IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable. @@ -138,7 +140,7 @@ ArrayObject: ## Continuous Integration -The project is continuously tested by [GitHub actions](https://github.com/olvlvl/event-dispatcher/actions). +The project is continuously tested by [GitHub actions](https://github.com/olvlvl/symfony-dependency-injection-proxy/actions). [![Tests](https://github.com/olvlvl/symfony-dependency-injection-proxy/workflows/test/badge.svg?branch=master)](https://github.com/olvlvl/symfony-dependency-injection-proxy/actions?query=workflow%3Atest) [![Static Analysis](https://github.com/olvlvl/symfony-dependency-injection-proxy/workflows/static-analysis/badge.svg?branch=master)](https://github.com/olvlvl/symfony-dependency-injection-proxy/actions?query=workflow%3Astatic-analysis) @@ -148,8 +150,8 @@ The project is continuously tested by [GitHub actions](https://github.com/olvlvl ## Code of Conduct -This project adheres to a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project and its -community, you are expected to uphold this code. +This project adheres to a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in +this project and its community, you are expected to uphold this code. diff --git a/composer.json b/composer.json index 1123a95..e0d73cf 100644 --- a/composer.json +++ b/composer.json @@ -7,23 +7,32 @@ "authors": [ { "name": "Olivier Laviale", - "email": "olivier.laviale@gmail.com" + "email": "olivier.laviale@gmail.com", + "homepage": "https://olvlvl.com/", + "role": "Developer" } ], "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "phpstan/extension-installer": true + } }, + "minimum-stability": "dev", + "prefer-stable": true, + "prefer-dist": true, "require": { - "php": ">=7.2.5", + "php": ">=8.0.2", "ext-json": "*", - "symfony/dependency-injection": "^5.0" + "symfony/dependency-injection": "^6.0" }, "require-dev": { - "jangregor/phpstan-prophecy": "^0.8.1", + "jangregor/phpstan-prophecy": "^1.0", + "phpspec/prophecy-phpunit": "^2.0", "phpstan/extension-installer": "^1.1", - "phpstan/phpstan": "^0.12.92", - "phpunit/phpunit": "^8.5", - "symfony/config": "^5.0" + "phpstan/phpstan": "^1.6", + "phpunit/phpunit": "^9.5", + "symfony/config": "^6.0" }, "autoload": { "psr-4": { diff --git a/docker-compose.yml b/docker-compose.yaml similarity index 61% rename from docker-compose.yml rename to docker-compose.yaml index d8e9940..2148f7f 100644 --- a/docker-compose.yml +++ b/docker-compose.yaml @@ -1,31 +1,21 @@ --- -version: "3.0" +version: "3.2" services: - app72: - build: - context: . - args: - PHP_VERSION: 7.2 - environment: - PHP_IDE_CONFIG: "serverName=symfony-di-proxy" - volumes: - - .:/app:delegated - - ~/.composer:/root/.composer:delegated - app74: + app80: build: context: . args: - PHP_VERSION: 7.4 + PHP_VERSION: 8.0 environment: PHP_IDE_CONFIG: "serverName=symfony-di-proxy" volumes: - .:/app:delegated - ~/.composer:/root/.composer:delegated - app80: + app81: build: context: . args: - PHP_VERSION: 8.0 + PHP_VERSION: 8.1 environment: PHP_IDE_CONFIG: "serverName=symfony-di-proxy" volumes: diff --git a/lib/FactoryRenderer.php b/lib/FactoryRenderer.php index ca29836..f2b3983 100644 --- a/lib/FactoryRenderer.php +++ b/lib/FactoryRenderer.php @@ -18,18 +18,11 @@ use function array_map; use function implode; -use const PHP_VERSION_ID; - class FactoryRenderer { - /** - * @var MethodRenderer - */ - private $methodRenderer; - - public function __construct(MethodRenderer $methodRenderer) - { - $this->methodRenderer = $methodRenderer; + public function __construct( + private MethodRenderer $methodRenderer + ) { } /** @@ -39,12 +32,7 @@ public function __construct(MethodRenderer $methodRenderer) */ public function __invoke(string $interface, string $factoryCode): string { - $methods = $this->renderMethods( - (new ReflectionClass($interface))->getMethods(), - PHP_VERSION_ID >= 70400 - ? '($this->service ??= ($this->factory)())' - : '($this->service ?: $this->service = ($this->factory)())' - ); + $methods = $this->renderMethods((new ReflectionClass($interface))->getMethods()); return <<factory = \$factory; + public function __construct( + private \Closure \$factory + ) { } $methods @@ -68,16 +56,17 @@ public function __construct(callable \$factory) /** * @param ReflectionMethod[] $methods */ - private function renderMethods(array $methods, string $getterCode): string + private function renderMethods(array $methods): string { $renderMethod = $this->methodRenderer; return implode( "\n", array_map( - function (ReflectionMethod $method) use ($renderMethod, $getterCode) { - return $renderMethod($method, $getterCode); - }, + fn(ReflectionMethod $method) => $renderMethod( + $method, + '($this->service ??= ($this->factory)())' + ), $methods ) ); diff --git a/lib/InterfaceResolver.php b/lib/InterfaceResolver.php index 005cc80..166a881 100644 --- a/lib/InterfaceResolver.php +++ b/lib/InterfaceResolver.php @@ -11,17 +11,18 @@ namespace olvlvl\SymfonyDependencyInjectionProxy; -use Exception; +use Throwable; interface InterfaceResolver { /** * Given a class, resolve the interface to use to create its proxy. * - * @phpstan-param class-string $class + * @param class-string $class * - * @phpstan-return class-string - * @throws Exception if the interface cannot be resolved. + * @return class-string + * + * @throws Throwable if the interface cannot be resolved. */ public function resolveInterface(string $class): string; } diff --git a/lib/InterfaceResolver/BasicInterfaceResolver.php b/lib/InterfaceResolver/BasicInterfaceResolver.php index b357185..6e4a62f 100644 --- a/lib/InterfaceResolver/BasicInterfaceResolver.php +++ b/lib/InterfaceResolver/BasicInterfaceResolver.php @@ -33,7 +33,7 @@ public function resolveInterface(string $class): string } if (class_exists($class)) { - /** @phpstan-var class-string[]|false $interfaces */ + /** @var class-string[]|false $interfaces */ $interfaces = class_implements($class); if ($interfaces) { diff --git a/lib/MethodRenderer.php b/lib/MethodRenderer.php index eaa4dbe..4411a8b 100644 --- a/lib/MethodRenderer.php +++ b/lib/MethodRenderer.php @@ -23,8 +23,6 @@ use function implode; use function json_encode; -use const PHP_VERSION_ID; - class MethodRenderer { public function __invoke(ReflectionMethod $method, string $getterCode): string @@ -107,7 +105,7 @@ function (ReflectionParameter $parameter) { private function renderType(ReflectionType $type): string { - if (PHP_VERSION_ID >= 80000 && $type instanceof ReflectionUnionType) { + if ($type instanceof ReflectionUnionType) { return implode('|', array_map(function (ReflectionNamedType $namedType) { return $namedType->getName(); }, $type->getTypes())); @@ -124,7 +122,7 @@ private function useReturn(ReflectionMethod $method): bool { $type = $method->getReturnType(); - if (PHP_VERSION_ID >= 80000 && $type instanceof ReflectionUnionType) { + if ($type instanceof ReflectionUnionType) { return true; } diff --git a/lib/ProxyDumper.php b/lib/ProxyDumper.php index 1ade757..4fd3642 100644 --- a/lib/ProxyDumper.php +++ b/lib/ProxyDumper.php @@ -11,13 +11,13 @@ namespace olvlvl\SymfonyDependencyInjectionProxy; -use Exception; use InvalidArgumentException; use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\BasicInterfaceResolver; use ReflectionException; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\LogicException; use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface; +use Throwable; use function class_exists; use function ltrim; @@ -25,18 +25,13 @@ final class ProxyDumper implements DumperInterface { - /** - * @var InterfaceResolver - */ - private $interfaceResolver; + private InterfaceResolver $interfaceResolver; + private FactoryRenderer $factoryRenderer; - /** - * @var FactoryRenderer - */ - private $factoryRenderer; - - public function __construct(InterfaceResolver $interfaceResolver = null, FactoryRenderer $factoryRenderer = null) - { + public function __construct( + InterfaceResolver $interfaceResolver = null, + FactoryRenderer $factoryRenderer = null + ) { $this->interfaceResolver = $interfaceResolver ?? new BasicInterfaceResolver(); $this->factoryRenderer = $factoryRenderer ?? new FactoryRenderer(new MethodRenderer()); } @@ -53,7 +48,7 @@ public function isProxyCandidate(Definition $definition): bool /** * @inheritdoc - * @throws Exception + * @throws Throwable */ public function getProxyFactoryCode(Definition $definition, string $id, string $factoryCode): string { @@ -76,7 +71,7 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ return <<interfaceResolver->resolveInterface($class); } /** - * @phpstan-return class-string|null + * @return class-string|null */ private function resolveInterfaceFromTags(Definition $definition): ?string { @@ -125,7 +121,7 @@ private function resolveInterfaceFromTags(Definition $definition): ?string } /** - * @phpstan-param class-string $interface + * @param class-string $interface * * @throws ReflectionException */ diff --git a/phpcs.xml b/phpcs.xml index 59891a4..815c4ec 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,7 +2,7 @@ lib tests - */tests/sandbox/* + tests/sandbox/* diff --git a/phpunit.xml b/phpunit.xml index 9771227..cf79b90 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,18 +1,17 @@ + + + ./lib + + ./tests - - - - ./lib - - diff --git a/tests/FactoryRendererTest.php b/tests/FactoryRendererTest.php index 60fc0d0..f3cd462 100644 --- a/tests/FactoryRendererTest.php +++ b/tests/FactoryRendererTest.php @@ -15,18 +15,20 @@ use olvlvl\SymfonyDependencyInjectionProxy\MethodRenderer; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; +use ReflectionException; use ReflectionMethod; use Serializable; -use const PHP_VERSION_ID; - /** * @group unit */ final class FactoryRendererTest extends TestCase { + use ProphecyTrait; + /** - * @throws \ReflectionException + * @throws ReflectionException */ public function testRender(): void { @@ -35,13 +37,9 @@ public function testRender(): void $methodRenderer = $this->prophesize(MethodRenderer::class); $methodRenderer->__invoke( Argument::type(ReflectionMethod::class), - PHP_VERSION_ID >= 70400 - ? '($this->service ??= ($this->factory)())' - : '($this->service ?: $this->service = ($this->factory)())' + '($this->service ??= ($this->factory)())' )->will( - function (array $args) { - return ' codeFor:' . $args[0]->getName(); - } + fn(array $args) => ' codeFor:' . $args[0]->getName() ); $stu = new FactoryRenderer($methodRenderer->reveal()); @@ -52,11 +50,11 @@ function () { } ) implements \\$interface { - private \$factory, \$service; + private \$service; - public function __construct(callable \$factory) - { - \$this->factory = \$factory; + public function __construct( + private \Closure \$factory + ) { } codeFor:serialize diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 30e8152..f5f6ae5 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -32,7 +32,7 @@ /** * @group integration */ -class IntegrationTest extends TestCase +final class IntegrationTest extends TestCase { /** * @dataProvider provideDefinition @@ -63,9 +63,7 @@ public function testCompilation(array $definitions, callable $assert, callable $ $assert(new $containerClass()); } - /** - * @return array[] - */ + // @phpstan-ignore-next-line public function provideDefinition(): array { $alias = 'alias-' . uniqid(); @@ -81,7 +79,7 @@ public function provideDefinition(): array ->addArgument($value = uniqid()), ], function (ContainerInterface $container) use ($id, $value) { - /* @var SampleInterface $service */ + /** @var SampleInterface $service */ $service = $container->get($id); $this->assertSame($service, $container->get($id)); @@ -103,7 +101,7 @@ function (ContainerInterface $container) use ($id, $value) { ->addTag('proxy', ['interface' => SampleInterface2::class]), ], function (ContainerInterface $container) use ($id, $value2) { - /* @var SampleInterface2 $service */ + /** @var SampleInterface2 $service */ $service = $container->get($id); $this->assertSame($service, $container->get($id)); @@ -127,7 +125,7 @@ function (ContainerInterface $container) use ($id, $value2) { ->addArgument($factoryName = 'factory-' . uniqid()), ], function (ContainerInterface $container) use ($id, $factoryName) { - /* @var BuildableInterface $service */ + /** @var BuildableInterface $service */ $service = $container->get($id); $this->assertSame($service, $container->get($id)); @@ -147,7 +145,7 @@ function (ContainerInterface $container) use ($id, $factoryName) { ->addArgument($value = uniqid()), ], function (ContainerInterface $container) use ($id, $alias, $value) { - /* @var SampleInterface $service */ + /** @var SampleInterface $service */ $service = $container->get($alias); $this->assertSame($service, $container->get($alias)); $this->assertSame($service, $container->get($id)); @@ -172,7 +170,7 @@ function (ContainerBuilder $builder) use ($id, $alias) { ->addArgument($value = uniqid()), ], function (ContainerInterface $container) use ($alias, $value) { - /* @var SampleInterface $service */ + /** @var SampleInterface $service */ $service = $container->get($alias); $this->assertSame($service, $container->get($alias)); diff --git a/tests/InterfaceResolver/BasicInterfaceResolverTest.php b/tests/InterfaceResolver/BasicInterfaceResolverTest.php index ad493f8..3dc7d2b 100644 --- a/tests/InterfaceResolver/BasicInterfaceResolverTest.php +++ b/tests/InterfaceResolver/BasicInterfaceResolverTest.php @@ -14,10 +14,10 @@ use ArrayIterator; use DateTimeImmutable; use DateTimeInterface; -use Exception; use LogicException; use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\BasicInterfaceResolver; use PHPUnit\Framework\TestCase; +use Throwable; /** * @group unit @@ -26,7 +26,7 @@ final class BasicInterfaceResolverTest extends TestCase { /** * @test - * @throws Exception + * @throws Throwable */ public function shouldFailIfClassImplementsManyInterfaces(): void { @@ -34,14 +34,14 @@ public function shouldFailIfClassImplementsManyInterfaces(): void $this->expectException(LogicException::class); $this->expectExceptionMessageMatches( - "/Don't know which interface to choose from for ArrayIterator: Iterator,/" + "/Don't know which interface to choose from for ArrayIterator:.*Iterator/" ); $stu->resolveInterface(ArrayIterator::class); } /** * @test - * @throws Exception + * @throws Throwable */ public function shouldFailIfClassDoesNotExist(): void { @@ -57,9 +57,9 @@ public function shouldFailIfClassDoesNotExist(): void /** * @dataProvider provideResolveInterface * - * @phpstan-param class-string $class + * @param class-string $class * - * @throws Exception + * @throws Throwable */ public function testResolveInterface(string $class, string $expected): void { @@ -67,9 +67,7 @@ public function testResolveInterface(string $class, string $expected): void $this->assertSame($expected, $stu->resolveInterface($class)); } - /** - * @return array[] - */ + // @phpstan-ignore-next-line public function provideResolveInterface(): array { return [ diff --git a/tests/MethodRendererTest.php b/tests/MethodRendererTest.php index 30eade7..e61421f 100644 --- a/tests/MethodRendererTest.php +++ b/tests/MethodRendererTest.php @@ -18,8 +18,6 @@ use tests\olvlvl\SymfonyDependencyInjectionProxy\cases\SampleInterfaceForMethodRenderer72; use tests\olvlvl\SymfonyDependencyInjectionProxy\cases\SampleInterfaceForMethodRenderer80; -use const PHP_VERSION_ID; - /** * @group unit */ @@ -35,23 +33,27 @@ public function testRender(ReflectionMethod $method, string $getterCode, string $this->assertEquals($expected, $stu($method, $getterCode)); } - /** - * @return array[] - */ + // @phpstan-ignore-next-line public function provideRender(): array { $getterCode = "get()"; - $reflectionFor = function (string $method) { - return new ReflectionMethod(SampleInterfaceForMethodRenderer70::class, $method); - }; - $reflectionFor72 = function (string $method) { - return new ReflectionMethod(SampleInterfaceForMethodRenderer72::class, $method); - }; + $reflectionFor70 = fn(string $method) => new ReflectionMethod( + SampleInterfaceForMethodRenderer70::class, + $method + ); + $reflectionFor72 = fn(string $method) => new ReflectionMethod( + SampleInterfaceForMethodRenderer72::class, + $method + ); + $reflectionFor80 = fn(string $method) => new ReflectionMethod( + SampleInterfaceForMethodRenderer80::class, + $method + ); $cases = [ [ - $reflectionFor('aStaticMethodWithoutParametersOrReturnType'), + $reflectionFor70('aStaticMethodWithoutParametersOrReturnType'), $getterCode, <<= 80000) { - $reflectionFor80 = function (string $method) { - return new ReflectionMethod(SampleInterfaceForMethodRenderer80::class, $method); - }; - - $cases["aMethodWithMixed"] = [ + "a method with mixed" => [ $reflectionFor80('aMethodWithMixed'), $getterCode, <<aMethodWithMixed(\$a); } PHPTPL - ]; + ], - $cases["aMethodWithUnionTypes"] = [ + "a method with union types" => [ $reflectionFor80('aMethodWithUnionTypes'), $getterCode, <<aMethodWithUnionTypes(\$a); } PHPTPL - ]; - } + ], + + ]; return $cases; } diff --git a/tests/ProxyDumperTest.php b/tests/ProxyDumperTest.php index a2af3b2..4cd739a 100644 --- a/tests/ProxyDumperTest.php +++ b/tests/ProxyDumperTest.php @@ -20,6 +20,7 @@ use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver; use olvlvl\SymfonyDependencyInjectionProxy\ProxyDumper; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\DependencyInjection\Definition; /** @@ -27,6 +28,8 @@ */ final class ProxyDumperTest extends TestCase { + use ProphecyTrait; + /** * @dataProvider provideIsProxyCandidate */ @@ -40,9 +43,7 @@ public function testIsProxyCandidate(Definition $definition, bool $expected): vo $this->assertSame($expected, $stu->isProxyCandidate($definition)); } - /** - * @return array[] - */ + // @phpstan-ignore-next-line public function provideIsProxyCandidate(): array { $factory = 'aFactory'; @@ -89,7 +90,7 @@ public function testGetProxyFactoryCode(string $id, bool $private, bool $shared, { $definition = (new Definition()) ->setClass($class = ArrayIterator::class) - ->setPrivate($private) + ->setPublic(!$private) ->setShared($shared); $interfaceResolver = $this->prophesize(InterfaceResolver::class); $interfaceResolver->resolveInterface($class) @@ -105,7 +106,7 @@ public function testGetProxyFactoryCode(string $id, bool $private, bool $shared, $expected = <<assertEquals($expected, $stu->getProxyFactoryCode($definition, $id, $factoryCode)); } - /** - * @return array[] - */ + // @phpstan-ignore-next-line public function provideGetProxyFactoryCode(): array { $id = 'aServiceId';