From fe0d82d8d56b7f99cab30acb8777636a31829643 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Thu, 1 Feb 2018 20:50:12 +0100 Subject: [PATCH 1/4] Add php 7.1 type hints --- src/AbstractFactory/ConfigAbstractFactory.php | 5 +- .../ReflectionBasedAbstractFactory.php | 21 ++----- src/AbstractPluginManager.php | 6 +- src/Config.php | 14 ++--- src/ConfigInterface.php | 4 +- ...tainerModificationsNotAllowedException.php | 3 +- src/Exception/CyclicAliasException.php | 19 +++---- src/Exception/InvalidArgumentException.php | 4 +- src/Factory/AbstractFactoryInterface.php | 2 +- src/Factory/DelegatorFactoryInterface.php | 2 +- src/Factory/FactoryInterface.php | 2 +- src/Factory/InvokableFactory.php | 2 +- src/Initializer/InitializerInterface.php | 2 +- src/InitializerInterface.php | 2 +- src/PluginManagerInterface.php | 3 +- src/Proxy/LazyServiceFactory.php | 8 +-- src/ServiceLocatorInterface.php | 2 +- src/ServiceManager.php | 55 +++++++++---------- src/Tool/ConfigDumper.php | 50 +++-------------- src/Tool/ConfigDumperCommand.php | 32 +++-------- src/Tool/FactoryCreator.php | 24 ++------ src/Tool/FactoryCreatorCommand.php | 22 ++------ ...thScalarDependencyDefiningDefaultValue.php | 5 +- test/ConfigTest.php | 5 +- test/Exception/CyclicAliasExceptionTest.php | 10 ++-- test/TestAsset/AbstractFactoryFoo.php | 4 +- test/TestAsset/CallTimesAbstractFactory.php | 4 +- test/TestAsset/FailingAbstractFactory.php | 4 +- ...ailingExceptionWithStringAsCodeFactory.php | 2 +- test/TestAsset/FailingFactory.php | 2 +- test/TestAsset/LenientPluginManager.php | 2 +- .../TestAsset/PassthroughDelegatorFactory.php | 2 +- test/TestAsset/PreDelegator.php | 2 +- test/TestAsset/SampleFactory.php | 2 +- test/TestAsset/SimpleAbstractFactory.php | 4 +- test/TestAsset/SimpleInitializer.php | 2 +- test/TestAsset/V2v3PluginManager.php | 2 +- test/Tool/ConfigDumperTest.php | 14 ----- 38 files changed, 119 insertions(+), 231 deletions(-) diff --git a/src/AbstractFactory/ConfigAbstractFactory.php b/src/AbstractFactory/ConfigAbstractFactory.php index 31f7d453..43c0368d 100644 --- a/src/AbstractFactory/ConfigAbstractFactory.php +++ b/src/AbstractFactory/ConfigAbstractFactory.php @@ -8,6 +8,7 @@ namespace Zend\ServiceManager\AbstractFactory; use ArrayObject; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Factory\AbstractFactoryInterface; @@ -24,7 +25,7 @@ final class ConfigAbstractFactory implements AbstractFactoryInterface * * {@inheritdoc} */ - public function canCreate(\Interop\Container\ContainerInterface $container, $requestedName) + public function canCreate(ContainerInterface $container, string $requestedName): bool { if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) { return false; @@ -38,7 +39,7 @@ public function canCreate(\Interop\Container\ContainerInterface $container, $req /** * {@inheritDoc} */ - public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { if (! $container->has('config')) { throw new ServiceNotCreatedException('Cannot find a config array in the container'); diff --git a/src/AbstractFactory/ReflectionBasedAbstractFactory.php b/src/AbstractFactory/ReflectionBasedAbstractFactory.php index 7d4b4df3..31b165ec 100644 --- a/src/AbstractFactory/ReflectionBasedAbstractFactory.php +++ b/src/AbstractFactory/ReflectionBasedAbstractFactory.php @@ -110,7 +110,7 @@ public function __construct(array $aliases = []) * * @return DispatchableInterface */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { $reflectionClass = new ReflectionClass($requestedName); @@ -136,7 +136,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, $requestedName) + public function canCreate(ContainerInterface $container, string $requestedName): bool { return class_exists($requestedName); } @@ -146,12 +146,8 @@ public function canCreate(ContainerInterface $container, $requestedName) * * Returns a callback for resolving a parameter to a value, but without * allowing mapping array `$config` arguments to the `config` service. - * - * @param ContainerInterface $container - * @param string $requestedName - * @return callable */ - private function resolveParameterWithoutConfigService(ContainerInterface $container, $requestedName) + private function resolveParameterWithoutConfigService(ContainerInterface $container, string $requestedName): callable { /** * @param ReflectionParameter $parameter @@ -169,12 +165,8 @@ private function resolveParameterWithoutConfigService(ContainerInterface $contai * * Unlike resolveParameter(), this version will detect `$config` array * arguments and have them return the 'config' service. - * - * @param ContainerInterface $container - * @param string $requestedName - * @return callable */ - private function resolveParameterWithConfigService(ContainerInterface $container, $requestedName) + private function resolveParameterWithConfigService(ContainerInterface $container, string $requestedName): callable { /** * @param ReflectionParameter $parameter @@ -193,14 +185,11 @@ private function resolveParameterWithConfigService(ContainerInterface $container /** * Logic common to all parameter resolution. * - * @param ReflectionParameter $parameter - * @param ContainerInterface $container - * @param string $requestedName * @return mixed * @throws ServiceNotFoundException If type-hinted parameter cannot be * resolved to a service in the container. */ - private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, $requestedName) + private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, string $requestedName) { if ($parameter->isArray()) { return []; diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index 7a83f4ea..eca83b49 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -107,7 +107,7 @@ public function __construct($configInstanceOrParentLocator = null, array $config * {@inheritDoc} * @throws InvalidServiceException */ - public function configure(array $config) + public function configure(array $config): ServiceManager { if (isset($config['services'])) { foreach ($config['services'] as $service) { @@ -125,7 +125,7 @@ public function configure(array $config) * * {@inheritDoc} */ - public function setService($name, $service) + public function setService(string $name, $service): void { $this->validate($service); parent::setService($name, $service); @@ -165,7 +165,7 @@ public function get($name, array $options = null) /** * {@inheritDoc} */ - public function validate($instance) + public function validate($instance): void { if (method_exists($this, 'validatePlugin')) { trigger_error(sprintf( diff --git a/src/Config.php b/src/Config.php index 6c85fab7..79bae549 100644 --- a/src/Config.php +++ b/src/Config.php @@ -68,7 +68,7 @@ class Config implements ConfigInterface public function __construct(array $config = []) { // Only merge keys we're interested in - foreach (\array_keys($config) as $key) { + foreach (array_keys($config) as $key) { if (! isset($this->allowedKeys[$key])) { unset($config[$key]); } @@ -79,7 +79,7 @@ public function __construct(array $config = []) /** * @inheritdoc */ - public function configureServiceManager(ServiceManager $serviceManager) + public function configureServiceManager(ServiceManager $serviceManager): ServiceManager { return $serviceManager->configure($this->config); } @@ -87,7 +87,7 @@ public function configureServiceManager(ServiceManager $serviceManager) /** * @inheritdoc */ - public function toArray() + public function toArray(): array { return $this->config; } @@ -98,17 +98,17 @@ public function toArray() * * @link https://github.com/zendframework/zend-servicemanager/pull/68 */ - private function merge(array $a, array $b) + private function merge(array $a, array $b): array { foreach ($b as $key => $value) { if ($value instanceof MergeReplaceKeyInterface) { $a[$key] = $value->getData(); - } elseif (isset($a[$key]) || \array_key_exists($key, $a)) { + } elseif (isset($a[$key]) || array_key_exists($key, $a)) { if ($value instanceof MergeRemoveKey) { unset($a[$key]); - } elseif (\is_int($key)) { + } elseif (is_int($key)) { $a[] = $value; - } elseif (\is_array($value) && \is_array($a[$key])) { + } elseif (is_array($value) && is_array($a[$key])) { $a[$key] = $this->merge($a[$key], $value); } else { $a[$key] = $value; diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index 67e1aee6..ed4d8f71 100644 --- a/src/ConfigInterface.php +++ b/src/ConfigInterface.php @@ -19,7 +19,7 @@ interface ConfigInterface * @param ServiceManager $serviceManager * @return ServiceManager */ - public function configureServiceManager(ServiceManager $serviceManager); + public function configureServiceManager(ServiceManager $serviceManager): ServiceManager; /** * Return configuration for a service manager instance as an array. @@ -42,5 +42,5 @@ public function configureServiceManager(ServiceManager $serviceManager); * * @return array */ - public function toArray(); + public function toArray(): array; } diff --git a/src/Exception/ContainerModificationsNotAllowedException.php b/src/Exception/ContainerModificationsNotAllowedException.php index 1d8cc900..6c05c762 100644 --- a/src/Exception/ContainerModificationsNotAllowedException.php +++ b/src/Exception/ContainerModificationsNotAllowedException.php @@ -16,9 +16,8 @@ class ContainerModificationsNotAllowedException extends DomainException implemen { /** * @param string $service Name of service that already exists. - * @return self */ - public static function fromExistingService($service) + public static function fromExistingService(string $service): self { return new self(sprintf( 'The container does not allow replacing or updating a service' diff --git a/src/Exception/CyclicAliasException.php b/src/Exception/CyclicAliasException.php index b6b95c2d..59a5de03 100644 --- a/src/Exception/CyclicAliasException.php +++ b/src/Exception/CyclicAliasException.php @@ -24,7 +24,7 @@ class CyclicAliasException extends InvalidArgumentException * @param string[] $aliases map of referenced services, indexed by alias name (string) * @return self */ - public static function fromCyclicAlias($alias, array $aliases) + public static function fromCyclicAlias(string $alias, array $aliases): self { $cycle = $alias; $cursor = $alias; @@ -44,7 +44,7 @@ public static function fromCyclicAlias($alias, array $aliases) * @param string[] $aliases map of referenced services, indexed by alias name (string) * @return self */ - public static function fromAliasesMap(array $aliases) + public static function fromAliasesMap(array $aliases): self { $detectedCycles = array_filter(array_map( function ($alias) use ($aliases) { @@ -72,10 +72,8 @@ function ($alias) use ($aliases) { * Retrieves the cycle detected for the given $alias, or `null` if no cycle was detected * * @param string[] $aliases - * @param string $alias - * @return array|null */ - private static function getCycleFor(array $aliases, $alias) + private static function getCycleFor(array $aliases, string $alias): ?array { $cycleCandidate = []; $targetName = $alias; @@ -94,9 +92,8 @@ private static function getCycleFor(array $aliases, $alias) /** * @param string[] $aliases - * @return string */ - private static function printReferencesMap(array $aliases) + private static function printReferencesMap(array $aliases): string { $map = []; @@ -109,18 +106,16 @@ private static function printReferencesMap(array $aliases) /** * @param string[][] $detectedCycles - * @return string */ - private static function printCycles(array $detectedCycles) + private static function printCycles(array $detectedCycles): string { return "[\n" . implode("\n", array_map([__CLASS__, 'printCycle'], $detectedCycles)) . "\n]"; } /** * @param string[] $detectedCycle - * @return string */ - private static function printCycle(array $detectedCycle) + private static function printCycle(array $detectedCycle): string { $fullCycle = array_keys($detectedCycle); $fullCycle[] = reset($fullCycle); @@ -140,7 +135,7 @@ function ($cycle) { * @param bool[][] $detectedCycles * @return bool[][] de-duplicated */ - private static function deDuplicateDetectedCycles(array $detectedCycles) + private static function deDuplicateDetectedCycles(array $detectedCycles): array { $detectedCyclesByHash = []; diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 0118e23d..33a79028 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -20,7 +20,7 @@ class InvalidArgumentException extends SplInvalidArgumentException implements Ex * @param mixed $initializer * @return self */ - public static function fromInvalidInitializer($initializer) + public static function fromInvalidInitializer($initializer): self { return new self(sprintf( 'An invalid initializer was registered. Expected a callable or an' @@ -34,7 +34,7 @@ public static function fromInvalidInitializer($initializer) * @param mixed $abstractFactory * @return self */ - public static function fromInvalidAbstractFactory($abstractFactory) + public static function fromInvalidAbstractFactory($abstractFactory): self { return new self(sprintf( 'An invalid abstract factory was registered. Expected an instance of or a valid' diff --git a/src/Factory/AbstractFactoryInterface.php b/src/Factory/AbstractFactoryInterface.php index 147e7dc5..84d4b389 100644 --- a/src/Factory/AbstractFactoryInterface.php +++ b/src/Factory/AbstractFactoryInterface.php @@ -28,5 +28,5 @@ interface AbstractFactoryInterface extends FactoryInterface * @param string $requestedName * @return bool */ - public function canCreate(ContainerInterface $container, $requestedName); + public function canCreate(ContainerInterface $container, string $requestedName): bool; } diff --git a/src/Factory/DelegatorFactoryInterface.php b/src/Factory/DelegatorFactoryInterface.php index 14df71d4..904942a9 100644 --- a/src/Factory/DelegatorFactoryInterface.php +++ b/src/Factory/DelegatorFactoryInterface.php @@ -35,5 +35,5 @@ interface DelegatorFactoryInterface * creating a service. * @throws ContainerException if any other error occurs */ - public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null); + public function __invoke(ContainerInterface $container, string $name, callable $callback, array $options = null); } diff --git a/src/Factory/FactoryInterface.php b/src/Factory/FactoryInterface.php index ffc5a04b..a3452cb5 100644 --- a/src/Factory/FactoryInterface.php +++ b/src/Factory/FactoryInterface.php @@ -34,5 +34,5 @@ interface FactoryInterface * creating a service. * @throws ContainerException if any other error occurs */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null); + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null); } diff --git a/src/Factory/InvokableFactory.php b/src/Factory/InvokableFactory.php index a2927a40..fd8ac18b 100644 --- a/src/Factory/InvokableFactory.php +++ b/src/Factory/InvokableFactory.php @@ -25,7 +25,7 @@ final class InvokableFactory implements FactoryInterface /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { return (null === $options) ? new $requestedName : new $requestedName($options); } diff --git a/src/Initializer/InitializerInterface.php b/src/Initializer/InitializerInterface.php index 80fe3e97..fceea8f3 100644 --- a/src/Initializer/InitializerInterface.php +++ b/src/Initializer/InitializerInterface.php @@ -24,5 +24,5 @@ interface InitializerInterface * @param object $instance * @return void */ - public function __invoke(ContainerInterface $container, $instance); + public function __invoke(ContainerInterface $container, $instance): void; } diff --git a/src/InitializerInterface.php b/src/InitializerInterface.php index 17c6531a..80338e9f 100644 --- a/src/InitializerInterface.php +++ b/src/InitializerInterface.php @@ -32,7 +32,7 @@ interface InitializerInterface extends Initializer\InitializerInterface /** * Initialize * - * @param $instance + * @param mixed $instance * @param ServiceLocatorInterface $serviceLocator * @return mixed */ diff --git a/src/PluginManagerInterface.php b/src/PluginManagerInterface.php index 54a2bc97..473c6d37 100644 --- a/src/PluginManagerInterface.php +++ b/src/PluginManagerInterface.php @@ -21,10 +21,9 @@ interface PluginManagerInterface extends ServiceLocatorInterface * Validate an instance * * @param object $instance - * @return void * @throws InvalidServiceException If created instance does not respect the * constraint on type imposed by the plugin manager * @throws ContainerException if any other error occurs */ - public function validate($instance); + public function validate($instance): void; } diff --git a/src/Proxy/LazyServiceFactory.php b/src/Proxy/LazyServiceFactory.php index 29a3a9cc..13b44731 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -10,9 +10,9 @@ use Interop\Container\ContainerInterface; use ProxyManager\Factory\LazyLoadingValueHolderFactory; use ProxyManager\Proxy\LazyLoadingInterface; +use ProxyManager\Proxy\VirtualProxyInterface; use Zend\ServiceManager\Exception; use Zend\ServiceManager\Factory\DelegatorFactoryInterface; - use function sprintf; /** @@ -24,7 +24,7 @@ final class LazyServiceFactory implements DelegatorFactoryInterface { /** - * @var \ProxyManager\Factory\LazyLoadingValueHolderFactory + * @var LazyLoadingValueHolderFactory */ private $proxyFactory; @@ -47,9 +47,9 @@ public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $ /** * {@inheritDoc} * - * @return \ProxyManager\Proxy\VirtualProxyInterface + * @return VirtualProxyInterface */ - public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null) + public function __invoke(ContainerInterface $container, string $name, callable $callback, array $options = null): VirtualProxyInterface { $initializer = function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback) { $proxy->setProxyInitializer(null); diff --git a/src/ServiceLocatorInterface.php b/src/ServiceLocatorInterface.php index c0f2bf19..bd0ba177 100644 --- a/src/ServiceLocatorInterface.php +++ b/src/ServiceLocatorInterface.php @@ -30,5 +30,5 @@ interface ServiceLocatorInterface extends * to create the instance. * @throws ContainerExceptionInterface if any other error occurs */ - public function build($name, array $options = null); + public function build(string $name, array $options = null); } diff --git a/src/ServiceManager.php b/src/ServiceManager.php index d4301178..88e0646c 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -237,7 +237,7 @@ public function get($name) /** * {@inheritDoc} */ - public function build($name, array $options = null) + public function build(string $name, array $options = null) { // We never cache when using "build". $name = $this->aliases[$name] ?? $name; @@ -247,7 +247,7 @@ public function build($name, array $options = null) /** * {@inheritDoc} */ - public function has($name) + public function has($name): bool { // Check services and factories first to speedup the most common requests. if (isset($this->services[$name]) || isset($this->factories[$name])) { @@ -284,20 +284,16 @@ public function has($name) /** * Indicate whether or not the instance is immutable. - * - * @param bool $flag */ - public function setAllowOverride($flag) + public function setAllowOverride(bool $flag): void { $this->allowOverride = (bool) $flag; } /** * Retrieve the flag indicating immutability status. - * - * @return bool */ - public function getAllowOverride() + public function getAllowOverride(): bool { return $this->allowOverride; } @@ -342,7 +338,7 @@ public function getAllowOverride() * override flag has been toggled off, and a service instance * exists for a given service. */ - public function configure(array $config) + public function configure(array $config): self { // This is a bulk update/initial configuration, // so we check all definitions up front. @@ -413,7 +409,7 @@ public function configure(array $config) * @throws ContainerModificationsNotAllowedException if $alias already * exists as a service and overrides are disallowed. */ - public function setAlias($alias, $target) + public function setAlias(string $alias, string $target): void { if (isset($this->services[$alias]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($alias); @@ -431,7 +427,7 @@ public function setAlias($alias, $target) * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setInvokableClass($name, $class = null) + public function setInvokableClass(string $name, string $class = null): void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -449,7 +445,7 @@ public function setInvokableClass($name, $class = null) * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setFactory($name, $factory) + public function setFactory(string $name, $factory): void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -465,7 +461,7 @@ public function setFactory($name, $factory) * @param null|string $class Class to which to map; if not provided, $name * will be used for the mapping. */ - public function mapLazyService($name, $class = null) + public function mapLazyService(string $name, string $class = null): void { $this->configure(['lazy_services' => ['class_map' => [$name => $class ?: $name]]]); } @@ -476,7 +472,7 @@ public function mapLazyService($name, $class = null) * @param string|Factory\AbstractFactoryInterface $factory Abstract factory * instance or class name. */ - public function addAbstractFactory($factory) + public function addAbstractFactory($factory): void { $this->resolveAbstractFactoryInstance($factory); } @@ -488,7 +484,7 @@ public function addAbstractFactory($factory) * @param string|callable|Factory\DelegatorFactoryInterface $factory Delegator * factory to assign. */ - public function addDelegator($name, $factory) + public function addDelegator(string $name, $factory): void { $this->configure(['delegators' => [$name => [$factory]]]); } @@ -498,7 +494,7 @@ public function addDelegator($name, $factory) * * @param string|callable|Initializer\InitializerInterface $initializer */ - public function addInitializer($initializer) + public function addInitializer($initializer): void { $this->configure(['initializers' => [$initializer]]); } @@ -507,11 +503,11 @@ public function addInitializer($initializer) * Map a service. * * @param string $name Service name - * @param array|object $service + * @param mixed $service * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setService($name, $service) + public function setService(string $name, $service): void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -527,7 +523,7 @@ public function setService($name, $service) * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setShared($name, $flag) + public function setShared(string $name, bool $flag): void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -541,7 +537,7 @@ public function setShared($name, $flag) * * @param string[]|Initializer\InitializerInterface[]|callable[] $initializers */ - private function resolveInitializers(array $initializers) + private function resolveInitializers(array $initializers): void { foreach ($initializers as $initializer) { if (is_string($initializer) && class_exists($initializer)) { @@ -564,7 +560,7 @@ private function resolveInitializers(array $initializers) * @return callable * @throws ServiceNotFoundException */ - private function getFactory($name) + private function getFactory(string $name): callable { $factory = $this->factories[$name] ?? null; @@ -600,7 +596,7 @@ private function getFactory($name) * @param null|array $options * @return object */ - private function createDelegatorFromName($name, array $options = null) + private function createDelegatorFromName(string $name, array $options = null) { $creationCallback = function () use ($name, $options) { // Code is inlined for performance reason, instead of abstracting the creation @@ -660,7 +656,7 @@ private function createDelegatorFromName($name, array $options = null) * creating a service. * @throws ContainerException if any other error occurs */ - private function doCreate($resolvedName, array $options = null) + private function doCreate(string $resolvedName, array $options = null) { try { if (! isset($this->delegators[$resolvedName])) { @@ -697,7 +693,7 @@ private function doCreate($resolvedName, array $options = null) * @throws ServiceNotCreatedException when the lazy service class_map * configuration is missing */ - private function createLazyServiceDelegatorFactory() + private function createLazyServiceDelegatorFactory(): Proxy\LazyServiceFactory { if ($this->lazyServicesDelegator) { return $this->lazyServicesDelegator; @@ -744,7 +740,7 @@ private function createLazyServiceDelegatorFactory() * * @param array $invokables */ - private function createAliasesAndFactoriesForInvokables(array $invokables) + private function createAliasesAndFactoriesForInvokables(array $invokables): void { foreach ($invokables as $name => $class) { $this->factories[$class] = Factory\InvokableFactory::class; @@ -768,7 +764,7 @@ private function createAliasesAndFactoriesForInvokables(array $invokables) * @throws ContainerModificationsNotAllowedException if any * service key is invalid. */ - private function validateServiceNames(array $config) + private function validateServiceNames(array $config): void { if ($this->allowOverride || ! $this->configured) { return; @@ -843,7 +839,7 @@ private function validateServiceNames(array $config) * @param string $alias * @param string $target */ - private function mapAliasToTarget($alias, $target) + private function mapAliasToTarget(string $alias, string $target): void { // $target is either an alias or something else // if it is an alias, resolve it @@ -882,7 +878,7 @@ private function mapAliasToTarget($alias, $target) * @see mapAliasToTarget above * */ - private function mapAliasesToTargets() + private function mapAliasesToTargets(): void { $tagged = []; foreach ($this->aliases as $alias => $target) { @@ -926,9 +922,8 @@ private function mapAliasesToTargets() * Instantiate abstract factories in order to avoid checks during service construction. * * @param string|Factory\AbstractFactoryInterface $abstractFactories - * @return void */ - private function resolveAbstractFactoryInstance($abstractFactory) + private function resolveAbstractFactoryInstance($abstractFactory): void { if (is_string($abstractFactory) && class_exists($abstractFactory)) { // Cached string factory name diff --git a/src/Tool/ConfigDumper.php b/src/Tool/ConfigDumper.php index 6a718357..a62e8b74 100644 --- a/src/Tool/ConfigDumper.php +++ b/src/Tool/ConfigDumper.php @@ -56,13 +56,9 @@ public function __construct(ContainerInterface $container = null) } /** - * @param array $config - * @param string $className - * @param bool $ignoreUnresolved - * @return array * @throws InvalidArgumentException for invalid $className */ - public function createDependencyConfig(array $config, $className, $ignoreUnresolved = false) + public function createDependencyConfig(array $config, string $className, bool $ignoreUnresolved = false): array { $this->validateClassName($className); @@ -125,35 +121,24 @@ function (ReflectionParameter $argument) { * @throws InvalidArgumentException if class name is not a string or does * not exist. */ - private function validateClassName($className) + private function validateClassName(string $className): void { - if (! is_string($className)) { - throw new InvalidArgumentException('Class name must be a string, ' . gettype($className) . ' given'); - } - if (! class_exists($className) && ! interface_exists($className)) { throw new InvalidArgumentException('Cannot find class or interface with name ' . $className); } } - /** - * @param array $config - * @param string $className - * @return array - */ - private function createInvokable(array $config, $className) + private function createInvokable(array $config, string $className): array { $config[ConfigAbstractFactory::class][$className] = []; return $config; } /** - * @param array $config - * @return array * @throws InvalidArgumentException if ConfigAbstractFactory configuration * value is not an array. */ - public function createFactoryMappingsFromConfig(array $config) + public function createFactoryMappingsFromConfig(array $config): array { if (! array_key_exists(ConfigAbstractFactory::class, $config)) { return $config; @@ -173,12 +158,7 @@ public function createFactoryMappingsFromConfig(array $config) return $config; } - /** - * @param array $config - * @param string $className - * @return array - */ - public function createFactoryMappings(array $config, $className) + public function createFactoryMappings(array $config, string $className): array { $this->validateClassName($className); @@ -193,11 +173,7 @@ public function createFactoryMappings(array $config, $className) return $config; } - /** - * @param array $config - * @return string - */ - public function dumpConfigFile(array $config) + public function dumpConfigFile(array $config): string { $prepared = $this->prepareConfig($config); return sprintf( @@ -208,12 +184,7 @@ public function dumpConfigFile(array $config) ); } - /** - * @param array|Traversable $config - * @param int $indentLevel - * @return string - */ - private function prepareConfig($config, $indentLevel = 1) + private function prepareConfig(iterable $config, int $indentLevel = 1): string { $indent = str_repeat(' ', $indentLevel * 4); $entries = []; @@ -238,9 +209,8 @@ private function prepareConfig($config, $indentLevel = 1) /** * @param string|int|null $key - * @return null|string */ - private function createConfigKey($key) + private function createConfigKey($key): ?string { if (is_string($key) && class_exists($key)) { return sprintf('\\%s::class', $key); @@ -255,10 +225,8 @@ private function createConfigKey($key) /** * @param mixed $value - * @param int $indentLevel - * @return string */ - private function createConfigValue($value, $indentLevel) + private function createConfigValue($value, int $indentLevel): string { 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 60f44847..f14ce1e0 100644 --- a/src/Tool/ConfigDumperCommand.php +++ b/src/Tool/ConfigDumperCommand.php @@ -7,9 +7,9 @@ namespace Zend\ServiceManager\Tool; +use stdClass; use Zend\ServiceManager\Exception; use Zend\Stdlib\ConsoleHelper; - use function array_shift; use function class_exists; use function dirname; @@ -60,10 +60,7 @@ class ConfigDumperCommand */ private $scriptName; - /** - * @param string $scriptName - */ - public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ConsoleHelper $helper = null) + public function __construct(string $scriptName = self::DEFAULT_SCRIPT_NAME, ConsoleHelper $helper = null) { $this->scriptName = $scriptName; $this->helper = $helper ?: new ConsoleHelper(); @@ -73,7 +70,7 @@ public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ConsoleHelp * @param array $args Argument list, minus script name * @return int Exit status */ - public function __invoke(array $args) + public function __invoke(array $args): int { $arguments = $this->parseArgs($args); @@ -117,11 +114,7 @@ public function __invoke(array $args) return 0; } - /** - * @param array $args - * @return \stdClass - */ - private function parseArgs(array $args) + private function parseArgs(array $args): stdClass { if (! $args) { return $this->createHelpArgument(); @@ -186,7 +179,7 @@ private function parseArgs(array $args) * @param resource $resource Defaults to STDOUT * @return void */ - private function help($resource = STDOUT) + private function help($resource = STDOUT): void { $this->helper->writeLine(sprintf( self::HELP_TEMPLATE, @@ -201,9 +194,9 @@ private function help($resource = STDOUT) * @param array $config Parsed configuration. * @param string $class Name of class to reflect. * @param bool $ignoreUnresolved If to ignore classes with unresolved direct dependencies. - * @return \stdClass + * @return stdClass */ - private function createArguments($command, $configFile, $config, $class, $ignoreUnresolved) + private function createArguments(string $command, string $configFile, array $config, string $class, bool $ignoreUnresolved): stdClass { return (object) [ 'command' => $command, @@ -214,11 +207,7 @@ private function createArguments($command, $configFile, $config, $class, $ignore ]; } - /** - * @param string $message - * @return \stdClass - */ - private function createErrorArgument($message) + private function createErrorArgument(string $message): stdClass { return (object) [ 'command' => self::COMMAND_ERROR, @@ -226,10 +215,7 @@ private function createErrorArgument($message) ]; } - /** - * @return \stdClass - */ - private function createHelpArgument() + private function createHelpArgument(): stdClass { return (object) [ 'command' => self::COMMAND_HELP, diff --git a/src/Tool/FactoryCreator.php b/src/Tool/FactoryCreator.php index 14605050..429543b0 100644 --- a/src/Tool/FactoryCreator.php +++ b/src/Tool/FactoryCreator.php @@ -49,11 +49,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o EOT; - /** - * @param string $className - * @return string - */ - public function createFactory($className) + public function createFactory(string $className): string { $class = $this->getClassName($className); @@ -68,21 +64,13 @@ public function createFactory($className) ); } - /** - * @param $className - * @return string - */ - private function getClassName($className) + private function getClassName(string $className): string { $class = substr($className, strrpos($className, '\\') + 1); return $class; } - /** - * @param string $className - * @return array - */ - private function getConstructorParameters($className) + private function getConstructorParameters(string $className): array { $reflectionClass = new ReflectionClass($className); @@ -124,11 +112,7 @@ function (ReflectionParameter $argument) { }, $constructorParameters); } - /** - * @param string $className - * @return string - */ - private function createArgumentString($className) + private function createArgumentString(string $className): string { $arguments = array_map(function ($dependency) { return sprintf('$container->get(\\%s::class)', $dependency); diff --git a/src/Tool/FactoryCreatorCommand.php b/src/Tool/FactoryCreatorCommand.php index 3bcffb85..b55a7470 100644 --- a/src/Tool/FactoryCreatorCommand.php +++ b/src/Tool/FactoryCreatorCommand.php @@ -7,9 +7,9 @@ namespace Zend\ServiceManager\Tool; +use stdClass; use Zend\ServiceManager\Exception; use Zend\Stdlib\ConsoleHelper; - use function array_shift; use function class_exists; use function in_array; @@ -48,11 +48,7 @@ class FactoryCreatorCommand */ private $scriptName; - /** - * @param string $scriptName - * @param ConsoleHelper $helper - */ - public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ConsoleHelper $helper = null) + public function __construct(string $scriptName = self::DEFAULT_SCRIPT_NAME, ConsoleHelper $helper = null) { $this->scriptName = $scriptName; $this->helper = $helper ?: new ConsoleHelper(); @@ -62,7 +58,7 @@ public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ConsoleHelp * @param array $args Argument list, minus script name * @return int Exit status */ - public function __invoke(array $args) + public function __invoke(array $args): int { $arguments = $this->parseArgs($args); @@ -97,11 +93,7 @@ public function __invoke(array $args) return 0; } - /** - * @param array $args - * @return \stdClass - */ - private function parseArgs(array $args) + private function parseArgs(array $args): stdClass { if (! $args) { return $this->createArguments(self::COMMAND_HELP); @@ -129,7 +121,7 @@ private function parseArgs(array $args) * @param resource $resource Defaults to STDOUT * @return void */ - private function help($resource = STDOUT) + private function help($resource = STDOUT): void { $this->helper->writeLine(sprintf( self::HELP_TEMPLATE, @@ -138,12 +130,10 @@ private function help($resource = STDOUT) } /** - * @param string $command * @param string|null $class Name of class to reflect. * @param string|null $error Error message, if any. - * @return \stdClass */ - private function createArguments($command, $class = null, $error = null) + private function createArguments(string $command, string $class = null, string $error = null): stdClass { return (object) [ 'command' => $command, diff --git a/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php b/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php index 39294e99..fca3e827 100644 --- a/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php +++ b/test/AbstractFactory/TestAsset/ClassWithScalarDependencyDefiningDefaultValue.php @@ -11,10 +11,7 @@ class ClassWithScalarDependencyDefiningDefaultValue { public $foo; - /** - * @param string $foo - */ - public function __construct($foo = 'bar') + public function __construct(string $foo = 'bar') { $this->foo = $foo; } diff --git a/test/ConfigTest.php b/test/ConfigTest.php index b2257d01..ffae666e 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -104,10 +104,11 @@ public function testPassesKnownServiceConfigKeysToServiceManagerWithConfigMethod ]; $services = $this->prophesize(ServiceManager::class); - $services->configure($expected)->willReturn('CALLED'); + $object = $services->reveal(); + $services->configure($expected)->willReturn($object); $configuration = new Config($config); - self::assertEquals('CALLED', $configuration->configureServiceManager($services->reveal())); + self::assertEquals($object, $configuration->configureServiceManager($object)); return [ 'array' => $expected, diff --git a/test/Exception/CyclicAliasExceptionTest.php b/test/Exception/CyclicAliasExceptionTest.php index bedf1352..483aee26 100644 --- a/test/Exception/CyclicAliasExceptionTest.php +++ b/test/Exception/CyclicAliasExceptionTest.php @@ -20,10 +20,8 @@ class CyclicAliasExceptionTest extends TestCase * @dataProvider cyclicAliasProvider * * @param string $alias, conflicting alias key - * @param string[] $aliases - * @param string $expectedMessage */ - public function testFromCyclicAlias($alias, array $aliases, $expectedMessage) + public function testFromCyclicAlias(string $alias, array $aliases, string $expectedMessage) { $exception = CyclicAliasException::fromCyclicAlias($alias, $aliases); @@ -36,7 +34,7 @@ public function testFromCyclicAlias($alias, array $aliases, $expectedMessage) * * @return string[][]|string[][][] */ - public function cyclicAliasProvider() + public function cyclicAliasProvider(): array { return [ [ @@ -113,7 +111,7 @@ public function cyclicAliasProvider() * @param string[] $aliases * @param string $expectedMessage */ - public function testFromAliasesMap(array $aliases, $expectedMessage) + public function testFromAliasesMap(array $aliases, string $expectedMessage) { $exception = CyclicAliasException::fromAliasesMap($aliases); @@ -124,7 +122,7 @@ public function testFromAliasesMap(array $aliases, $expectedMessage) /** * @return string[][]|string[][][] */ - public function aliasesProvider() + public function aliasesProvider(): array { return [ 'empty set' => [ diff --git a/test/TestAsset/AbstractFactoryFoo.php b/test/TestAsset/AbstractFactoryFoo.php index 4b991515..b2859f2c 100644 --- a/test/TestAsset/AbstractFactoryFoo.php +++ b/test/TestAsset/AbstractFactoryFoo.php @@ -12,7 +12,7 @@ class AbstractFactoryFoo implements AbstractFactoryInterface { - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { if ($requestedName === 'foo') { return new Foo($options); @@ -20,7 +20,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return false; } - public function canCreate(ContainerInterface $container, $requestedName) + public function canCreate(ContainerInterface $container, string $requestedName): bool { return ($requestedName === 'foo'); } diff --git a/test/TestAsset/CallTimesAbstractFactory.php b/test/TestAsset/CallTimesAbstractFactory.php index 2a09bdf1..a10bdb95 100644 --- a/test/TestAsset/CallTimesAbstractFactory.php +++ b/test/TestAsset/CallTimesAbstractFactory.php @@ -17,7 +17,7 @@ class CallTimesAbstractFactory implements AbstractFactoryInterface /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, $name) + public function canCreate(ContainerInterface $container, string $name): bool { self::$callTimes++; @@ -27,7 +27,7 @@ public function canCreate(ContainerInterface $container, $name) /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $className, array $options = null) + public function __invoke(ContainerInterface $container, string $className, array $options = null) { } diff --git a/test/TestAsset/FailingAbstractFactory.php b/test/TestAsset/FailingAbstractFactory.php index 744e4783..f2776e0a 100644 --- a/test/TestAsset/FailingAbstractFactory.php +++ b/test/TestAsset/FailingAbstractFactory.php @@ -15,7 +15,7 @@ class FailingAbstractFactory implements AbstractFactoryInterface /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, $name) + public function canCreate(ContainerInterface $container, string $name): bool { return false; } @@ -23,7 +23,7 @@ public function canCreate(ContainerInterface $container, $name) /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $className, array $options = null) + public function __invoke(ContainerInterface $container, string $className, array $options = null) { } } diff --git a/test/TestAsset/FailingExceptionWithStringAsCodeFactory.php b/test/TestAsset/FailingExceptionWithStringAsCodeFactory.php index 126d06e6..14020b7c 100644 --- a/test/TestAsset/FailingExceptionWithStringAsCodeFactory.php +++ b/test/TestAsset/FailingExceptionWithStringAsCodeFactory.php @@ -15,7 +15,7 @@ class FailingExceptionWithStringAsCodeFactory implements FactoryInterface /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { throw new ExceptionWithStringAsCode('There is an error'); } diff --git a/test/TestAsset/FailingFactory.php b/test/TestAsset/FailingFactory.php index 56d8f07e..d676ec50 100644 --- a/test/TestAsset/FailingFactory.php +++ b/test/TestAsset/FailingFactory.php @@ -15,7 +15,7 @@ class FailingFactory implements FactoryInterface /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { throw new \RuntimeException('There is an error'); } diff --git a/test/TestAsset/LenientPluginManager.php b/test/TestAsset/LenientPluginManager.php index a98b9f2c..5160db2d 100644 --- a/test/TestAsset/LenientPluginManager.php +++ b/test/TestAsset/LenientPluginManager.php @@ -14,7 +14,7 @@ class LenientPluginManager extends AbstractPluginManager /** * Allow anything to be considered valid. */ - public function validate($instance) + public function validate($instance): void { return; } diff --git a/test/TestAsset/PassthroughDelegatorFactory.php b/test/TestAsset/PassthroughDelegatorFactory.php index 311732e5..b4f2834e 100644 --- a/test/TestAsset/PassthroughDelegatorFactory.php +++ b/test/TestAsset/PassthroughDelegatorFactory.php @@ -16,7 +16,7 @@ class PassthroughDelegatorFactory implements DelegatorFactoryInterface * {@inheritDoc} * @see \Zend\ServiceManager\Factory\DelegatorFactoryInterface::__invoke() */ - public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null) + public function __invoke(ContainerInterface $container, string $name, callable $callback, array $options = null) { return call_user_func($callback); } diff --git a/test/TestAsset/PreDelegator.php b/test/TestAsset/PreDelegator.php index 7919aebd..f1b72b95 100644 --- a/test/TestAsset/PreDelegator.php +++ b/test/TestAsset/PreDelegator.php @@ -15,7 +15,7 @@ class PreDelegator implements DelegatorFactoryInterface /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null) + public function __invoke(ContainerInterface $container, string $name, callable $callback, array $options = null) { if (! $container->has('config')) { return $callback(); diff --git a/test/TestAsset/SampleFactory.php b/test/TestAsset/SampleFactory.php index ce095894..518e8312 100644 --- a/test/TestAsset/SampleFactory.php +++ b/test/TestAsset/SampleFactory.php @@ -13,7 +13,7 @@ class SampleFactory implements FactoryInterface { - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { return new InvokableObject(); } diff --git a/test/TestAsset/SimpleAbstractFactory.php b/test/TestAsset/SimpleAbstractFactory.php index ebabffec..a4dbbbb9 100644 --- a/test/TestAsset/SimpleAbstractFactory.php +++ b/test/TestAsset/SimpleAbstractFactory.php @@ -15,7 +15,7 @@ class SimpleAbstractFactory implements AbstractFactoryInterface /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, $name) + public function canCreate(ContainerInterface $container, string $name): bool { return true; } @@ -23,7 +23,7 @@ public function canCreate(ContainerInterface $container, $name) /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $className, array $options = null) + public function __invoke(ContainerInterface $container, string $className, array $options = null) { if (empty($options)) { return new $className(); diff --git a/test/TestAsset/SimpleInitializer.php b/test/TestAsset/SimpleInitializer.php index d29c22b0..13eb9053 100644 --- a/test/TestAsset/SimpleInitializer.php +++ b/test/TestAsset/SimpleInitializer.php @@ -16,7 +16,7 @@ class SimpleInitializer implements InitializerInterface /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $instance) + public function __invoke(ContainerInterface $container, $instance): void { if (! $instance instanceof stdClass) { return; diff --git a/test/TestAsset/V2v3PluginManager.php b/test/TestAsset/V2v3PluginManager.php index 8e612b79..6146647d 100644 --- a/test/TestAsset/V2v3PluginManager.php +++ b/test/TestAsset/V2v3PluginManager.php @@ -33,7 +33,7 @@ class V2v3PluginManager extends AbstractPluginManager protected $sharedByDefault = false; - public function validate($plugin) + public function validate($plugin): void { if ($plugin instanceof $this->instanceOf) { return; diff --git a/test/Tool/ConfigDumperTest.php b/test/Tool/ConfigDumperTest.php index 325ccdde..4cf7c337 100644 --- a/test/Tool/ConfigDumperTest.php +++ b/test/Tool/ConfigDumperTest.php @@ -39,13 +39,6 @@ public function setUp() $this->dumper = new ConfigDumper(); } - public function testCreateDependencyConfigExceptsIfClassNameIsNotString() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Class name must be a string, integer given'); - $this->dumper->createDependencyConfig([], 42); - } - public function testCreateDependencyConfigExceptsIfClassDoesNotExist() { $className = 'Dirk\Gentley\Holistic\Detective\Agency'; @@ -212,13 +205,6 @@ public function testCreateDependencyConfigWorksWithMultipleDependenciesOfSameTyp self::assertEquals($expectedConfig, $this->dumper->createDependencyConfig([], DoubleDependencyObject::class)); } - public function testCreateFactoryMappingsExceptsIfClassNameIsNotString() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Class name must be a string, integer given'); - $this->dumper->createFactoryMappings([], 42); - } - public function testCreateFactoryMappingsExceptsIfClassDoesNotExist() { $className = 'Dirk\Gentley\Holistic\Detective\Agency'; From de15b137a06acaed9d81240998b0dc0c05bb75db Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Mon, 19 Mar 2018 23:28:39 +0100 Subject: [PATCH 2/4] Fix bench code --- benchmarks/BenchAsset/AbstractFactoryFoo.php | 4 ++-- benchmarks/BenchAsset/FactoryFoo.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/BenchAsset/AbstractFactoryFoo.php b/benchmarks/BenchAsset/AbstractFactoryFoo.php index 4d61801f..c3039593 100644 --- a/benchmarks/BenchAsset/AbstractFactoryFoo.php +++ b/benchmarks/BenchAsset/AbstractFactoryFoo.php @@ -12,7 +12,7 @@ class AbstractFactoryFoo implements AbstractFactoryInterface { - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { if ($requestedName === 'foo') { return new Foo($options); @@ -20,7 +20,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return false; } - public function canCreate(ContainerInterface $container, $requestedName) + public function canCreate(ContainerInterface $container, string $requestedName): bool { return ($requestedName === 'foo'); } diff --git a/benchmarks/BenchAsset/FactoryFoo.php b/benchmarks/BenchAsset/FactoryFoo.php index 474ea283..8fa9b3c3 100644 --- a/benchmarks/BenchAsset/FactoryFoo.php +++ b/benchmarks/BenchAsset/FactoryFoo.php @@ -12,7 +12,7 @@ class FactoryFoo implements FactoryInterface { - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, string $requestedName, array $options = null) { return new Foo($options); } From 22f328918927a12186c94bde14808864ce825e1b Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Mon, 19 Mar 2018 23:34:38 +0100 Subject: [PATCH 3/4] CS fixes --- .../ReflectionBasedAbstractFactory.php | 13 +++++++++---- src/Proxy/LazyServiceFactory.php | 8 ++++++-- src/Tool/ConfigDumperCommand.php | 9 +++++++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/AbstractFactory/ReflectionBasedAbstractFactory.php b/src/AbstractFactory/ReflectionBasedAbstractFactory.php index 0a35557f..0fd323ef 100644 --- a/src/AbstractFactory/ReflectionBasedAbstractFactory.php +++ b/src/AbstractFactory/ReflectionBasedAbstractFactory.php @@ -147,8 +147,10 @@ public function canCreate(ContainerInterface $container, string $requestedName): * Returns a callback for resolving a parameter to a value, but without * allowing mapping array `$config` arguments to the `config` service. */ - private function resolveParameterWithoutConfigService(ContainerInterface $container, string $requestedName): callable - { + private function resolveParameterWithoutConfigService( + ContainerInterface $container, + string $requestedName + ): callable { /** * @param ReflectionParameter $parameter * @return mixed @@ -189,8 +191,11 @@ private function resolveParameterWithConfigService(ContainerInterface $container * @throws ServiceNotFoundException If type-hinted parameter cannot be * resolved to a service in the container. */ - private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, string $requestedName) - { + private function resolveParameter( + ReflectionParameter $parameter, + ContainerInterface $container, + string $requestedName + ) { if ($parameter->isArray()) { return []; } diff --git a/src/Proxy/LazyServiceFactory.php b/src/Proxy/LazyServiceFactory.php index 5f5a6d02..066c77f4 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -49,8 +49,12 @@ public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $ * * @return VirtualProxyInterface */ - public function __invoke(ContainerInterface $container, string $name, callable $callback, array $options = null): VirtualProxyInterface - { + public function __invoke( + ContainerInterface $container, + string $name, + callable $callback, + array $options = null + ): VirtualProxyInterface { $initializer = function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback) { $proxy->setProxyInitializer(null); $wrappedInstance = $callback(); diff --git a/src/Tool/ConfigDumperCommand.php b/src/Tool/ConfigDumperCommand.php index f14ce1e0..0cec78e6 100644 --- a/src/Tool/ConfigDumperCommand.php +++ b/src/Tool/ConfigDumperCommand.php @@ -196,8 +196,13 @@ private function help($resource = STDOUT): void * @param bool $ignoreUnresolved If to ignore classes with unresolved direct dependencies. * @return stdClass */ - private function createArguments(string $command, string $configFile, array $config, string $class, bool $ignoreUnresolved): stdClass - { + private function createArguments( + string $command, + string $configFile, + array $config, + string $class, + bool $ignoreUnresolved + ): stdClass { return (object) [ 'command' => $command, 'configFile' => $configFile, From 0d4633dc6a433885100787ae7bc70dfb3c870445 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 20 Mar 2018 21:01:57 +0100 Subject: [PATCH 4/4] Improve coding standard --- benchmarks/BenchAsset/AbstractFactoryFoo.php | 2 +- src/AbstractFactory/ConfigAbstractFactory.php | 2 +- .../ReflectionBasedAbstractFactory.php | 6 +-- src/AbstractPluginManager.php | 6 +-- src/Config.php | 6 +-- src/ConfigInterface.php | 4 +- ...tainerModificationsNotAllowedException.php | 2 +- src/Exception/CyclicAliasException.php | 14 +++---- src/Exception/InvalidArgumentException.php | 4 +- src/Factory/AbstractFactoryInterface.php | 2 +- src/Initializer/InitializerInterface.php | 2 +- src/PluginManagerInterface.php | 2 +- src/Proxy/LazyServiceFactory.php | 2 +- src/ServiceManager.php | 42 +++++++++---------- src/Tool/ConfigDumper.php | 18 ++++---- src/Tool/ConfigDumperCommand.php | 12 +++--- src/Tool/FactoryCreator.php | 8 ++-- src/Tool/FactoryCreatorCommand.php | 8 ++-- test/Exception/CyclicAliasExceptionTest.php | 4 +- test/TestAsset/AbstractFactoryFoo.php | 2 +- test/TestAsset/CallTimesAbstractFactory.php | 2 +- test/TestAsset/FailingAbstractFactory.php | 2 +- test/TestAsset/LenientPluginManager.php | 2 +- test/TestAsset/SimpleAbstractFactory.php | 2 +- test/TestAsset/SimpleInitializer.php | 2 +- test/TestAsset/V2v3PluginManager.php | 2 +- 26 files changed, 80 insertions(+), 80 deletions(-) diff --git a/benchmarks/BenchAsset/AbstractFactoryFoo.php b/benchmarks/BenchAsset/AbstractFactoryFoo.php index c3039593..ddbe7d75 100644 --- a/benchmarks/BenchAsset/AbstractFactoryFoo.php +++ b/benchmarks/BenchAsset/AbstractFactoryFoo.php @@ -20,7 +20,7 @@ public function __invoke(ContainerInterface $container, string $requestedName, a return false; } - public function canCreate(ContainerInterface $container, string $requestedName): bool + public function canCreate(ContainerInterface $container, string $requestedName) : bool { return ($requestedName === 'foo'); } diff --git a/src/AbstractFactory/ConfigAbstractFactory.php b/src/AbstractFactory/ConfigAbstractFactory.php index a4c0ed16..a2a7dc29 100644 --- a/src/AbstractFactory/ConfigAbstractFactory.php +++ b/src/AbstractFactory/ConfigAbstractFactory.php @@ -25,7 +25,7 @@ final class ConfigAbstractFactory implements AbstractFactoryInterface * * {@inheritdoc} */ - public function canCreate(ContainerInterface $container, string $requestedName): bool + public function canCreate(ContainerInterface $container, string $requestedName) : bool { if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) { return false; diff --git a/src/AbstractFactory/ReflectionBasedAbstractFactory.php b/src/AbstractFactory/ReflectionBasedAbstractFactory.php index 0fd323ef..6fb64b08 100644 --- a/src/AbstractFactory/ReflectionBasedAbstractFactory.php +++ b/src/AbstractFactory/ReflectionBasedAbstractFactory.php @@ -136,7 +136,7 @@ public function __invoke(ContainerInterface $container, string $requestedName, a /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, string $requestedName): bool + public function canCreate(ContainerInterface $container, string $requestedName) : bool { return class_exists($requestedName); } @@ -150,7 +150,7 @@ public function canCreate(ContainerInterface $container, string $requestedName): private function resolveParameterWithoutConfigService( ContainerInterface $container, string $requestedName - ): callable { + ) : callable { /** * @param ReflectionParameter $parameter * @return mixed @@ -168,7 +168,7 @@ private function resolveParameterWithoutConfigService( * Unlike resolveParameter(), this version will detect `$config` array * arguments and have them return the 'config' service. */ - private function resolveParameterWithConfigService(ContainerInterface $container, string $requestedName): callable + private function resolveParameterWithConfigService(ContainerInterface $container, string $requestedName) : callable { /** * @param ReflectionParameter $parameter diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index a6c1b09e..e62c0d05 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -107,7 +107,7 @@ public function __construct($configInstanceOrParentLocator = null, array $config * {@inheritDoc} * @throws InvalidServiceException */ - public function configure(array $config): ServiceManager + public function configure(array $config) : ServiceManager { if (isset($config['services'])) { foreach ($config['services'] as $service) { @@ -125,7 +125,7 @@ public function configure(array $config): ServiceManager * * {@inheritDoc} */ - public function setService(string $name, $service): void + public function setService(string $name, $service) : void { $this->validate($service); parent::setService($name, $service); @@ -165,7 +165,7 @@ public function get($name, array $options = null) /** * {@inheritDoc} */ - public function validate($instance): void + public function validate($instance) : void { if (method_exists($this, 'validatePlugin')) { trigger_error(sprintf( diff --git a/src/Config.php b/src/Config.php index 79bae549..ca51ebdf 100644 --- a/src/Config.php +++ b/src/Config.php @@ -79,7 +79,7 @@ public function __construct(array $config = []) /** * @inheritdoc */ - public function configureServiceManager(ServiceManager $serviceManager): ServiceManager + public function configureServiceManager(ServiceManager $serviceManager) : ServiceManager { return $serviceManager->configure($this->config); } @@ -87,7 +87,7 @@ public function configureServiceManager(ServiceManager $serviceManager): Service /** * @inheritdoc */ - public function toArray(): array + public function toArray() : array { return $this->config; } @@ -98,7 +98,7 @@ public function toArray(): array * * @link https://github.com/zendframework/zend-servicemanager/pull/68 */ - private function merge(array $a, array $b): array + private function merge(array $a, array $b) : array { foreach ($b as $key => $value) { if ($value instanceof MergeReplaceKeyInterface) { diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index ed4d8f71..7b9984bd 100644 --- a/src/ConfigInterface.php +++ b/src/ConfigInterface.php @@ -19,7 +19,7 @@ interface ConfigInterface * @param ServiceManager $serviceManager * @return ServiceManager */ - public function configureServiceManager(ServiceManager $serviceManager): ServiceManager; + public function configureServiceManager(ServiceManager $serviceManager) : ServiceManager; /** * Return configuration for a service manager instance as an array. @@ -42,5 +42,5 @@ public function configureServiceManager(ServiceManager $serviceManager): Service * * @return array */ - public function toArray(): array; + public function toArray() : array; } diff --git a/src/Exception/ContainerModificationsNotAllowedException.php b/src/Exception/ContainerModificationsNotAllowedException.php index 9af360cd..4c2f16f4 100644 --- a/src/Exception/ContainerModificationsNotAllowedException.php +++ b/src/Exception/ContainerModificationsNotAllowedException.php @@ -19,7 +19,7 @@ class ContainerModificationsNotAllowedException extends DomainException implemen /** * @param string $service Name of service that already exists. */ - public static function fromExistingService(string $service): self + public static function fromExistingService(string $service) : self { return new self(sprintf( 'The container does not allow replacing or updating a service' diff --git a/src/Exception/CyclicAliasException.php b/src/Exception/CyclicAliasException.php index 59a5de03..7556dbaa 100644 --- a/src/Exception/CyclicAliasException.php +++ b/src/Exception/CyclicAliasException.php @@ -24,7 +24,7 @@ class CyclicAliasException extends InvalidArgumentException * @param string[] $aliases map of referenced services, indexed by alias name (string) * @return self */ - public static function fromCyclicAlias(string $alias, array $aliases): self + public static function fromCyclicAlias(string $alias, array $aliases) : self { $cycle = $alias; $cursor = $alias; @@ -44,7 +44,7 @@ public static function fromCyclicAlias(string $alias, array $aliases): self * @param string[] $aliases map of referenced services, indexed by alias name (string) * @return self */ - public static function fromAliasesMap(array $aliases): self + public static function fromAliasesMap(array $aliases) : self { $detectedCycles = array_filter(array_map( function ($alias) use ($aliases) { @@ -73,7 +73,7 @@ function ($alias) use ($aliases) { * * @param string[] $aliases */ - private static function getCycleFor(array $aliases, string $alias): ?array + private static function getCycleFor(array $aliases, string $alias) : ?array { $cycleCandidate = []; $targetName = $alias; @@ -93,7 +93,7 @@ private static function getCycleFor(array $aliases, string $alias): ?array /** * @param string[] $aliases */ - private static function printReferencesMap(array $aliases): string + private static function printReferencesMap(array $aliases) : string { $map = []; @@ -107,7 +107,7 @@ private static function printReferencesMap(array $aliases): string /** * @param string[][] $detectedCycles */ - private static function printCycles(array $detectedCycles): string + private static function printCycles(array $detectedCycles) : string { return "[\n" . implode("\n", array_map([__CLASS__, 'printCycle'], $detectedCycles)) . "\n]"; } @@ -115,7 +115,7 @@ private static function printCycles(array $detectedCycles): string /** * @param string[] $detectedCycle */ - private static function printCycle(array $detectedCycle): string + private static function printCycle(array $detectedCycle) : string { $fullCycle = array_keys($detectedCycle); $fullCycle[] = reset($fullCycle); @@ -135,7 +135,7 @@ function ($cycle) { * @param bool[][] $detectedCycles * @return bool[][] de-duplicated */ - private static function deDuplicateDetectedCycles(array $detectedCycles): array + private static function deDuplicateDetectedCycles(array $detectedCycles) : array { $detectedCyclesByHash = []; diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index beaab96c..2a10d4f5 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -25,7 +25,7 @@ class InvalidArgumentException extends SplInvalidArgumentException implements Ex * @param mixed $initializer * @return self */ - public static function fromInvalidInitializer($initializer): self + public static function fromInvalidInitializer($initializer) : self { return new self(sprintf( 'An invalid initializer was registered. Expected a callable or an' @@ -39,7 +39,7 @@ public static function fromInvalidInitializer($initializer): self * @param mixed $abstractFactory * @return self */ - public static function fromInvalidAbstractFactory($abstractFactory): self + public static function fromInvalidAbstractFactory($abstractFactory) : self { return new self(sprintf( 'An invalid abstract factory was registered. Expected an instance of or a valid' diff --git a/src/Factory/AbstractFactoryInterface.php b/src/Factory/AbstractFactoryInterface.php index d40872e2..13bb7aef 100644 --- a/src/Factory/AbstractFactoryInterface.php +++ b/src/Factory/AbstractFactoryInterface.php @@ -28,5 +28,5 @@ interface AbstractFactoryInterface extends FactoryInterface * @param string $requestedName * @return bool */ - public function canCreate(ContainerInterface $container, string $requestedName): bool; + public function canCreate(ContainerInterface $container, string $requestedName) : bool; } diff --git a/src/Initializer/InitializerInterface.php b/src/Initializer/InitializerInterface.php index d682ec8f..16ba4bcd 100644 --- a/src/Initializer/InitializerInterface.php +++ b/src/Initializer/InitializerInterface.php @@ -24,5 +24,5 @@ interface InitializerInterface * @param object $instance * @return void */ - public function __invoke(ContainerInterface $container, $instance): void; + public function __invoke(ContainerInterface $container, $instance) : void; } diff --git a/src/PluginManagerInterface.php b/src/PluginManagerInterface.php index b3b399a5..ddd7c53f 100644 --- a/src/PluginManagerInterface.php +++ b/src/PluginManagerInterface.php @@ -24,5 +24,5 @@ interface PluginManagerInterface extends ServiceLocatorInterface * constraint on type imposed by the plugin manager * @throws ContainerException if any other error occurs */ - public function validate($instance): void; + public function validate($instance) : void; } diff --git a/src/Proxy/LazyServiceFactory.php b/src/Proxy/LazyServiceFactory.php index 066c77f4..351490ac 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -54,7 +54,7 @@ public function __invoke( string $name, callable $callback, array $options = null - ): VirtualProxyInterface { + ) : VirtualProxyInterface { $initializer = function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback) { $proxy->setProxyInitializer(null); $wrappedInstance = $callback(); diff --git a/src/ServiceManager.php b/src/ServiceManager.php index f03db749..513faaa0 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -248,7 +248,7 @@ public function build(string $name, array $options = null) /** * {@inheritDoc} */ - public function has($name): bool + public function has($name) : bool { // Check services and factories first to speedup the most common requests. if (isset($this->services[$name]) || isset($this->factories[$name])) { @@ -286,7 +286,7 @@ public function has($name): bool /** * Indicate whether or not the instance is immutable. */ - public function setAllowOverride(bool $flag): void + public function setAllowOverride(bool $flag) : void { $this->allowOverride = (bool) $flag; } @@ -294,7 +294,7 @@ public function setAllowOverride(bool $flag): void /** * Retrieve the flag indicating immutability status. */ - public function getAllowOverride(): bool + public function getAllowOverride() : bool { return $this->allowOverride; } @@ -339,7 +339,7 @@ public function getAllowOverride(): bool * override flag has been toggled off, and a service instance * exists for a given service. */ - public function configure(array $config): self + public function configure(array $config) : self { // This is a bulk update/initial configuration, // so we check all definitions up front. @@ -410,7 +410,7 @@ public function configure(array $config): self * @throws ContainerModificationsNotAllowedException if $alias already * exists as a service and overrides are disallowed. */ - public function setAlias(string $alias, string $target): void + public function setAlias(string $alias, string $target) : void { if (isset($this->services[$alias]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($alias); @@ -428,7 +428,7 @@ public function setAlias(string $alias, string $target): void * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setInvokableClass(string $name, string $class = null): void + public function setInvokableClass(string $name, string $class = null) : void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -446,7 +446,7 @@ public function setInvokableClass(string $name, string $class = null): void * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setFactory(string $name, $factory): void + public function setFactory(string $name, $factory) : void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -462,7 +462,7 @@ public function setFactory(string $name, $factory): void * @param null|string $class Class to which to map; if not provided, $name * will be used for the mapping. */ - public function mapLazyService(string $name, string $class = null): void + public function mapLazyService(string $name, string $class = null) : void { $this->configure(['lazy_services' => ['class_map' => [$name => $class ?: $name]]]); } @@ -473,7 +473,7 @@ public function mapLazyService(string $name, string $class = null): void * @param string|Factory\AbstractFactoryInterface $factory Abstract factory * instance or class name. */ - public function addAbstractFactory($factory): void + public function addAbstractFactory($factory) : void { $this->resolveAbstractFactoryInstance($factory); } @@ -485,7 +485,7 @@ public function addAbstractFactory($factory): void * @param string|callable|Factory\DelegatorFactoryInterface $factory Delegator * factory to assign. */ - public function addDelegator(string $name, $factory): void + public function addDelegator(string $name, $factory) : void { $this->configure(['delegators' => [$name => [$factory]]]); } @@ -495,7 +495,7 @@ public function addDelegator(string $name, $factory): void * * @param string|callable|Initializer\InitializerInterface $initializer */ - public function addInitializer($initializer): void + public function addInitializer($initializer) : void { $this->configure(['initializers' => [$initializer]]); } @@ -508,7 +508,7 @@ public function addInitializer($initializer): void * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setService(string $name, $service): void + public function setService(string $name, $service) : void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -524,7 +524,7 @@ public function setService(string $name, $service): void * @throws ContainerModificationsNotAllowedException if $name already * exists as a service and overrides are disallowed. */ - public function setShared(string $name, bool $flag): void + public function setShared(string $name, bool $flag) : void { if (isset($this->services[$name]) && ! $this->allowOverride) { throw ContainerModificationsNotAllowedException::fromExistingService($name); @@ -538,7 +538,7 @@ public function setShared(string $name, bool $flag): void * * @param string[]|Initializer\InitializerInterface[]|callable[] $initializers */ - private function resolveInitializers(array $initializers): void + private function resolveInitializers(array $initializers) : void { foreach ($initializers as $initializer) { if (is_string($initializer) && class_exists($initializer)) { @@ -561,7 +561,7 @@ private function resolveInitializers(array $initializers): void * @return callable * @throws ServiceNotFoundException */ - private function getFactory(string $name): callable + private function getFactory(string $name) : callable { $factory = $this->factories[$name] ?? null; @@ -694,7 +694,7 @@ private function doCreate(string $resolvedName, array $options = null) * @throws ServiceNotCreatedException when the lazy service class_map * configuration is missing */ - private function createLazyServiceDelegatorFactory(): Proxy\LazyServiceFactory + private function createLazyServiceDelegatorFactory() : Proxy\LazyServiceFactory { if ($this->lazyServicesDelegator) { return $this->lazyServicesDelegator; @@ -741,7 +741,7 @@ private function createLazyServiceDelegatorFactory(): Proxy\LazyServiceFactory * * @param array $invokables */ - private function createAliasesAndFactoriesForInvokables(array $invokables): void + private function createAliasesAndFactoriesForInvokables(array $invokables) : void { foreach ($invokables as $name => $class) { $this->factories[$class] = Factory\InvokableFactory::class; @@ -765,7 +765,7 @@ private function createAliasesAndFactoriesForInvokables(array $invokables): void * @throws ContainerModificationsNotAllowedException if any * service key is invalid. */ - private function validateServiceNames(array $config): void + private function validateServiceNames(array $config) : void { if ($this->allowOverride || ! $this->configured) { return; @@ -840,7 +840,7 @@ private function validateServiceNames(array $config): void * @param string $alias * @param string $target */ - private function mapAliasToTarget(string $alias, string $target): void + private function mapAliasToTarget(string $alias, string $target) : void { // $target is either an alias or something else // if it is an alias, resolve it @@ -879,7 +879,7 @@ private function mapAliasToTarget(string $alias, string $target): void * @see mapAliasToTarget above * */ - private function mapAliasesToTargets(): void + private function mapAliasesToTargets() : void { $tagged = []; foreach ($this->aliases as $alias => $target) { @@ -924,7 +924,7 @@ private function mapAliasesToTargets(): void * * @param string|Factory\AbstractFactoryInterface $abstractFactories */ - private function resolveAbstractFactoryInstance($abstractFactory): void + private function resolveAbstractFactoryInstance($abstractFactory) : void { if (is_string($abstractFactory) && class_exists($abstractFactory)) { // Cached string factory name diff --git a/src/Tool/ConfigDumper.php b/src/Tool/ConfigDumper.php index bf466a80..7c9e8b11 100644 --- a/src/Tool/ConfigDumper.php +++ b/src/Tool/ConfigDumper.php @@ -58,7 +58,7 @@ public function __construct(ContainerInterface $container = null) /** * @throws InvalidArgumentException for invalid $className */ - public function createDependencyConfig(array $config, string $className, bool $ignoreUnresolved = false): array + public function createDependencyConfig(array $config, string $className, bool $ignoreUnresolved = false) : array { $this->validateClassName($className); @@ -121,14 +121,14 @@ function (ReflectionParameter $argument) { * @throws InvalidArgumentException if class name is not a string or does * not exist. */ - private function validateClassName(string $className): void + private function validateClassName(string $className) : void { if (! class_exists($className) && ! interface_exists($className)) { throw new InvalidArgumentException('Cannot find class or interface with name ' . $className); } } - private function createInvokable(array $config, string $className): array + private function createInvokable(array $config, string $className) : array { $config[ConfigAbstractFactory::class][$className] = []; return $config; @@ -138,7 +138,7 @@ private function createInvokable(array $config, string $className): array * @throws InvalidArgumentException if ConfigAbstractFactory configuration * value is not an array. */ - public function createFactoryMappingsFromConfig(array $config): array + public function createFactoryMappingsFromConfig(array $config) : array { if (! array_key_exists(ConfigAbstractFactory::class, $config)) { return $config; @@ -158,7 +158,7 @@ public function createFactoryMappingsFromConfig(array $config): array return $config; } - public function createFactoryMappings(array $config, string $className): array + public function createFactoryMappings(array $config, string $className) : array { $this->validateClassName($className); @@ -173,7 +173,7 @@ public function createFactoryMappings(array $config, string $className): array return $config; } - public function dumpConfigFile(array $config): string + public function dumpConfigFile(array $config) : string { $prepared = $this->prepareConfig($config); return sprintf( @@ -184,7 +184,7 @@ public function dumpConfigFile(array $config): string ); } - private function prepareConfig(iterable $config, int $indentLevel = 1): string + private function prepareConfig(iterable $config, int $indentLevel = 1) : string { $indent = str_repeat(' ', $indentLevel * 4); $entries = []; @@ -210,7 +210,7 @@ private function prepareConfig(iterable $config, int $indentLevel = 1): string /** * @param string|int|null $key */ - private function createConfigKey($key): ?string + private function createConfigKey($key) : ?string { if (is_string($key) && class_exists($key)) { return sprintf('\\%s::class', $key); @@ -226,7 +226,7 @@ private function createConfigKey($key): ?string /** * @param mixed $value */ - private function createConfigValue($value, int $indentLevel): string + private function createConfigValue($value, int $indentLevel) : string { 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 0cec78e6..afa0a61c 100644 --- a/src/Tool/ConfigDumperCommand.php +++ b/src/Tool/ConfigDumperCommand.php @@ -70,7 +70,7 @@ public function __construct(string $scriptName = self::DEFAULT_SCRIPT_NAME, Cons * @param array $args Argument list, minus script name * @return int Exit status */ - public function __invoke(array $args): int + public function __invoke(array $args) : int { $arguments = $this->parseArgs($args); @@ -114,7 +114,7 @@ public function __invoke(array $args): int return 0; } - private function parseArgs(array $args): stdClass + private function parseArgs(array $args) : stdClass { if (! $args) { return $this->createHelpArgument(); @@ -179,7 +179,7 @@ private function parseArgs(array $args): stdClass * @param resource $resource Defaults to STDOUT * @return void */ - private function help($resource = STDOUT): void + private function help($resource = STDOUT) : void { $this->helper->writeLine(sprintf( self::HELP_TEMPLATE, @@ -202,7 +202,7 @@ private function createArguments( array $config, string $class, bool $ignoreUnresolved - ): stdClass { + ) : stdClass { return (object) [ 'command' => $command, 'configFile' => $configFile, @@ -212,7 +212,7 @@ private function createArguments( ]; } - private function createErrorArgument(string $message): stdClass + private function createErrorArgument(string $message) : stdClass { return (object) [ 'command' => self::COMMAND_ERROR, @@ -220,7 +220,7 @@ private function createErrorArgument(string $message): stdClass ]; } - private function createHelpArgument(): stdClass + private function createHelpArgument() : stdClass { return (object) [ 'command' => self::COMMAND_HELP, diff --git a/src/Tool/FactoryCreator.php b/src/Tool/FactoryCreator.php index 2cdb7075..e094048a 100644 --- a/src/Tool/FactoryCreator.php +++ b/src/Tool/FactoryCreator.php @@ -49,7 +49,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o EOT; - public function createFactory(string $className): string + public function createFactory(string $className) : string { $class = $this->getClassName($className); @@ -64,13 +64,13 @@ public function createFactory(string $className): string ); } - private function getClassName(string $className): string + private function getClassName(string $className) : string { $class = substr($className, strrpos($className, '\\') + 1); return $class; } - private function getConstructorParameters(string $className): array + private function getConstructorParameters(string $className) : array { $reflectionClass = new ReflectionClass($className); @@ -112,7 +112,7 @@ function (ReflectionParameter $argument) { }, $constructorParameters); } - private function createArgumentString(string $className): string + private function createArgumentString(string $className) : string { $arguments = array_map(function ($dependency) { return sprintf('$container->get(\\%s::class)', $dependency); diff --git a/src/Tool/FactoryCreatorCommand.php b/src/Tool/FactoryCreatorCommand.php index b55a7470..c2386376 100644 --- a/src/Tool/FactoryCreatorCommand.php +++ b/src/Tool/FactoryCreatorCommand.php @@ -58,7 +58,7 @@ public function __construct(string $scriptName = self::DEFAULT_SCRIPT_NAME, Cons * @param array $args Argument list, minus script name * @return int Exit status */ - public function __invoke(array $args): int + public function __invoke(array $args) : int { $arguments = $this->parseArgs($args); @@ -93,7 +93,7 @@ public function __invoke(array $args): int return 0; } - private function parseArgs(array $args): stdClass + private function parseArgs(array $args) : stdClass { if (! $args) { return $this->createArguments(self::COMMAND_HELP); @@ -121,7 +121,7 @@ private function parseArgs(array $args): stdClass * @param resource $resource Defaults to STDOUT * @return void */ - private function help($resource = STDOUT): void + private function help($resource = STDOUT) : void { $this->helper->writeLine(sprintf( self::HELP_TEMPLATE, @@ -133,7 +133,7 @@ private function help($resource = STDOUT): void * @param string|null $class Name of class to reflect. * @param string|null $error Error message, if any. */ - private function createArguments(string $command, string $class = null, string $error = null): stdClass + private function createArguments(string $command, string $class = null, string $error = null) : stdClass { return (object) [ 'command' => $command, diff --git a/test/Exception/CyclicAliasExceptionTest.php b/test/Exception/CyclicAliasExceptionTest.php index 483aee26..fb72724b 100644 --- a/test/Exception/CyclicAliasExceptionTest.php +++ b/test/Exception/CyclicAliasExceptionTest.php @@ -34,7 +34,7 @@ public function testFromCyclicAlias(string $alias, array $aliases, string $expec * * @return string[][]|string[][][] */ - public function cyclicAliasProvider(): array + public function cyclicAliasProvider() : array { return [ [ @@ -122,7 +122,7 @@ public function testFromAliasesMap(array $aliases, string $expectedMessage) /** * @return string[][]|string[][][] */ - public function aliasesProvider(): array + public function aliasesProvider() : array { return [ 'empty set' => [ diff --git a/test/TestAsset/AbstractFactoryFoo.php b/test/TestAsset/AbstractFactoryFoo.php index 13dc6c45..3fa2a168 100644 --- a/test/TestAsset/AbstractFactoryFoo.php +++ b/test/TestAsset/AbstractFactoryFoo.php @@ -20,7 +20,7 @@ public function __invoke(ContainerInterface $container, string $requestedName, a return false; } - public function canCreate(ContainerInterface $container, string $requestedName): bool + public function canCreate(ContainerInterface $container, string $requestedName) : bool { return ($requestedName === 'foo'); } diff --git a/test/TestAsset/CallTimesAbstractFactory.php b/test/TestAsset/CallTimesAbstractFactory.php index b94a6f13..005a0337 100644 --- a/test/TestAsset/CallTimesAbstractFactory.php +++ b/test/TestAsset/CallTimesAbstractFactory.php @@ -17,7 +17,7 @@ class CallTimesAbstractFactory implements AbstractFactoryInterface /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, string $name): bool + public function canCreate(ContainerInterface $container, string $name) : bool { self::$callTimes++; diff --git a/test/TestAsset/FailingAbstractFactory.php b/test/TestAsset/FailingAbstractFactory.php index 7365ef2e..8e8dd1fd 100644 --- a/test/TestAsset/FailingAbstractFactory.php +++ b/test/TestAsset/FailingAbstractFactory.php @@ -15,7 +15,7 @@ class FailingAbstractFactory implements AbstractFactoryInterface /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, string $name): bool + public function canCreate(ContainerInterface $container, string $name) : bool { return false; } diff --git a/test/TestAsset/LenientPluginManager.php b/test/TestAsset/LenientPluginManager.php index 5160db2d..f5273040 100644 --- a/test/TestAsset/LenientPluginManager.php +++ b/test/TestAsset/LenientPluginManager.php @@ -14,7 +14,7 @@ class LenientPluginManager extends AbstractPluginManager /** * Allow anything to be considered valid. */ - public function validate($instance): void + public function validate($instance) : void { return; } diff --git a/test/TestAsset/SimpleAbstractFactory.php b/test/TestAsset/SimpleAbstractFactory.php index 24109e3b..70fc5287 100644 --- a/test/TestAsset/SimpleAbstractFactory.php +++ b/test/TestAsset/SimpleAbstractFactory.php @@ -15,7 +15,7 @@ class SimpleAbstractFactory implements AbstractFactoryInterface /** * {@inheritDoc} */ - public function canCreate(ContainerInterface $container, string $name): bool + public function canCreate(ContainerInterface $container, string $name) : bool { return true; } diff --git a/test/TestAsset/SimpleInitializer.php b/test/TestAsset/SimpleInitializer.php index dc0e91bb..ae3334c6 100644 --- a/test/TestAsset/SimpleInitializer.php +++ b/test/TestAsset/SimpleInitializer.php @@ -16,7 +16,7 @@ class SimpleInitializer implements InitializerInterface /** * {@inheritDoc} */ - public function __invoke(ContainerInterface $container, $instance): void + public function __invoke(ContainerInterface $container, $instance) : void { if (! $instance instanceof stdClass) { return; diff --git a/test/TestAsset/V2v3PluginManager.php b/test/TestAsset/V2v3PluginManager.php index 6146647d..1b23c3e3 100644 --- a/test/TestAsset/V2v3PluginManager.php +++ b/test/TestAsset/V2v3PluginManager.php @@ -33,7 +33,7 @@ class V2v3PluginManager extends AbstractPluginManager protected $sharedByDefault = false; - public function validate($plugin): void + public function validate($plugin) : void { if ($plugin instanceof $this->instanceOf) { return;