diff --git a/benchmarks/BenchAsset/AbstractFactoryFoo.php b/benchmarks/BenchAsset/AbstractFactoryFoo.php index 4d61801f..ddbe7d75 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); } diff --git a/src/AbstractFactory/ConfigAbstractFactory.php b/src/AbstractFactory/ConfigAbstractFactory.php index eef3c935..a2a7dc29 100644 --- a/src/AbstractFactory/ConfigAbstractFactory.php +++ b/src/AbstractFactory/ConfigAbstractFactory.php @@ -8,6 +8,7 @@ namespace Zend\ServiceManager\AbstractFactory; use ArrayObject; +use Psr\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(\Psr\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(\Psr\Container\ContainerInterface $container, $request /** * {@inheritDoc} */ - public function __invoke(\Psr\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 6df35677..6fb64b08 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,13 +146,11 @@ 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 * @return mixed @@ -169,12 +167,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,15 +187,15 @@ 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 ca196472..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) + 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 b82d4c85..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) + 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,7 +98,7 @@ 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) { diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index 67e1aee6..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); + 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 8f315d66..4c2f16f4 100644 --- a/src/Exception/ContainerModificationsNotAllowedException.php +++ b/src/Exception/ContainerModificationsNotAllowedException.php @@ -18,9 +18,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..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($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 4eb9e891..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) + 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) * @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 03a0e471..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, $requestedName); + public function canCreate(ContainerInterface $container, string $requestedName) : bool; } diff --git a/src/Factory/DelegatorFactoryInterface.php b/src/Factory/DelegatorFactoryInterface.php index 02e69c21..5ca34060 100644 --- a/src/Factory/DelegatorFactoryInterface.php +++ b/src/Factory/DelegatorFactoryInterface.php @@ -34,5 +34,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 33084447..98936bc6 100644 --- a/src/Factory/FactoryInterface.php +++ b/src/Factory/FactoryInterface.php @@ -33,5 +33,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 85ac0b58..7d8ca14d 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 24c059c4..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); + public function __invoke(ContainerInterface $container, $instance) : void; } diff --git a/src/InitializerInterface.php b/src/InitializerInterface.php index 23c5ac20..2f5f3646 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 7392195e..ddd7c53f 100644 --- a/src/PluginManagerInterface.php +++ b/src/PluginManagerInterface.php @@ -20,10 +20,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 9f2c2446..351490ac 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -9,10 +9,10 @@ use ProxyManager\Factory\LazyLoadingValueHolderFactory; use ProxyManager\Proxy\LazyLoadingInterface; +use ProxyManager\Proxy\VirtualProxyInterface; use Psr\Container\ContainerInterface; 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,10 +47,14 @@ 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); $wrappedInstance = $callback(); diff --git a/src/ServiceLocatorInterface.php b/src/ServiceLocatorInterface.php index ffcfe08d..37328a0d 100644 --- a/src/ServiceLocatorInterface.php +++ b/src/ServiceLocatorInterface.php @@ -27,5 +27,5 @@ interface ServiceLocatorInterface extends ContainerInterface * 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 e4d0c48f..513faaa0 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -238,7 +238,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; @@ -248,7 +248,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])) { @@ -285,20 +285,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; } @@ -343,7 +339,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. @@ -414,7 +410,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); @@ -432,7 +428,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); @@ -450,7 +446,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); @@ -466,7 +462,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]]]); } @@ -477,7 +473,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); } @@ -489,7 +485,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]]]); } @@ -499,7 +495,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]]); } @@ -508,11 +504,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); @@ -528,7 +524,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); @@ -542,7 +538,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)) { @@ -565,7 +561,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; @@ -601,7 +597,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 @@ -661,7 +657,7 @@ private function createDelegatorFromName($name, array $options = null) * creating a service. * @throws ExceptionInterface 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])) { @@ -698,7 +694,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; @@ -745,7 +741,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; @@ -769,7 +765,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; @@ -844,7 +840,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 @@ -883,7 +879,7 @@ private function mapAliasToTarget($alias, $target) * @see mapAliasToTarget above * */ - private function mapAliasesToTargets() + private function mapAliasesToTargets() : void { $tagged = []; foreach ($this->aliases as $alias => $target) { @@ -927,9 +923,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 e4cb5a63..7c9e8b11 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..afa0a61c 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,10 +194,15 @@ 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, 'configFile' => $configFile, @@ -214,11 +212,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 +220,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 f3f19ec7..e094048a 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..c2386376 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..fb72724b 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 9ab36eb7..3fa2a168 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 a14382f8..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, $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 8332cece..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, $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 27169ffe..277d291d 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 5d2085d3..d9778673 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..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) + public function validate($instance) : void { return; } diff --git a/test/TestAsset/PassthroughDelegatorFactory.php b/test/TestAsset/PassthroughDelegatorFactory.php index d0eec4b9..923ba312 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 97e32b8a..5c5312e9 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 b0e0db2d..f3ffd406 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 2c5f74df..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, $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 9419a763..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) + 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..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) + public function validate($plugin) : void { if ($plugin instanceof $this->instanceOf) { return; diff --git a/test/Tool/ConfigDumperTest.php b/test/Tool/ConfigDumperTest.php index 38ff0846..090f60ac 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';