Skip to content

Commit

Permalink
Merge pull request #157 from samsonasik/apply-php80-syntax
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius authored Oct 10, 2022
2 parents 5d786a0 + 0c9036e commit ed16072
Show file tree
Hide file tree
Showing 22 changed files with 36 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function get($name, ?array $options = null)
*
* @psalm-assert InstanceType $instance
*/
public function validate($instance)
public function validate(mixed $instance)
{
if (method_exists($this, 'validatePlugin')) {
trigger_error(sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CyclicAliasException.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function fromCyclicAlias(string $alias, array $aliases): self
public static function fromAliasesMap(array $aliases)
{
$detectedCycles = array_filter(array_map(
static fn($alias) => self::getCycleFor($aliases, $alias),
static fn($alias): ?array => self::getCycleFor($aliases, $alias),
array_keys($aliases)
));

Expand Down
10 changes: 2 additions & 8 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
*/
class InvalidArgumentException extends SplInvalidArgumentException implements ExceptionInterface
{
/**
* @param mixed $initializer
*/
public static function fromInvalidInitializer($initializer): self
public static function fromInvalidInitializer(mixed $initializer): self
{
return new self(sprintf(
'An invalid initializer was registered. Expected a callable or an'
Expand All @@ -30,10 +27,7 @@ public static function fromInvalidInitializer($initializer): self
));
}

/**
* @param mixed $abstractFactory
*/
public static function fromInvalidAbstractFactory($abstractFactory): self
public static function fromInvalidAbstractFactory(mixed $abstractFactory): self
{
return new self(sprintf(
'An invalid abstract factory was registered. Expected an instance of or a valid'
Expand Down
3 changes: 1 addition & 2 deletions src/InitializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ interface InitializerInterface extends Initializer\InitializerInterface
/**
* Initialize
*
* @param mixed $instance
* @return mixed
*/
public function initialize($instance, ServiceLocatorInterface $serviceLocator);
public function initialize(mixed $instance, ServiceLocatorInterface $serviceLocator);
}
3 changes: 1 addition & 2 deletions src/PluginManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ interface PluginManagerInterface extends ServiceLocatorInterface
/**
* Validate an instance
*
* @param mixed $instance
* @return void
* @throws InvalidServiceException If created instance does not respect the
* constraint on type imposed by the plugin manager.
* @throws ContainerExceptionInterface If any other error occurs.
* @psalm-assert InstanceType $instance
*/
public function validate($instance);
public function validate(mixed $instance);
}
11 changes: 2 additions & 9 deletions src/Proxy/LazyServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@
*/
final class LazyServiceFactory implements DelegatorFactoryInterface
{
private LazyLoadingValueHolderFactory $proxyFactory;

/** @var array<string, class-string> map of service names to class names */
private array $servicesMap;

/**
* @param array<string, class-string> $servicesMap A map of service names to
* class names of their respective classes
*/
public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $servicesMap)
public function __construct(private LazyLoadingValueHolderFactory $proxyFactory, private array $servicesMap)
{
$this->proxyFactory = $proxyFactory;
$this->servicesMap = $servicesMap;
}

/**
Expand All @@ -45,7 +38,7 @@ public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $
public function __invoke(ContainerInterface $container, $name, callable $callback, ?array $options = null)
{
if (isset($this->servicesMap[$name])) {
$initializer = function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback) {
$initializer = static function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback): bool {
$proxy->setProxyInitializer(null);
$wrappedInstance = $callback();

Expand Down
2 changes: 0 additions & 2 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class ServiceManager implements ServiceLocatorInterface
* See {@see \Laminas\ServiceManager\ServiceManager::configure()} for details
* on what $config accepts.
*
* @param array $config
* @psalm-param ServiceManagerConfiguration $config
*/
public function __construct(array $config = [])
Expand Down Expand Up @@ -285,7 +284,6 @@ public function getAllowOverride()
}

/**
* @param array $config
* @psalm-param ServiceManagerConfiguration $config
* @return self
* @throws ContainerModificationsNotAllowedException If the allow
Expand Down
13 changes: 2 additions & 11 deletions src/Tool/ConfigDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ class ConfigDumper
return %s;
EOC;

private ?ContainerInterface $container;

public function __construct(?ContainerInterface $container = null)
public function __construct(private ?ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* @param array $config
* @param string $className
* @param bool $ignoreUnresolved
* @return array
Expand Down Expand Up @@ -127,7 +123,6 @@ private function validateClassName($className)
}

/**
* @param array $config
* @param string $className
* @return array
*/
Expand All @@ -138,7 +133,6 @@ private function createInvokable(array $config, $className)
}

/**
* @param array $config
* @return array
* @throws InvalidArgumentException If ConfigAbstractFactory configuration
* value is not an array.
Expand All @@ -164,7 +158,6 @@ public function createFactoryMappingsFromConfig(array $config)
}

/**
* @param array $config
* @param string $className
* @return array
*/
Expand All @@ -185,7 +178,6 @@ public function createFactoryMappings(array $config, $className)
}

/**
* @param array $config
* @return string
*/
public function dumpConfigFile(array $config)
Expand Down Expand Up @@ -245,11 +237,10 @@ private function createConfigKey($key)
}

/**
* @param mixed $value
* @param int $indentLevel
* @return string
*/
private function createConfigValue($value, $indentLevel)
private function createConfigValue(mixed $value, $indentLevel)
{
if (is_array($value) || $value instanceof Traversable) {
return $this->prepareConfig($value, $indentLevel + 1);
Expand Down
8 changes: 2 additions & 6 deletions src/Tool/ConfigDumperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ class ConfigDumperCommand

private ConsoleHelper $helper;

private string $scriptName;

/**
* @param string $scriptName
*/
public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null)
public function __construct(private $scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null)
{
$this->scriptName = $scriptName;
$this->helper = $helper ?: new ConsoleHelper();
$this->helper = $helper ?: new ConsoleHelper();
}

/**
Expand Down Expand Up @@ -128,7 +125,6 @@ public function __invoke(array $args)
}

/**
* @param array $args
* @return object
*/
private function parseArgs(array $args)
Expand Down
6 changes: 3 additions & 3 deletions src/Tool/FactoryCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private function getConstructorParameters($className)

$constructorParameters = array_filter(
$constructorParameters,
function (ReflectionParameter $argument): bool {
static function (ReflectionParameter $argument): bool {
if ($argument->isOptional()) {
return false;
}
Expand All @@ -124,7 +124,7 @@ function (ReflectionParameter $argument): bool {
return [];
}

return array_map(function (ReflectionParameter $parameter): ?string {
return array_map(static function (ReflectionParameter $parameter): ?string {
$type = $parameter->getType();
return $type instanceof ReflectionNamedType && ! $type->isBuiltin() ? $type->getName() : null;
}, $constructorParameters);
Expand All @@ -136,7 +136,7 @@ function (ReflectionParameter $argument): bool {
*/
private function createArgumentString($className)
{
$arguments = array_map(fn(string $dependency): string
$arguments = array_map(static fn(string $dependency): string
=> sprintf('$container->get(\\%s::class)', $dependency), $this->getConstructorParameters($className));

switch (count($arguments)) {
Expand Down
8 changes: 2 additions & 6 deletions src/Tool/FactoryCreatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,12 @@ class FactoryCreatorCommand

private ConsoleHelper $helper;

private string $scriptName;

/**
* @param string $scriptName
*/
public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null)
public function __construct(private $scriptName = self::DEFAULT_SCRIPT_NAME, ?ConsoleHelper $helper = null)
{
$this->scriptName = $scriptName;
$this->helper = $helper ?: new ConsoleHelper();
$this->helper = $helper ?: new ConsoleHelper();
}

/**
Expand Down Expand Up @@ -98,7 +95,6 @@ public function __invoke(array $args)
}

/**
* @param array $args
* @return ArgumentObject
*/
private function parseArgs(array $args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

final class ClassAcceptingConfigToConstructor
{
public array $config;

public function __construct(array $config)
public function __construct(public array $config)
{
$this->config = $config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

final class ClassAcceptingWellKnownServicesAsConstructorParameters
{
public ValidatorPluginManager $validators;

public function __construct(ValidatorPluginManager $validators)
public function __construct(public ValidatorPluginManager $validators)
{
$this->validators = $validators;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,11 @@

final class ClassWithMixedConstructorParameters
{
public array $config;

public SampleInterface $sample;

public ValidatorPluginManager $validators;

public ?array $options;

public function __construct(
array $config,
SampleInterface $sample,
ValidatorPluginManager $validators,
?array $options = null
public array $config,
public SampleInterface $sample,
public ValidatorPluginManager $validators,
public ?array $options = null
) {
$this->config = $config;
$this->sample = $sample;
$this->validators = $validators;
$this->options = $options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

final class ClassWithScalarDependencyDefiningDefaultValue
{
public string $foo;

public function __construct(string $foo = 'bar')
public function __construct(public string $foo = 'bar')
{
$this->foo = $foo;
}
}
8 changes: 1 addition & 7 deletions test/AbstractFactory/TestAsset/ClassWithScalarParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

final class ClassWithScalarParameters
{
public string $foo = 'foo';

public string $bar = 'bar';

public function __construct(string $foo, string $bar)
public function __construct(public string $foo, public string $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

final class ClassWithTypeHintedConstructorParameter
{
public SampleInterface $sample;

public function __construct(SampleInterface $sample)
public function __construct(public SampleInterface $sample)
{
$this->sample = $sample;
}
}
3 changes: 1 addition & 2 deletions test/AbstractPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,9 @@ public function invalidConstructorArguments(): array

/**
* @group migration
* @param mixed $arg
* @dataProvider invalidConstructorArguments
*/
public function testPassingNonContainerNonConfigNonNullFirstConstructorArgumentRaisesException($arg): void
public function testPassingNonContainerNonConfigNonNullFirstConstructorArgumentRaisesException(mixed $arg): void
{
$this->expectException(InvalidArgumentException::class);
new TestAsset\LenientPluginManager($arg);
Expand Down
Loading

0 comments on commit ed16072

Please sign in to comment.