From aa006a0d0344ec4cd758d5f79908abbc6f7f705f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:09:13 +0700 Subject: [PATCH 1/7] Apply PHP 8.0 Syntax and constructor promotion Signed-off-by: Abdul Malik Ikhsan --- src/Exception/CyclicAliasException.php | 2 +- src/Exception/InvalidArgumentException.php | 10 ++-------- src/InitializerInterface.php | 3 +-- src/PluginManagerInterface.php | 3 +-- src/Proxy/LazyServiceFactory.php | 12 ++--------- src/ServiceManager.php | 4 +--- src/Tool/ConfigDumper.php | 14 +++---------- src/Tool/ConfigDumperCommand.php | 8 ++------ src/Tool/FactoryCreator.php | 9 +++------ src/Tool/FactoryCreatorCommand.php | 8 ++------ .../ClassAcceptingConfigToConstructor.php | 5 +---- ...llKnownServicesAsConstructorParameters.php | 5 +---- .../ClassWithMixedConstructorParameters.php | 20 ++++--------------- ...thScalarDependencyDefiningDefaultValue.php | 5 +---- .../TestAsset/ClassWithScalarParameters.php | 8 +------- ...lassWithTypeHintedConstructorParameter.php | 5 +---- test/AbstractPluginManagerTest.php | 3 +-- test/CommonServiceLocatorBehaviorsTrait.php | 16 ++++++--------- test/Proxy/LazyServiceFactoryTest.php | 2 +- ...ManagerContainerInteropIntegrationTest.php | 3 +-- test/ServiceManagerTest.php | 4 +--- test/TestAsset/CallTimesAbstractFactory.php | 2 +- test/TestAsset/FactoryObject.php | 9 +-------- test/TestAsset/Foo.php | 6 +----- test/TestAsset/InvokableObject.php | 5 +---- test/TestAsset/ObjectWithScalarDependency.php | 6 +----- test/TestAsset/V2ValidationPluginManager.php | 5 +---- test/TestAsset/V2v3PluginManager.php | 6 ++---- .../laminas-code/ClassReflection.php | 4 ++-- .../laminas-code/ParameterReflection.php | 4 ++-- 30 files changed, 49 insertions(+), 147 deletions(-) diff --git a/src/Exception/CyclicAliasException.php b/src/Exception/CyclicAliasException.php index 51a42c2f..806da73c 100644 --- a/src/Exception/CyclicAliasException.php +++ b/src/Exception/CyclicAliasException.php @@ -43,7 +43,7 @@ public static function fromCyclicAlias(string $alias, array $aliases): self public static function fromAliasesMap(array $aliases) { $detectedCycles = array_filter(array_map( - static fn($alias) => self::getCycleFor($aliases, $alias), + static fn($alias): ?array => self::getCycleFor($aliases, $alias), array_keys($aliases) )); diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index e9c011eb..dd8ee808 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -17,10 +17,7 @@ */ class InvalidArgumentException extends SplInvalidArgumentException implements ExceptionInterface { - /** - * @param mixed $initializer - */ - public static function fromInvalidInitializer($initializer): self + public static function fromInvalidInitializer(mixed $initializer): self { return new self(sprintf( 'An invalid initializer was registered. Expected a callable or an' @@ -30,10 +27,7 @@ public static function fromInvalidInitializer($initializer): self )); } - /** - * @param mixed $abstractFactory - */ - public static function fromInvalidAbstractFactory($abstractFactory): self + public static function fromInvalidAbstractFactory(mixed $abstractFactory): self { return new self(sprintf( 'An invalid abstract factory was registered. Expected an instance of or a valid' diff --git a/src/InitializerInterface.php b/src/InitializerInterface.php index f092e763..dc29dd6f 100644 --- a/src/InitializerInterface.php +++ b/src/InitializerInterface.php @@ -29,8 +29,7 @@ interface InitializerInterface extends Initializer\InitializerInterface /** * Initialize * - * @param mixed $instance * @return mixed */ - public function initialize($instance, ServiceLocatorInterface $serviceLocator); + public function initialize(mixed $instance, ServiceLocatorInterface $serviceLocator); } diff --git a/src/PluginManagerInterface.php b/src/PluginManagerInterface.php index f0d1540b..cb82d4e6 100644 --- a/src/PluginManagerInterface.php +++ b/src/PluginManagerInterface.php @@ -19,12 +19,11 @@ interface PluginManagerInterface extends ServiceLocatorInterface /** * Validate an instance * - * @param mixed $instance * @return void * @throws InvalidServiceException If created instance does not respect the * constraint on type imposed by the plugin manager. * @throws ContainerExceptionInterface If any other error occurs. * @psalm-assert InstanceType $instance */ - public function validate($instance); + public function validate(mixed $instance); } diff --git a/src/Proxy/LazyServiceFactory.php b/src/Proxy/LazyServiceFactory.php index a873e8c7..0a65c396 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -21,19 +21,12 @@ */ final class LazyServiceFactory implements DelegatorFactoryInterface { - private LazyLoadingValueHolderFactory $proxyFactory; - - /** @var array map of service names to class names */ - private array $servicesMap; - /** * @param array $servicesMap A map of service names to * class names of their respective classes */ - public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $servicesMap) + public function __construct(private LazyLoadingValueHolderFactory $proxyFactory, private array $servicesMap) { - $this->proxyFactory = $proxyFactory; - $this->servicesMap = $servicesMap; } /** @@ -45,10 +38,9 @@ public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $ public function __invoke(ContainerInterface $container, $name, callable $callback, ?array $options = null) { if (isset($this->servicesMap[$name])) { - $initializer = function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback) { + $initializer = static function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback): bool { $proxy->setProxyInitializer(null); $wrappedInstance = $callback(); - return true; }; diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 647d978e..6e5c59eb 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -157,7 +157,6 @@ class ServiceManager implements ServiceLocatorInterface * See {@see \Laminas\ServiceManager\ServiceManager::configure()} for details * on what $config accepts. * - * @param array $config * @psalm-param ServiceManagerConfiguration $config */ public function __construct(array $config = []) @@ -285,7 +284,6 @@ public function getAllowOverride() } /** - * @param array $config * @psalm-param ServiceManagerConfiguration $config * @return self * @throws ContainerModificationsNotAllowedException If the allow @@ -562,7 +560,7 @@ private function getFactory(string $name): callable */ private function createDelegatorFromName(string $name, ?array $options = null) { - $creationCallback = function () use ($name, $options) { + $creationCallback = function () use ($name, $options): object { // Code is inlined for performance reason, instead of abstracting the creation $factory = $this->getFactory($name); return $factory($this->creationContext, $name, $options); diff --git a/src/Tool/ConfigDumper.php b/src/Tool/ConfigDumper.php index 6e7d99ec..c73ba94c 100644 --- a/src/Tool/ConfigDumper.php +++ b/src/Tool/ConfigDumper.php @@ -21,6 +21,7 @@ use function interface_exists; use function is_array; use function is_int; +use function is_iterable; use function is_string; use function sprintf; use function str_repeat; @@ -39,15 +40,11 @@ class ConfigDumper return %s; EOC; - private ?ContainerInterface $container; - - public function __construct(?ContainerInterface $container = null) + public function __construct(private ?ContainerInterface $container = null) { - $this->container = $container; } /** - * @param array $config * @param string $className * @param bool $ignoreUnresolved * @return array @@ -127,7 +124,6 @@ private function validateClassName($className) } /** - * @param array $config * @param string $className * @return array */ @@ -138,7 +134,6 @@ private function createInvokable(array $config, $className) } /** - * @param array $config * @return array * @throws InvalidArgumentException If ConfigAbstractFactory configuration * value is not an array. @@ -164,7 +159,6 @@ public function createFactoryMappingsFromConfig(array $config) } /** - * @param array $config * @param string $className * @return array */ @@ -185,7 +179,6 @@ public function createFactoryMappings(array $config, $className) } /** - * @param array $config * @return string */ public function dumpConfigFile(array $config) @@ -245,11 +238,10 @@ private function createConfigKey($key) } /** - * @param mixed $value * @param int $indentLevel * @return string */ - private function createConfigValue($value, $indentLevel) + private function createConfigValue(mixed $value, $indentLevel) { if (is_array($value) || $value instanceof Traversable) { return $this->prepareConfig($value, $indentLevel + 1); diff --git a/src/Tool/ConfigDumperCommand.php b/src/Tool/ConfigDumperCommand.php index 6ce1fda8..f9747868 100644 --- a/src/Tool/ConfigDumperCommand.php +++ b/src/Tool/ConfigDumperCommand.php @@ -68,15 +68,12 @@ class ConfigDumperCommand private ConsoleHelper $helper; - private string $scriptName; - /** * @param string $scriptName */ - public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null) + public function __construct(private $scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null) { - $this->scriptName = $scriptName; - $this->helper = $helper ?: new ConsoleHelper(); + $this->helper = $helper ?: new ConsoleHelper(); } /** @@ -128,7 +125,6 @@ public function __invoke(array $args) } /** - * @param array $args * @return object */ private function parseArgs(array $args) diff --git a/src/Tool/FactoryCreator.php b/src/Tool/FactoryCreator.php index 0e9b24df..304e53ec 100644 --- a/src/Tool/FactoryCreator.php +++ b/src/Tool/FactoryCreator.php @@ -100,14 +100,12 @@ private function getConstructorParameters($className) $constructorParameters = array_filter( $constructorParameters, - function (ReflectionParameter $argument): bool { + static function (ReflectionParameter $argument): bool { if ($argument->isOptional()) { return false; } - $type = $argument->getType(); $class = $type instanceof ReflectionNamedType && ! $type->isBuiltin() ? $type->getName() : null; - if (null === $class) { throw new InvalidArgumentException(sprintf( 'Cannot identify type for constructor argument "%s"; ' @@ -115,7 +113,6 @@ function (ReflectionParameter $argument): bool { $argument->getName() )); } - return true; } ); @@ -124,7 +121,7 @@ function (ReflectionParameter $argument): bool { return []; } - return array_map(function (ReflectionParameter $parameter): ?string { + return array_map(static function (ReflectionParameter $parameter): ?string { $type = $parameter->getType(); return $type instanceof ReflectionNamedType && ! $type->isBuiltin() ? $type->getName() : null; }, $constructorParameters); @@ -136,7 +133,7 @@ function (ReflectionParameter $argument): bool { */ private function createArgumentString($className) { - $arguments = array_map(fn(string $dependency): string + $arguments = array_map(static fn(string $dependency): string => sprintf('$container->get(\\%s::class)', $dependency), $this->getConstructorParameters($className)); switch (count($arguments)) { diff --git a/src/Tool/FactoryCreatorCommand.php b/src/Tool/FactoryCreatorCommand.php index ec6c1fc6..a89df165 100644 --- a/src/Tool/FactoryCreatorCommand.php +++ b/src/Tool/FactoryCreatorCommand.php @@ -45,15 +45,12 @@ class FactoryCreatorCommand private ConsoleHelper $helper; - private string $scriptName; - /** * @param string $scriptName */ - public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null) + public function __construct(private $scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null) { - $this->scriptName = $scriptName; - $this->helper = $helper ?: new ConsoleHelper(); + $this->helper = $helper ?: new ConsoleHelper(); } /** @@ -98,7 +95,6 @@ public function __invoke(array $args) } /** - * @param array $args * @return ArgumentObject */ private function parseArgs(array $args) diff --git a/test/AbstractFactory/TestAsset/ClassAcceptingConfigToConstructor.php b/test/AbstractFactory/TestAsset/ClassAcceptingConfigToConstructor.php index e505d633..c00d6ac8 100644 --- a/test/AbstractFactory/TestAsset/ClassAcceptingConfigToConstructor.php +++ b/test/AbstractFactory/TestAsset/ClassAcceptingConfigToConstructor.php @@ -6,10 +6,7 @@ final class ClassAcceptingConfigToConstructor { - public array $config; - - public function __construct(array $config) + public function __construct(public array $config) { - $this->config = $config; } } diff --git a/test/AbstractFactory/TestAsset/ClassAcceptingWellKnownServicesAsConstructorParameters.php b/test/AbstractFactory/TestAsset/ClassAcceptingWellKnownServicesAsConstructorParameters.php index 846dfdd0..c3c2a68d 100644 --- a/test/AbstractFactory/TestAsset/ClassAcceptingWellKnownServicesAsConstructorParameters.php +++ b/test/AbstractFactory/TestAsset/ClassAcceptingWellKnownServicesAsConstructorParameters.php @@ -6,10 +6,7 @@ final class ClassAcceptingWellKnownServicesAsConstructorParameters { - public ValidatorPluginManager $validators; - - public function __construct(ValidatorPluginManager $validators) + public function __construct(public ValidatorPluginManager $validators) { - $this->validators = $validators; } } diff --git a/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php b/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php index e66dedd5..dcff6578 100644 --- a/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php +++ b/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php @@ -6,23 +6,11 @@ final class ClassWithMixedConstructorParameters { - public array $config; - - public SampleInterface $sample; - - public ValidatorPluginManager $validators; - - public ?array $options; - public function __construct( - array $config, - SampleInterface $sample, - ValidatorPluginManager $validators, - ?array $options = null + public array $config, + public SampleInterface $sample, + public ValidatorPluginManager $validators, + public ?array $options = null ) { - $this->config = $config; - $this->sample = $sample; - $this->validators = $validators; - $this->options = $options; } } diff --git a/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php b/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php index 63c76806..063a55cb 100644 --- a/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php +++ b/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php @@ -6,10 +6,7 @@ final class ClassWithScalarDependencyDefiningDefaultValue { - public string $foo; - - public function __construct(string $foo = 'bar') + public function __construct(public string $foo = 'bar') { - $this->foo = $foo; } } diff --git a/test/AbstractFactory/TestAsset/ClassWithScalarParameters.php b/test/AbstractFactory/TestAsset/ClassWithScalarParameters.php index 95ea25a4..74f902ee 100644 --- a/test/AbstractFactory/TestAsset/ClassWithScalarParameters.php +++ b/test/AbstractFactory/TestAsset/ClassWithScalarParameters.php @@ -6,13 +6,7 @@ final class ClassWithScalarParameters { - public string $foo = 'foo'; - - public string $bar = 'bar'; - - public function __construct(string $foo, string $bar) + public function __construct(public string $foo, public string $bar) { - $this->foo = $foo; - $this->bar = $bar; } } diff --git a/test/AbstractFactory/TestAsset/ClassWithTypeHintedConstructorParameter.php b/test/AbstractFactory/TestAsset/ClassWithTypeHintedConstructorParameter.php index e5ba91e3..fcce2516 100644 --- a/test/AbstractFactory/TestAsset/ClassWithTypeHintedConstructorParameter.php +++ b/test/AbstractFactory/TestAsset/ClassWithTypeHintedConstructorParameter.php @@ -6,10 +6,7 @@ final class ClassWithTypeHintedConstructorParameter { - public SampleInterface $sample; - - public function __construct(SampleInterface $sample) + public function __construct(public SampleInterface $sample) { - $this->sample = $sample; } } diff --git a/test/AbstractPluginManagerTest.php b/test/AbstractPluginManagerTest.php index 2caaff93..2410f93d 100644 --- a/test/AbstractPluginManagerTest.php +++ b/test/AbstractPluginManagerTest.php @@ -265,10 +265,9 @@ public function invalidConstructorArguments(): array /** * @group migration - * @param mixed $arg * @dataProvider invalidConstructorArguments */ - public function testPassingNonContainerNonConfigNonNullFirstConstructorArgumentRaisesException($arg): void + public function testPassingNonContainerNonConfigNonNullFirstConstructorArgumentRaisesException(mixed $arg): void { $this->expectException(InvalidArgumentException::class); new TestAsset\LenientPluginManager($arg); diff --git a/test/CommonServiceLocatorBehaviorsTrait.php b/test/CommonServiceLocatorBehaviorsTrait.php index eec5ee41..e8a4e276 100644 --- a/test/CommonServiceLocatorBehaviorsTrait.php +++ b/test/CommonServiceLocatorBehaviorsTrait.php @@ -436,9 +436,8 @@ public function abstractFactories(): array /** * @group has * @dataProvider abstractFactories - * @param mixed $abstractFactory */ - public function testHasChecksAgainstAbstractFactories($abstractFactory, bool $expected): void + public function testHasChecksAgainstAbstractFactories(mixed $abstractFactory, bool $expected): void { $serviceManager = $this->createContainer([ 'abstract_factories' => [ @@ -572,11 +571,10 @@ public function invalidAbstractFactories(): array /** * @dataProvider invalidAbstractFactories - * @param mixed $factory * @covers \Laminas\ServiceManager\ServiceManager::configure */ public function testPassingInvalidAbstractFactoryTypeViaConfigurationRaisesException( - $factory, + mixed $factory, string $contains = 'invalid abstract factory' ): void { $this->expectException(InvalidArgumentException::class); @@ -617,11 +615,10 @@ public function invalidInitializers(): array /** * @dataProvider invalidInitializers - * @param mixed $initializer * @covers \Laminas\ServiceManager\ServiceManager::configure */ public function testPassingInvalidInitializerTypeViaConfigurationRaisesException( - $initializer, + mixed $initializer, string $contains = 'invalid initializer' ): void { $this->expectException(InvalidArgumentException::class); @@ -651,12 +648,11 @@ public function invalidDelegators(): array } /** - * @param mixed $delegator * @dataProvider invalidDelegators * @covers \Laminas\ServiceManager\ServiceManager::createDelegatorFromName */ public function testInvalidDelegatorShouldRaiseExceptionDuringCreation( - $delegator, + mixed $delegator, string $contains = 'non-callable delegator' ): void { /** @psalm-suppress InvalidArgument */ @@ -859,7 +855,7 @@ public function methodsAffectedByOverrideSettings(): array 'setFactory', [ 'foo', - function () { + static function (): void { }, ], ], @@ -870,7 +866,7 @@ function () { 'addDelegator', [ 'foo', - function () { + static function (): void { }, ], ], diff --git a/test/Proxy/LazyServiceFactoryTest.php b/test/Proxy/LazyServiceFactoryTest.php index 757ff425..a8227cb0 100644 --- a/test/Proxy/LazyServiceFactoryTest.php +++ b/test/Proxy/LazyServiceFactoryTest.php @@ -87,7 +87,7 @@ public function testCreates(): void ->expects(self::once()) ->method('createProxy') ->willReturnCallback( - static function ($className, $initializer) use ($expectedService, $proxy) { + static function ($className, $initializer) use ($expectedService, $proxy): MockObject { self::assertEquals('FooClass', $className, 'class name not match'); $wrappedInstance = null; diff --git a/test/ServiceManagerContainerInteropIntegrationTest.php b/test/ServiceManagerContainerInteropIntegrationTest.php index 815146dd..a4f54ac7 100644 --- a/test/ServiceManagerContainerInteropIntegrationTest.php +++ b/test/ServiceManagerContainerInteropIntegrationTest.php @@ -15,8 +15,7 @@ final class ServiceManagerContainerInteropIntegrationTest extends TestCase { - /** @var ServiceManager */ - private $container; + private ServiceManager $container; protected function setUp(): void { diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 7346898b..d63eb116 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -17,8 +17,6 @@ use Psr\Container\ContainerInterface; use stdClass; -use function get_class; - /** * @see ConfigInterface * @@ -399,7 +397,7 @@ public function testFactoryMayBeStaticMethodDescribedByCallableString(): void ]; $serviceManager = new SimpleServiceManager($config); - self::assertEquals(stdClass::class, get_class($serviceManager->get(stdClass::class))); + self::assertEquals(stdClass::class, $serviceManager->get(stdClass::class)::class); } public function testResolvedAliasFromAbstractFactory(): void diff --git a/test/TestAsset/CallTimesAbstractFactory.php b/test/TestAsset/CallTimesAbstractFactory.php index 5fce487e..8e06057c 100644 --- a/test/TestAsset/CallTimesAbstractFactory.php +++ b/test/TestAsset/CallTimesAbstractFactory.php @@ -9,7 +9,7 @@ final class CallTimesAbstractFactory implements AbstractFactoryInterface { - protected static int $callTimes = 0; + private static int $callTimes = 0; /** {@inheritDoc} */ public function canCreate(containerinterface $container, $name) diff --git a/test/TestAsset/FactoryObject.php b/test/TestAsset/FactoryObject.php index df072a3c..2e050015 100644 --- a/test/TestAsset/FactoryObject.php +++ b/test/TestAsset/FactoryObject.php @@ -6,14 +6,7 @@ final class FactoryObject { - /** @var mixed */ - public $dependency; - - /** - * @param mixed $dependency - */ - public function __construct($dependency) + public function __construct(public mixed $dependency) { - $this->dependency = $dependency; } } diff --git a/test/TestAsset/Foo.php b/test/TestAsset/Foo.php index a1ce475d..e4e6c527 100644 --- a/test/TestAsset/Foo.php +++ b/test/TestAsset/Foo.php @@ -6,14 +6,10 @@ final class Foo { - /** @var array|null */ - protected array $options; - /** * @param array|null $options */ - public function __construct(?array $options = null) + public function __construct(protected ?array $options = null) { - $this->options = $options; } } diff --git a/test/TestAsset/InvokableObject.php b/test/TestAsset/InvokableObject.php index b44cd484..6d53519c 100644 --- a/test/TestAsset/InvokableObject.php +++ b/test/TestAsset/InvokableObject.php @@ -6,11 +6,8 @@ class InvokableObject { - public array $options; - - public function __construct(array $options = []) + public function __construct(public array $options = []) { - $this->options = $options; } public function getOptions(): array diff --git a/test/TestAsset/ObjectWithScalarDependency.php b/test/TestAsset/ObjectWithScalarDependency.php index 49cc5f27..b2651860 100644 --- a/test/TestAsset/ObjectWithScalarDependency.php +++ b/test/TestAsset/ObjectWithScalarDependency.php @@ -6,11 +6,7 @@ final class ObjectWithScalarDependency { - /** - * @param mixed $aName - * @param mixed $aValue - */ - public function __construct($aName, $aValue) + public function __construct(mixed $aName, mixed $aValue) { } } diff --git a/test/TestAsset/V2ValidationPluginManager.php b/test/TestAsset/V2ValidationPluginManager.php index 44a564fe..17df10a5 100644 --- a/test/TestAsset/V2ValidationPluginManager.php +++ b/test/TestAsset/V2ValidationPluginManager.php @@ -15,10 +15,7 @@ final class V2ValidationPluginManager extends AbstractPluginManager /** @var (callable(mixed):void)|null */ public $assertion; - /** - * @param mixed $plugin - */ - public function validatePlugin($plugin): void + public function validatePlugin(mixed $plugin): void { if (! is_callable($this->assertion)) { throw new RuntimeException(sprintf( diff --git a/test/TestAsset/V2v3PluginManager.php b/test/TestAsset/V2v3PluginManager.php index 8c68462b..d825e9fe 100644 --- a/test/TestAsset/V2v3PluginManager.php +++ b/test/TestAsset/V2v3PluginManager.php @@ -34,8 +34,7 @@ final class V2v3PluginManager extends AbstractPluginManager /** @var string */ protected $instanceOf = InvokableObject::class; - /** @var bool */ - protected $shareByDefault = false; + private bool $shareByDefault = false; /** @var bool */ protected $sharedByDefault = false; @@ -59,11 +58,10 @@ public function validate($plugin) } /** - * @param mixed $plugin * @return void * @throws RuntimeException */ - public function validatePlugin($plugin) + public function validatePlugin(mixed $plugin) { try { $this->validate($plugin); diff --git a/test/TestAsset/laminas-code/ClassReflection.php b/test/TestAsset/laminas-code/ClassReflection.php index 7ad47694..f5f5d166 100644 --- a/test/TestAsset/laminas-code/ClassReflection.php +++ b/test/TestAsset/laminas-code/ClassReflection.php @@ -19,7 +19,7 @@ * @todo Remove once laminas-code has a release that supports PHP 8.1. * @internal */ -class ClassReflection extends ReflectionClass implements ReflectionInterface +class ClassReflection extends ReflectionClass implements ReflectionInterface, \Stringable { /** @var DocBlockReflection|null */ protected $docBlock; @@ -224,7 +224,7 @@ public function toString() /** * @return string */ - public function __toString() + public function __toString(): string { return parent::__toString(); } diff --git a/test/TestAsset/laminas-code/ParameterReflection.php b/test/TestAsset/laminas-code/ParameterReflection.php index d1ba559d..62a0120a 100644 --- a/test/TestAsset/laminas-code/ParameterReflection.php +++ b/test/TestAsset/laminas-code/ParameterReflection.php @@ -15,7 +15,7 @@ * @todo Remove once laminas-code has a release that supports PHP 8.1. * @internal */ -class ParameterReflection extends ReflectionParameter implements ReflectionInterface +class ParameterReflection extends ReflectionParameter implements ReflectionInterface, \Stringable { /** @var bool */ protected $isFromMethod = false; @@ -122,7 +122,7 @@ public function toString() /** * @return string */ - public function __toString() + public function __toString(): string { return parent::__toString(); } From 44f698479e3b69bf600939d2a2f40f242d553589 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:23:50 +0700 Subject: [PATCH 2/7] apply mixed type on validate() AbstractPluginManager Signed-off-by: Abdul Malik Ikhsan --- src/AbstractPluginManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index 004292eb..1e113e12 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -177,7 +177,7 @@ public function get($name, ?array $options = null) * * @psalm-assert InstanceType $instance */ - public function validate($instance) + public function validate(mixed $instance) { if (method_exists($this, 'validatePlugin')) { trigger_error(sprintf( From 659673630a7661dfdd4f4722a928224b635cb06b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:24:04 +0700 Subject: [PATCH 3/7] exclude TestAsset for change Signed-off-by: Abdul Malik Ikhsan --- .../TestAsset/ClassWithMixedConstructorParameters.php | 8 ++------ test/CommonServiceLocatorBehaviorsTrait.php | 4 ++-- test/Proxy/LazyServiceFactoryTest.php | 2 +- test/ServiceManagerContainerInteropIntegrationTest.php | 2 +- test/ServiceManagerTest.php | 2 ++ test/TestAsset/CallTimesAbstractFactory.php | 2 +- test/TestAsset/FactoryObject.php | 9 ++++++++- test/TestAsset/Foo.php | 6 +++++- test/TestAsset/InvokableObject.php | 5 ++++- test/TestAsset/ObjectWithScalarDependency.php | 6 +++++- test/TestAsset/V2ValidationPluginManager.php | 5 ++++- test/TestAsset/V2v3PluginManager.php | 6 ++++-- test/TestAsset/laminas-code/ClassReflection.php | 4 ++-- test/TestAsset/laminas-code/ParameterReflection.php | 4 ++-- 14 files changed, 43 insertions(+), 22 deletions(-) diff --git a/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php b/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php index dcff6578..74e0efd9 100644 --- a/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php +++ b/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php @@ -6,11 +6,7 @@ final class ClassWithMixedConstructorParameters { - public function __construct( - public array $config, - public SampleInterface $sample, - public ValidatorPluginManager $validators, - public ?array $options = null - ) { + public function __construct(public array $config, public SampleInterface $sample, public ValidatorPluginManager $validators, public ?array $options = null) + { } } diff --git a/test/CommonServiceLocatorBehaviorsTrait.php b/test/CommonServiceLocatorBehaviorsTrait.php index e8a4e276..7125ebac 100644 --- a/test/CommonServiceLocatorBehaviorsTrait.php +++ b/test/CommonServiceLocatorBehaviorsTrait.php @@ -855,7 +855,7 @@ public function methodsAffectedByOverrideSettings(): array 'setFactory', [ 'foo', - static function (): void { + static function () : void { }, ], ], @@ -866,7 +866,7 @@ static function (): void { 'addDelegator', [ 'foo', - static function (): void { + static function () : void { }, ], ], diff --git a/test/Proxy/LazyServiceFactoryTest.php b/test/Proxy/LazyServiceFactoryTest.php index a8227cb0..c9499dc0 100644 --- a/test/Proxy/LazyServiceFactoryTest.php +++ b/test/Proxy/LazyServiceFactoryTest.php @@ -87,7 +87,7 @@ public function testCreates(): void ->expects(self::once()) ->method('createProxy') ->willReturnCallback( - static function ($className, $initializer) use ($expectedService, $proxy): MockObject { + static function ($className, $initializer) use ($expectedService, $proxy): \PHPUnit\Framework\MockObject\MockObject { self::assertEquals('FooClass', $className, 'class name not match'); $wrappedInstance = null; diff --git a/test/ServiceManagerContainerInteropIntegrationTest.php b/test/ServiceManagerContainerInteropIntegrationTest.php index a4f54ac7..6714d98a 100644 --- a/test/ServiceManagerContainerInteropIntegrationTest.php +++ b/test/ServiceManagerContainerInteropIntegrationTest.php @@ -15,7 +15,7 @@ final class ServiceManagerContainerInteropIntegrationTest extends TestCase { - private ServiceManager $container; + private \Laminas\ServiceManager\ServiceManager $container; protected function setUp(): void { diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index d63eb116..ea15ccbf 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -17,6 +17,8 @@ use Psr\Container\ContainerInterface; use stdClass; +use function get_class; + /** * @see ConfigInterface * diff --git a/test/TestAsset/CallTimesAbstractFactory.php b/test/TestAsset/CallTimesAbstractFactory.php index 8e06057c..5fce487e 100644 --- a/test/TestAsset/CallTimesAbstractFactory.php +++ b/test/TestAsset/CallTimesAbstractFactory.php @@ -9,7 +9,7 @@ final class CallTimesAbstractFactory implements AbstractFactoryInterface { - private static int $callTimes = 0; + protected static int $callTimes = 0; /** {@inheritDoc} */ public function canCreate(containerinterface $container, $name) diff --git a/test/TestAsset/FactoryObject.php b/test/TestAsset/FactoryObject.php index 2e050015..df072a3c 100644 --- a/test/TestAsset/FactoryObject.php +++ b/test/TestAsset/FactoryObject.php @@ -6,7 +6,14 @@ final class FactoryObject { - public function __construct(public mixed $dependency) + /** @var mixed */ + public $dependency; + + /** + * @param mixed $dependency + */ + public function __construct($dependency) { + $this->dependency = $dependency; } } diff --git a/test/TestAsset/Foo.php b/test/TestAsset/Foo.php index e4e6c527..a1ce475d 100644 --- a/test/TestAsset/Foo.php +++ b/test/TestAsset/Foo.php @@ -6,10 +6,14 @@ final class Foo { + /** @var array|null */ + protected array $options; + /** * @param array|null $options */ - public function __construct(protected ?array $options = null) + public function __construct(?array $options = null) { + $this->options = $options; } } diff --git a/test/TestAsset/InvokableObject.php b/test/TestAsset/InvokableObject.php index 6d53519c..b44cd484 100644 --- a/test/TestAsset/InvokableObject.php +++ b/test/TestAsset/InvokableObject.php @@ -6,8 +6,11 @@ class InvokableObject { - public function __construct(public array $options = []) + public array $options; + + public function __construct(array $options = []) { + $this->options = $options; } public function getOptions(): array diff --git a/test/TestAsset/ObjectWithScalarDependency.php b/test/TestAsset/ObjectWithScalarDependency.php index b2651860..49cc5f27 100644 --- a/test/TestAsset/ObjectWithScalarDependency.php +++ b/test/TestAsset/ObjectWithScalarDependency.php @@ -6,7 +6,11 @@ final class ObjectWithScalarDependency { - public function __construct(mixed $aName, mixed $aValue) + /** + * @param mixed $aName + * @param mixed $aValue + */ + public function __construct($aName, $aValue) { } } diff --git a/test/TestAsset/V2ValidationPluginManager.php b/test/TestAsset/V2ValidationPluginManager.php index 17df10a5..44a564fe 100644 --- a/test/TestAsset/V2ValidationPluginManager.php +++ b/test/TestAsset/V2ValidationPluginManager.php @@ -15,7 +15,10 @@ final class V2ValidationPluginManager extends AbstractPluginManager /** @var (callable(mixed):void)|null */ public $assertion; - public function validatePlugin(mixed $plugin): void + /** + * @param mixed $plugin + */ + public function validatePlugin($plugin): void { if (! is_callable($this->assertion)) { throw new RuntimeException(sprintf( diff --git a/test/TestAsset/V2v3PluginManager.php b/test/TestAsset/V2v3PluginManager.php index d825e9fe..8c68462b 100644 --- a/test/TestAsset/V2v3PluginManager.php +++ b/test/TestAsset/V2v3PluginManager.php @@ -34,7 +34,8 @@ final class V2v3PluginManager extends AbstractPluginManager /** @var string */ protected $instanceOf = InvokableObject::class; - private bool $shareByDefault = false; + /** @var bool */ + protected $shareByDefault = false; /** @var bool */ protected $sharedByDefault = false; @@ -58,10 +59,11 @@ public function validate($plugin) } /** + * @param mixed $plugin * @return void * @throws RuntimeException */ - public function validatePlugin(mixed $plugin) + public function validatePlugin($plugin) { try { $this->validate($plugin); diff --git a/test/TestAsset/laminas-code/ClassReflection.php b/test/TestAsset/laminas-code/ClassReflection.php index f5f5d166..7ad47694 100644 --- a/test/TestAsset/laminas-code/ClassReflection.php +++ b/test/TestAsset/laminas-code/ClassReflection.php @@ -19,7 +19,7 @@ * @todo Remove once laminas-code has a release that supports PHP 8.1. * @internal */ -class ClassReflection extends ReflectionClass implements ReflectionInterface, \Stringable +class ClassReflection extends ReflectionClass implements ReflectionInterface { /** @var DocBlockReflection|null */ protected $docBlock; @@ -224,7 +224,7 @@ public function toString() /** * @return string */ - public function __toString(): string + public function __toString() { return parent::__toString(); } diff --git a/test/TestAsset/laminas-code/ParameterReflection.php b/test/TestAsset/laminas-code/ParameterReflection.php index 62a0120a..d1ba559d 100644 --- a/test/TestAsset/laminas-code/ParameterReflection.php +++ b/test/TestAsset/laminas-code/ParameterReflection.php @@ -15,7 +15,7 @@ * @todo Remove once laminas-code has a release that supports PHP 8.1. * @internal */ -class ParameterReflection extends ReflectionParameter implements ReflectionInterface, \Stringable +class ParameterReflection extends ReflectionParameter implements ReflectionInterface { /** @var bool */ protected $isFromMethod = false; @@ -122,7 +122,7 @@ public function toString() /** * @return string */ - public function __toString(): string + public function __toString() { return parent::__toString(); } From f6ef40f5c94c16ffd35c739e08772f3f941ce395 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:26:44 +0700 Subject: [PATCH 4/7] cs fix Signed-off-by: Abdul Malik Ikhsan --- src/Tool/ConfigDumper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tool/ConfigDumper.php b/src/Tool/ConfigDumper.php index c73ba94c..6dd6b7f6 100644 --- a/src/Tool/ConfigDumper.php +++ b/src/Tool/ConfigDumper.php @@ -21,7 +21,6 @@ use function interface_exists; use function is_array; use function is_int; -use function is_iterable; use function is_string; use function sprintf; use function str_repeat; From 455ca05a4a71c5d966a7021cf15f807c0386713b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:28:21 +0700 Subject: [PATCH 5/7] rollback spacing Signed-off-by: Abdul Malik Ikhsan --- src/Proxy/LazyServiceFactory.php | 1 + src/Tool/FactoryCreator.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/Proxy/LazyServiceFactory.php b/src/Proxy/LazyServiceFactory.php index 0a65c396..65b04465 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -41,6 +41,7 @@ public function __invoke(ContainerInterface $container, $name, callable $callbac $initializer = static function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback): bool { $proxy->setProxyInitializer(null); $wrappedInstance = $callback(); + return true; }; diff --git a/src/Tool/FactoryCreator.php b/src/Tool/FactoryCreator.php index 304e53ec..6abcbf69 100644 --- a/src/Tool/FactoryCreator.php +++ b/src/Tool/FactoryCreator.php @@ -104,8 +104,10 @@ static function (ReflectionParameter $argument): bool { if ($argument->isOptional()) { return false; } + $type = $argument->getType(); $class = $type instanceof ReflectionNamedType && ! $type->isBuiltin() ? $type->getName() : null; + if (null === $class) { throw new InvalidArgumentException(sprintf( 'Cannot identify type for constructor argument "%s"; ' @@ -113,6 +115,7 @@ static function (ReflectionParameter $argument): bool { $argument->getName() )); } + return true; } ); From e71a83a0e8bbae5bdadf89575e4007728181e1f5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:28:26 +0700 Subject: [PATCH 6/7] rollback spacing Signed-off-by: Abdul Malik Ikhsan --- .../TestAsset/ClassWithMixedConstructorParameters.php | 8 ++++++-- test/CommonServiceLocatorBehaviorsTrait.php | 4 ++-- test/Proxy/LazyServiceFactoryTest.php | 2 +- test/ServiceManagerContainerInteropIntegrationTest.php | 2 +- test/ServiceManagerTest.php | 2 -- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php b/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php index 74e0efd9..dcff6578 100644 --- a/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php +++ b/test/AbstractFactory/TestAsset/ClassWithMixedConstructorParameters.php @@ -6,7 +6,11 @@ final class ClassWithMixedConstructorParameters { - public function __construct(public array $config, public SampleInterface $sample, public ValidatorPluginManager $validators, public ?array $options = null) - { + public function __construct( + public array $config, + public SampleInterface $sample, + public ValidatorPluginManager $validators, + public ?array $options = null + ) { } } diff --git a/test/CommonServiceLocatorBehaviorsTrait.php b/test/CommonServiceLocatorBehaviorsTrait.php index 7125ebac..e8a4e276 100644 --- a/test/CommonServiceLocatorBehaviorsTrait.php +++ b/test/CommonServiceLocatorBehaviorsTrait.php @@ -855,7 +855,7 @@ public function methodsAffectedByOverrideSettings(): array 'setFactory', [ 'foo', - static function () : void { + static function (): void { }, ], ], @@ -866,7 +866,7 @@ static function () : void { 'addDelegator', [ 'foo', - static function () : void { + static function (): void { }, ], ], diff --git a/test/Proxy/LazyServiceFactoryTest.php b/test/Proxy/LazyServiceFactoryTest.php index c9499dc0..a8227cb0 100644 --- a/test/Proxy/LazyServiceFactoryTest.php +++ b/test/Proxy/LazyServiceFactoryTest.php @@ -87,7 +87,7 @@ public function testCreates(): void ->expects(self::once()) ->method('createProxy') ->willReturnCallback( - static function ($className, $initializer) use ($expectedService, $proxy): \PHPUnit\Framework\MockObject\MockObject { + static function ($className, $initializer) use ($expectedService, $proxy): MockObject { self::assertEquals('FooClass', $className, 'class name not match'); $wrappedInstance = null; diff --git a/test/ServiceManagerContainerInteropIntegrationTest.php b/test/ServiceManagerContainerInteropIntegrationTest.php index 6714d98a..a4f54ac7 100644 --- a/test/ServiceManagerContainerInteropIntegrationTest.php +++ b/test/ServiceManagerContainerInteropIntegrationTest.php @@ -15,7 +15,7 @@ final class ServiceManagerContainerInteropIntegrationTest extends TestCase { - private \Laminas\ServiceManager\ServiceManager $container; + private ServiceManager $container; protected function setUp(): void { diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index ea15ccbf..d63eb116 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -17,8 +17,6 @@ use Psr\Container\ContainerInterface; use stdClass; -use function get_class; - /** * @see ConfigInterface * From 0c9036e4d78bbbbe9c9c26a7c5fc05e11d77f6f8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 11 Oct 2022 03:47:21 +0700 Subject: [PATCH 7/7] rollback object return type for delegator callback Signed-off-by: Abdul Malik Ikhsan --- src/ServiceManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 6e5c59eb..3947c25e 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -560,7 +560,7 @@ private function getFactory(string $name): callable */ private function createDelegatorFromName(string $name, ?array $options = null) { - $creationCallback = function () use ($name, $options): object { + $creationCallback = function () use ($name, $options) { // Code is inlined for performance reason, instead of abstracting the creation $factory = $this->getFactory($name); return $factory($this->creationContext, $name, $options);