From 5e7fc3945591a1c3fce0d95799d7733cde073b53 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 27 Sep 2022 16:20:27 +0300 Subject: [PATCH 01/10] Add rector files https://github.com/yiisoft/yii-dev-tool/pull/232 --- .github/workflows/rector.yml | 65 ++++++++++++++++++++++++++++++++++++ rector.php | 22 ++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 .github/workflows/rector.yml create mode 100644 rector.php diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml new file mode 100644 index 0000000..b235fb0 --- /dev/null +++ b/.github/workflows/rector.yml @@ -0,0 +1,65 @@ +on: + push: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.env.example' + - '.gitattributes' + - 'infection.json.dist' + - 'phpunit.xml.dist' + +name: rector + +jobs: + rector: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.0 + tools: composer:v2 + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v2 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + + - name: Update composer + run: composer self-update + + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - run: vendor/bin/rector process --ansi + + - name: Check for Rector modified files + id: rector-git-check + run: echo ::set-output name=modified::$(if git diff --exit-code --no-patch; then echo "false"; else echo "true"; fi) + + - name: Git config + if: steps.rector-git-check.outputs.modified == 'true' + run: | + git config --global user.name 'rector-bot' + git config --global user.email 'rector@yiiframework.com' + + - name: Commit Rector changes + if: steps.rector-git-check.outputs.modified == 'true' + run: git commit -am "[rector] Apply fixes" + + - name: Push changes + if: steps.rector-git-check.outputs.modified == 'true' + run: git push + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..63713ce --- /dev/null +++ b/rector.php @@ -0,0 +1,22 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80, + ]); +}; From fb91052a3f3c4f224cb2509870aa49d1d5d5fdb1 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 27 Sep 2022 16:37:55 +0300 Subject: [PATCH 02/10] Add rector/rector dependecy --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 6805800..c6a4c1d 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", + "rector/rector": "^0.14.3", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^4.18", From 7ee305816678e25a6180c8091a3b6f8fc4d48f8e Mon Sep 17 00:00:00 2001 From: rector-bot Date: Tue, 27 Sep 2022 13:45:30 +0000 Subject: [PATCH 03/10] [rector] Apply fixes --- src/Factory.php | 16 +++------------- src/FactoryInternalContainer.php | 19 +------------------ src/NotFoundException.php | 5 +---- tests/Support/Car.php | 6 +----- tests/Support/Circular/CircularA.php | 5 +---- tests/Support/Circular/CircularB.php | 5 +---- tests/Support/Cube.php | 5 +---- tests/Support/EngineMarkOne.php | 5 +---- .../ExcessiveConstructorParameters.php | 5 +---- tests/Support/Firefighter.php | 5 +---- tests/Support/GearBox.php | 5 +---- tests/Support/MethodTest.php | 5 +---- tests/Support/NullableConcreteDependency.php | 5 +---- tests/Support/NullableInterfaceDependency.php | 5 +---- .../NullableScalarConstructorArgument.php | 5 +---- tests/Support/Phone.php | 6 +----- tests/Support/PinkCircle.php | 5 +---- tests/Support/ScalarConstructorArgument.php | 5 +---- tests/Support/TwoParametersDependency.php | 8 +------- tests/Support/VariadicConstructor.php | 14 +------------- 20 files changed, 22 insertions(+), 117 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index edfea07..7f2f8de 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -25,11 +25,6 @@ final class Factory { private FactoryInternalContainer $internalContainer; - /** - * @var bool $validate If definitions should be validated when set. - */ - private bool $validate; - /** * Factory constructor. * @@ -44,9 +39,8 @@ final class Factory public function __construct( ContainerInterface $container, array $definitions = [], - bool $validate = true + private bool $validate = true ) { - $this->validate = $validate; $this->validateDefinitions($definitions); $this->internalContainer = new FactoryInternalContainer($container, $definitions); } @@ -56,8 +50,6 @@ public function __construct( * @psalm-param array $definitions * * @throws InvalidConfigException - * - * @return self */ public function withDefinitions(array $definitions): self { @@ -134,7 +126,7 @@ private function validateDefinitions(array $definitions): void * @psalm-return ($config is class-string ? T : mixed) * @psalm-suppress MixedReturnStatement */ - public function create($config) + public function create(mixed $config) { if ($this->validate) { DefinitionValidator::validate($config); @@ -156,11 +148,9 @@ public function create($config) } /** - * @param mixed $config - * * @throws InvalidConfigException */ - private function createDefinition($config): DefinitionInterface + private function createDefinition(mixed $config): DefinitionInterface { $definition = Normalizer::normalize($config); diff --git a/src/FactoryInternalContainer.php b/src/FactoryInternalContainer.php index d647cfd..b27cf70 100644 --- a/src/FactoryInternalContainer.php +++ b/src/FactoryInternalContainer.php @@ -26,17 +26,6 @@ */ final class FactoryInternalContainer implements ContainerInterface { - /** - * @var ContainerInterface Container to use for resolving dependencies. - */ - private ContainerInterface $container; - - /** - * @var array Definitions to create objects with. - * @psalm-var array - */ - private array $definitions; - /** * @var DefinitionInterface[] Object created from definitions indexed by their types. * @psalm-var array @@ -55,17 +44,13 @@ final class FactoryInternalContainer implements ContainerInterface * @param array $definitions Definitions to create objects with. * @psalm-param array $definitions */ - public function __construct(ContainerInterface $container, array $definitions = []) + public function __construct(private ContainerInterface $container, private array $definitions = []) { - $this->container = $container; - $this->definitions = $definitions; } /** * @param array $definitions Definitions to create objects with. * @psalm-param array $definitions - * - * @return self */ public function withDefinitions(array $definitions): self { @@ -170,13 +155,11 @@ public function hasDefinition(string $id): bool } /** - * @param string $id * * @throws CircularReferenceException * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException - * * @return mixed|object */ private function build(string $id) diff --git a/src/NotFoundException.php b/src/NotFoundException.php index bb84e20..1943aee 100644 --- a/src/NotFoundException.php +++ b/src/NotFoundException.php @@ -12,14 +12,11 @@ */ final class NotFoundException extends Exception implements NotFoundExceptionInterface { - private string $id; - /** * @param string $id ID of the definition or name of the class that was not found. */ - public function __construct(string $id) + public function __construct(private string $id) { - $this->id = $id; parent::__construct(sprintf('No definition or class found or resolvable for %s.', $id)); } diff --git a/tests/Support/Car.php b/tests/Support/Car.php index 3e4bea8..a212d60 100644 --- a/tests/Support/Car.php +++ b/tests/Support/Car.php @@ -7,13 +7,9 @@ final class Car { public ?ColorInterface $color = null; - private EngineInterface $engine; - private array $moreEngines; - public function __construct(EngineInterface $engine, array $moreEngines = []) + public function __construct(private EngineInterface $engine, private array $moreEngines = []) { - $this->engine = $engine; - $this->moreEngines = $moreEngines; } public function getEngine(): EngineInterface diff --git a/tests/Support/Circular/CircularA.php b/tests/Support/Circular/CircularA.php index d7143b2..046769a 100644 --- a/tests/Support/Circular/CircularA.php +++ b/tests/Support/Circular/CircularA.php @@ -6,10 +6,7 @@ final class CircularA { - public ?CircularB $b; - - public function __construct(?CircularB $b = null) + public function __construct(public ?\Yiisoft\Factory\Tests\Support\Circular\CircularB $b = null) { - $this->b = $b; } } diff --git a/tests/Support/Circular/CircularB.php b/tests/Support/Circular/CircularB.php index c96a9bf..bbff57e 100644 --- a/tests/Support/Circular/CircularB.php +++ b/tests/Support/Circular/CircularB.php @@ -6,10 +6,7 @@ final class CircularB { - public ?CircularA $a; - - public function __construct(?CircularA $a = null) + public function __construct(public ?\Yiisoft\Factory\Tests\Support\Circular\CircularA $a = null) { - $this->a = $a; } } diff --git a/tests/Support/Cube.php b/tests/Support/Cube.php index 871a848..3b3a3f3 100644 --- a/tests/Support/Cube.php +++ b/tests/Support/Cube.php @@ -6,11 +6,8 @@ final class Cube { - private ColorInterface $color; - - public function __construct(ColorInterface $color) + public function __construct(private ColorInterface $color) { - $this->color = $color; } public function getColor(): ColorInterface diff --git a/tests/Support/EngineMarkOne.php b/tests/Support/EngineMarkOne.php index f4311d6..f77bff5 100644 --- a/tests/Support/EngineMarkOne.php +++ b/tests/Support/EngineMarkOne.php @@ -8,11 +8,8 @@ final class EngineMarkOne implements EngineInterface { public const NAME = 'Mark One'; - private int $number; - - public function __construct(int $number = 0) + public function __construct(private int $number = 0) { - $this->number = $number; } public function getName(): string diff --git a/tests/Support/ExcessiveConstructorParameters.php b/tests/Support/ExcessiveConstructorParameters.php index 92b7dd8..0ffc33f 100644 --- a/tests/Support/ExcessiveConstructorParameters.php +++ b/tests/Support/ExcessiveConstructorParameters.php @@ -8,13 +8,10 @@ final class ExcessiveConstructorParameters { - private $parameter; - private array $allParameters; - public function __construct($parameter) + public function __construct(private $parameter) { - $this->parameter = $parameter; $this->allParameters = func_get_args(); } diff --git a/tests/Support/Firefighter.php b/tests/Support/Firefighter.php index 7592bdc..e579f5c 100644 --- a/tests/Support/Firefighter.php +++ b/tests/Support/Firefighter.php @@ -6,11 +6,8 @@ final class Firefighter { - private ?string $name; - - public function __construct(?string $name) + public function __construct(private ?string $name) { - $this->name = $name; } public function getName(): ?string diff --git a/tests/Support/GearBox.php b/tests/Support/GearBox.php index ac69956..2df8f19 100644 --- a/tests/Support/GearBox.php +++ b/tests/Support/GearBox.php @@ -9,10 +9,7 @@ */ final class GearBox { - private int $maxGear; - - public function __construct(int $maxGear = 5) + public function __construct(private int $maxGear = 5) { - $this->maxGear = $maxGear; } } diff --git a/tests/Support/MethodTest.php b/tests/Support/MethodTest.php index 6142b11..a4fdaff 100644 --- a/tests/Support/MethodTest.php +++ b/tests/Support/MethodTest.php @@ -16,10 +16,7 @@ public function getValue() return $this->value; } - /** - * @param mixed $value - */ - public function setValue($value): void + public function setValue(mixed $value): void { $this->value = $value; } diff --git a/tests/Support/NullableConcreteDependency.php b/tests/Support/NullableConcreteDependency.php index 9111672..00fd970 100644 --- a/tests/Support/NullableConcreteDependency.php +++ b/tests/Support/NullableConcreteDependency.php @@ -6,11 +6,8 @@ final class NullableConcreteDependency { - private Car $car; - - public function __construct(?Car $car) + public function __construct(private ?Car $car) { - $this->car = $car; } public function getCar(): Car diff --git a/tests/Support/NullableInterfaceDependency.php b/tests/Support/NullableInterfaceDependency.php index 1338359..86c752f 100644 --- a/tests/Support/NullableInterfaceDependency.php +++ b/tests/Support/NullableInterfaceDependency.php @@ -6,11 +6,8 @@ final class NullableInterfaceDependency { - private ?EngineInterface $engine; - - public function __construct(?EngineInterface $engine) + public function __construct(private ?\Yiisoft\Factory\Tests\Support\EngineInterface $engine) { - $this->engine = $engine; } public function getEngine(): ?EngineInterface diff --git a/tests/Support/NullableScalarConstructorArgument.php b/tests/Support/NullableScalarConstructorArgument.php index a4c6749..efe3139 100644 --- a/tests/Support/NullableScalarConstructorArgument.php +++ b/tests/Support/NullableScalarConstructorArgument.php @@ -6,11 +6,8 @@ final class NullableScalarConstructorArgument { - private ?string $name; - - public function __construct(?string $name) + public function __construct(private ?string $name) { - $this->name = $name; } public function getName(): ?string diff --git a/tests/Support/Phone.php b/tests/Support/Phone.php index 55ba100..1f53129 100644 --- a/tests/Support/Phone.php +++ b/tests/Support/Phone.php @@ -7,8 +7,6 @@ final class Phone { private ?string $id = null; - private ?string $name; - private ?string $version; private array $colors; private array $apps = []; private ?string $author = null; @@ -17,10 +15,8 @@ final class Phone public bool $dev = false; public ?string $codeName = null; - public function __construct(?string $name = null, ?string $version = null, string ...$colors) + public function __construct(private ?string $name = null, private ?string $version = null, string ...$colors) { - $this->name = $name; - $this->version = $version; $this->colors = $colors; } diff --git a/tests/Support/PinkCircle.php b/tests/Support/PinkCircle.php index 30e55e6..4301a6f 100644 --- a/tests/Support/PinkCircle.php +++ b/tests/Support/PinkCircle.php @@ -6,11 +6,8 @@ final class PinkCircle { - private ColorPink $color; - - public function __construct(ColorPink $color) + public function __construct(private ColorPink $color) { - $this->color = $color; } public function getColor(): ColorPink diff --git a/tests/Support/ScalarConstructorArgument.php b/tests/Support/ScalarConstructorArgument.php index 2eb0321..02a1753 100644 --- a/tests/Support/ScalarConstructorArgument.php +++ b/tests/Support/ScalarConstructorArgument.php @@ -6,11 +6,8 @@ final class ScalarConstructorArgument { - private string $name; - - public function __construct(string $name) + public function __construct(private string $name) { - $this->name = $name; } public function getName(): string diff --git a/tests/Support/TwoParametersDependency.php b/tests/Support/TwoParametersDependency.php index 6b208dc..c066145 100644 --- a/tests/Support/TwoParametersDependency.php +++ b/tests/Support/TwoParametersDependency.php @@ -6,14 +6,8 @@ final class TwoParametersDependency { - private string $firstParameter; - - private string $secondParameter; - - public function __construct(string $firstParameter, string $secondParameter) + public function __construct(private string $firstParameter, private string $secondParameter) { - $this->firstParameter = $firstParameter; - $this->secondParameter = $secondParameter; } public function getFirstParameter(): string diff --git a/tests/Support/VariadicConstructor.php b/tests/Support/VariadicConstructor.php index 13006a8..bbd29b4 100644 --- a/tests/Support/VariadicConstructor.php +++ b/tests/Support/VariadicConstructor.php @@ -6,22 +6,10 @@ final class VariadicConstructor { - /** - * @var mixed - */ - private $first; - - private EngineInterface $engine; private array $parameters; - /** - * @param mixed $first - * @param mixed ...$parameters - */ - public function __construct($first, EngineInterface $engine, ...$parameters) + public function __construct(private mixed $first, private EngineInterface $engine, mixed ...$parameters) { - $this->first = $first; - $this->engine = $engine; $this->parameters = $parameters; } From 9167e52fdbb6f25d3bc88dd32ee7f5855f7d9549 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 27 Sep 2022 13:45:39 +0000 Subject: [PATCH 04/10] Apply fixes from StyleCI --- src/FactoryInternalContainer.php | 2 +- tests/Support/Circular/CircularA.php | 2 +- tests/Support/Circular/CircularB.php | 2 +- tests/Support/NullableInterfaceDependency.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FactoryInternalContainer.php b/src/FactoryInternalContainer.php index b27cf70..30a56e3 100644 --- a/src/FactoryInternalContainer.php +++ b/src/FactoryInternalContainer.php @@ -155,11 +155,11 @@ public function hasDefinition(string $id): bool } /** - * * @throws CircularReferenceException * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException + * * @return mixed|object */ private function build(string $id) diff --git a/tests/Support/Circular/CircularA.php b/tests/Support/Circular/CircularA.php index 046769a..06dbdcf 100644 --- a/tests/Support/Circular/CircularA.php +++ b/tests/Support/Circular/CircularA.php @@ -6,7 +6,7 @@ final class CircularA { - public function __construct(public ?\Yiisoft\Factory\Tests\Support\Circular\CircularB $b = null) + public function __construct(public ?CircularB $b = null) { } } diff --git a/tests/Support/Circular/CircularB.php b/tests/Support/Circular/CircularB.php index bbff57e..5b2486a 100644 --- a/tests/Support/Circular/CircularB.php +++ b/tests/Support/Circular/CircularB.php @@ -6,7 +6,7 @@ final class CircularB { - public function __construct(public ?\Yiisoft\Factory\Tests\Support\Circular\CircularA $a = null) + public function __construct(public ?CircularA $a = null) { } } diff --git a/tests/Support/NullableInterfaceDependency.php b/tests/Support/NullableInterfaceDependency.php index 86c752f..98225e8 100644 --- a/tests/Support/NullableInterfaceDependency.php +++ b/tests/Support/NullableInterfaceDependency.php @@ -6,7 +6,7 @@ final class NullableInterfaceDependency { - public function __construct(private ?\Yiisoft\Factory\Tests\Support\EngineInterface $engine) + public function __construct(private ?EngineInterface $engine) { } From c58507bea7454130e080d836d31e26ba2c6baa0f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 27 Sep 2022 21:39:12 +0300 Subject: [PATCH 05/10] Use predefined rector action --- .github/workflows/rector.yml | 60 +++++------------------------------- 1 file changed, 8 insertions(+), 52 deletions(-) diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml index b235fb0..adacd73 100644 --- a/.github/workflows/rector.yml +++ b/.github/workflows/rector.yml @@ -1,65 +1,21 @@ on: - push: + pull_request: paths-ignore: - 'docs/**' - 'README.md' - 'CHANGELOG.md' - '.gitignore' - - '.env.example' - '.gitattributes' - 'infection.json.dist' - - 'phpunit.xml.dist' + - 'psalm.xml' name: rector jobs: rector: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 8.0 - tools: composer:v2 - coverage: none - - - name: Determine composer cache directory - run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV - - - name: Cache dependencies installed with composer - uses: actions/cache@v2 - with: - path: ${{ env.COMPOSER_CACHE_DIR }} - key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: | - php${{ matrix.php }}-composer- - - - name: Update composer - run: composer self-update - - - name: Install dependencies with composer - run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - - - run: vendor/bin/rector process --ansi - - - name: Check for Rector modified files - id: rector-git-check - run: echo ::set-output name=modified::$(if git diff --exit-code --no-patch; then echo "false"; else echo "true"; fi) - - - name: Git config - if: steps.rector-git-check.outputs.modified == 'true' - run: | - git config --global user.name 'rector-bot' - git config --global user.email 'rector@yiiframework.com' - - - name: Commit Rector changes - if: steps.rector-git-check.outputs.modified == 'true' - run: git commit -am "[rector] Apply fixes" - - - name: Push changes - if: steps.rector-git-check.outputs.modified == 'true' - run: git push - + uses: yiisoft/actions/.github/workflows/rector.yml@master + with: + os: >- + ['ubuntu-latest'] + php: >- + ['8.0'] From 93f44fabc503c1e5bf1209c1f1fde444edc513c6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 31 Oct 2022 17:27:35 +0300 Subject: [PATCH 06/10] php 8.0 --- .github/workflows/build.yml | 2 +- .github/workflows/static.yml | 2 +- README.md | 2 +- composer.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0dde6b6..8506ea1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,4 +28,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['7.4', '8.0', '8.1'] + ['8.0', '8.1'] diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 40ac260..96b2679 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -28,4 +28,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['7.4', '8.0', '8.1'] + ['8.0', '8.1'] diff --git a/README.md b/README.md index fae67d5..aa524e2 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ with dependencies resolved by a [PSR-11](https://www.php-fig.org/psr/psr-11/) co ## Requirements -- PHP 7.4 or higher. +- PHP 8.0 or higher. ## Installation diff --git a/composer.json b/composer.json index c6a4c1d..e9ce670 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "source": "https://github.com/yiisoft/factory" }, "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "psr/container": "^1.0|^2.0", "yiisoft/definitions": "^1.0|^2.0" }, From e6f26fba706d0062a10f6754422d4a6d56d9b82d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 31 Oct 2022 17:31:21 +0300 Subject: [PATCH 07/10] cleanup --- src/Factory.php | 13 ++++--------- src/FactoryInternalContainer.php | 17 ++++++++--------- src/NotFoundException.php | 5 +++-- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 7f2f8de..d6f4e54 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -17,21 +17,16 @@ /** * Factory allows creating objects passing arguments runtime. - * A factory will try to use a PSR-11 compliant container to get dependencies, - * but will fall back to manual instantiation - * if the container cannot provide a required dependency. + * A factory will try to use a PSR-11 compliant container to get dependencies, but will fall back to manual + * instantiation if the container cannot provide a required dependency. */ final class Factory { private FactoryInternalContainer $internalContainer; /** - * Factory constructor. - * * @param ContainerInterface $container Container to use for resolving dependencies. - * @param array $definitions Definitions to create objects with. - * @psalm-param array $definitions - * + * @param array $definitions Definitions to create objects with. * @param bool $validate If definitions should be validated when set. * * @throws InvalidConfigException @@ -126,7 +121,7 @@ private function validateDefinitions(array $definitions): void * @psalm-return ($config is class-string ? T : mixed) * @psalm-suppress MixedReturnStatement */ - public function create(mixed $config) + public function create(mixed $config): mixed { if ($this->validate) { DefinitionValidator::validate($config); diff --git a/src/FactoryInternalContainer.php b/src/FactoryInternalContainer.php index 30a56e3..76e0e98 100644 --- a/src/FactoryInternalContainer.php +++ b/src/FactoryInternalContainer.php @@ -27,29 +27,28 @@ final class FactoryInternalContainer implements ContainerInterface { /** - * @var DefinitionInterface[] Object created from definitions indexed by their types. - * @psalm-var array + * @var array Object created from definitions indexed by their types. */ private array $definitionInstances = []; /** - * @var array Used to collect IDs instantiated during build to detect circular references. - * - * @psalm-var array + * @var array Used to collect IDs instantiated during build to detect circular references. */ private array $creatingIds = []; /** * @param ContainerInterface $container Container to use for resolving dependencies. - * @param array $definitions Definitions to create objects with. - * @psalm-param array $definitions + * @param array $definitions Definitions to create objects with. */ - public function __construct(private ContainerInterface $container, private array $definitions = []) - { + public function __construct( + private ContainerInterface $container, + private array $definitions = [] + ) { } /** * @param array $definitions Definitions to create objects with. + * * @psalm-param array $definitions */ public function withDefinitions(array $definitions): self diff --git a/src/NotFoundException.php b/src/NotFoundException.php index 1943aee..3bdb884 100644 --- a/src/NotFoundException.php +++ b/src/NotFoundException.php @@ -15,8 +15,9 @@ final class NotFoundException extends Exception implements NotFoundExceptionInte /** * @param string $id ID of the definition or name of the class that was not found. */ - public function __construct(private string $id) - { + public function __construct( + private string $id + ) { parent::__construct(sprintf('No definition or class found or resolvable for %s.', $id)); } From ee5b99761cdace826b54f512d07641ef2377aa7a Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 31 Oct 2022 17:37:42 +0300 Subject: [PATCH 08/10] cleanup --- src/Factory.php | 10 +++++----- src/FactoryInternalContainer.php | 27 +++++++++++---------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index d6f4e54..074e7e1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -41,8 +41,7 @@ public function __construct( } /** - * @param array $definitions Definitions to create objects with. - * @psalm-param array $definitions + * @param array $definitions Definitions to create objects with. * * @throws InvalidConfigException */ @@ -150,9 +149,10 @@ private function createDefinition(mixed $config): DefinitionInterface $definition = Normalizer::normalize($config); if ( - ($definition instanceof ArrayDefinition) && - $this->internalContainer->hasDefinition($definition->getClass()) && - ($containerDefinition = $this->internalContainer->getDefinition($definition->getClass())) instanceof ArrayDefinition + ($definition instanceof ArrayDefinition) + && $this->internalContainer->hasDefinition($definition->getClass()) + && ($containerDefinition = $this->internalContainer->getDefinition($definition->getClass())) + instanceof ArrayDefinition ) { $definition = $this->mergeDefinitions( $containerDefinition, diff --git a/src/FactoryInternalContainer.php b/src/FactoryInternalContainer.php index 76e0e98..d64bdf3 100644 --- a/src/FactoryInternalContainer.php +++ b/src/FactoryInternalContainer.php @@ -47,9 +47,7 @@ public function __construct( } /** - * @param array $definitions Definitions to create objects with. - * - * @psalm-param array $definitions + * @param array $definitions Definitions to create objects with. */ public function withDefinitions(array $definitions): self { @@ -68,7 +66,7 @@ public function withDefinitions(array $definitions): self * @return mixed|object * @psalm-suppress InvalidThrow */ - public function get($id) + public function get($id): mixed { if ($this->hasDefinition($id)) { return $this->build($id); @@ -86,10 +84,7 @@ public function has($id): bool return $this->hasDefinition($id) || $this->container->has($id); } - /** - * @return mixed - */ - public function create(DefinitionInterface $definition) + public function create(DefinitionInterface $definition): mixed { if ($definition instanceof ArrayDefinition) { $this->creatingIds[$definition->getClass()] = 1; @@ -158,17 +153,17 @@ public function hasDefinition(string $id): bool * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException - * - * @return mixed|object */ - private function build(string $id) + private function build(string $id): mixed { if (isset($this->creatingIds[$id])) { - throw new CircularReferenceException(sprintf( - 'Circular reference to "%s" detected while creating: %s.', - $id, - implode(', ', array_keys($this->creatingIds)) - )); + throw new CircularReferenceException( + sprintf( + 'Circular reference to "%s" detected while creating: %s.', + $id, + implode(', ', array_keys($this->creatingIds)) + ) + ); } $definition = $this->getDefinition($id); From 4b122e265f230aa49a00829f84dfaa4eb35f34ec Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 31 Oct 2022 17:38:59 +0300 Subject: [PATCH 09/10] improve --- README.md | 2 +- src/FactoryInternalContainer.php | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index aa524e2..63859de 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ with dependencies resolved by a [PSR-11](https://www.php-fig.org/psr/psr-11/) co The package could be installed with [composer](http://getcomposer.org/download/): ```shell -composer require yiisoft/definitions --prefer-dist +composer require yiisoft/definitions ``` ## General usage diff --git a/src/FactoryInternalContainer.php b/src/FactoryInternalContainer.php index d64bdf3..687f3d4 100644 --- a/src/FactoryInternalContainer.php +++ b/src/FactoryInternalContainer.php @@ -62,9 +62,6 @@ public function withDefinitions(array $definitions): self * @inheritDoc * * @param string $id - * - * @return mixed|object - * @psalm-suppress InvalidThrow */ public function get($id): mixed { From 87fd5a081f5428802d549af99fab53d025ec4732 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 31 Oct 2022 17:40:15 +0300 Subject: [PATCH 10/10] changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 613f72f..383ea0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,11 @@ ## 1.0.2 under development -- no changes in this release. +- Enh #162: Raise minimum PHP version to 8.0 and refactor code (@xepozz, @vjik) ## 1.0.1 June 17, 2022 -- Enh #159: Add support for `yiisoft/definitions` version `^2.0` (vjik) +- Enh #159: Add support for `yiisoft/definitions` version `^2.0` (@vjik) ## 1.0.0 December 11, 2021