diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0dde6b68..8506ea11 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/rector.yml b/.github/workflows/rector.yml new file mode 100644 index 00000000..adacd735 --- /dev/null +++ b/.github/workflows/rector.yml @@ -0,0 +1,21 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + +name: rector + +jobs: + rector: + uses: yiisoft/actions/.github/workflows/rector.yml@master + with: + os: >- + ['ubuntu-latest'] + php: >- + ['8.0'] diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 40ac2609..96b26790 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/CHANGELOG.md b/CHANGELOG.md index 613f72f5..383ea0c7 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 diff --git a/README.md b/README.md index fae67d5c..63859de1 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,14 @@ 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 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/composer.json b/composer.json index 6805800b..e9ce6704 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,13 @@ "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" }, "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", diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..63713ce9 --- /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, + ]); +}; diff --git a/src/Factory.php b/src/Factory.php index edfea07d..074e7e1d 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -17,26 +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; /** - * @var bool $validate If definitions should be validated when set. - */ - private bool $validate; - - /** - * 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 @@ -44,20 +34,16 @@ 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); } /** - * @param array $definitions Definitions to create objects with. - * @psalm-param array $definitions + * @param array $definitions Definitions to create objects with. * * @throws InvalidConfigException - * - * @return self */ public function withDefinitions(array $definitions): self { @@ -134,7 +120,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): mixed { if ($this->validate) { DefinitionValidator::validate($config); @@ -156,18 +142,17 @@ public function create($config) } /** - * @param mixed $config - * * @throws InvalidConfigException */ - private function createDefinition($config): DefinitionInterface + 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 d647cfd4..687f3d46 100644 --- a/src/FactoryInternalContainer.php +++ b/src/FactoryInternalContainer.php @@ -27,45 +27,27 @@ 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 + * @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(ContainerInterface $container, array $definitions = []) - { - $this->container = $container; - $this->definitions = $definitions; + public function __construct( + private ContainerInterface $container, + private array $definitions = [] + ) { } /** - * @param array $definitions Definitions to create objects with. - * @psalm-param array $definitions - * - * @return self + * @param array $definitions Definitions to create objects with. */ public function withDefinitions(array $definitions): self { @@ -80,11 +62,8 @@ public function withDefinitions(array $definitions): self * @inheritDoc * * @param string $id - * - * @return mixed|object - * @psalm-suppress InvalidThrow */ - public function get($id) + public function get($id): mixed { if ($this->hasDefinition($id)) { return $this->build($id); @@ -102,10 +81,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; @@ -170,23 +146,21 @@ 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) + 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); diff --git a/src/NotFoundException.php b/src/NotFoundException.php index bb84e203..3bdb8845 100644 --- a/src/NotFoundException.php +++ b/src/NotFoundException.php @@ -12,14 +12,12 @@ */ 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) - { - $this->id = $id; + public function __construct( + private string $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 3e4bea88..a212d602 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 d7143b20..06dbdcfd 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 ?CircularB $b = null) { - $this->b = $b; } } diff --git a/tests/Support/Circular/CircularB.php b/tests/Support/Circular/CircularB.php index c96a9bf6..5b2486a7 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 ?CircularA $a = null) { - $this->a = $a; } } diff --git a/tests/Support/Cube.php b/tests/Support/Cube.php index 871a848e..3b3a3f3e 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 f4311d68..f77bff5c 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 92b7dd87..0ffc33f8 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 7592bdc8..e579f5cf 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 ac699560..2df8f193 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 6142b113..a4fdaff3 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 91116722..00fd9705 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 1338359d..98225e8f 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 ?EngineInterface $engine) { - $this->engine = $engine; } public function getEngine(): ?EngineInterface diff --git a/tests/Support/NullableScalarConstructorArgument.php b/tests/Support/NullableScalarConstructorArgument.php index a4c67495..efe31397 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 55ba1007..1f531294 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 30e55e6d..4301a6fd 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 2eb0321d..02a17532 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 6b208dca..c0661456 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 13006a8f..bbd29b48 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; }