Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Add php 7.1 type hints #247

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmarks/BenchAsset/AbstractFactoryFoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

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);
}
return false;
}

public function canCreate(ContainerInterface $container, $requestedName)
public function canCreate(ContainerInterface $container, string $requestedName): bool
{
return ($requestedName === 'foo');
}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/BenchAsset/FactoryFoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/AbstractFactory/ConfigAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Zend\ServiceManager\AbstractFactory;

use ArrayObject;
use Psr\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any plans to release this feature of coding standard?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add your question to the related PR. Thanks!

{
if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) {
return false;
Expand All @@ -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');
Expand Down
30 changes: 12 additions & 18 deletions src/AbstractFactory/ReflectionBasedAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here…

{
return class_exists($requestedName);
}
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and so on…

/**
* @param ReflectionParameter $parameter
* @return mixed
Expand All @@ -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
Expand All @@ -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 [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public function __construct(array $config = [])
/**
* @inheritdoc
*/
public function configureServiceManager(ServiceManager $serviceManager)
public function configureServiceManager(ServiceManager $serviceManager): ServiceManager
{
return $serviceManager->configure($this->config);
}

/**
* @inheritdoc
*/
public function toArray()
public function toArray(): array
{
return $this->config;
}
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -42,5 +42,5 @@ public function configureServiceManager(ServiceManager $serviceManager);
*
* @return array
*/
public function toArray();
public function toArray(): array;
}
3 changes: 1 addition & 2 deletions src/Exception/ContainerModificationsNotAllowedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 7 additions & 12 deletions src/Exception/CyclicAliasException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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 = [];

Expand All @@ -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);
Expand All @@ -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 = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/AbstractFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Factory/DelegatorFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/Factory/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/Factory/InvokableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Initializer/InitializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ interface InitializerInterface
* @param object $instance
* @return void
*/
public function __invoke(ContainerInterface $container, $instance);
public function __invoke(ContainerInterface $container, $instance): void;
}
2 changes: 1 addition & 1 deletion src/InitializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface InitializerInterface extends Initializer\InitializerInterface
/**
* Initialize
*
* @param $instance
* @param mixed $instance
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
Expand Down
3 changes: 1 addition & 2 deletions src/PluginManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 9 additions & 5 deletions src/Proxy/LazyServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -24,7 +24,7 @@
final class LazyServiceFactory implements DelegatorFactoryInterface
{
/**
* @var \ProxyManager\Factory\LazyLoadingValueHolderFactory
* @var LazyLoadingValueHolderFactory
*/
private $proxyFactory;

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceLocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading