Skip to content

Commit

Permalink
[Php 8] Update up to php 8 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Oct 12, 2021
1 parent b38f401 commit f916f65
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 136 deletions.
7 changes: 1 addition & 6 deletions src/ProxyManager/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@

class Autoloader implements AutoloaderInterface
{
protected FileLocatorInterface $fileLocator;
protected ClassNameInflectorInterface $classNameInflector;

public function __construct(FileLocatorInterface $fileLocator, ClassNameInflectorInterface $classNameInflector)
public function __construct(protected FileLocatorInterface $fileLocator, protected ClassNameInflectorInterface $classNameInflector)
{
$this->fileLocator = $fileLocator;
$this->classNameInflector = $classNameInflector;
}

public function __invoke(string $className): bool
Expand Down
8 changes: 2 additions & 6 deletions src/ProxyManager/Exception/InvalidProxiedClassException.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@ public static function abstractProtectedMethodsNotSupported(ReflectionClass $ref
implode(
"\n",
array_map(
static function (ReflectionMethod $reflectionMethod): string {
return $reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName();
},
static fn (ReflectionMethod $reflectionMethod): string => $reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName(),
array_filter(
$reflection->getMethods(),
static function (ReflectionMethod $method): bool {
return $method->isAbstract() && $method->isProtected();
}
static fn (ReflectionMethod $method): bool => $method->isAbstract() && $method->isProtected()
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public static function nonReferenceableLocalizedReflectionProperties(
return new self(sprintf(
'Cannot create references for following properties of class %s: %s',
$class->getName(),
implode(', ', array_map(static function (ReflectionProperty $property): string {
return $property->getName();
}, $properties->getInstanceProperties()))
implode(', ', array_map(static fn (ReflectionProperty $property): string => $property->getName(), $properties->getInstanceProperties()))
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use ProxyManager\Signature\Exception\InvalidSignatureException;
use ProxyManager\Signature\Exception\MissingSignatureException;

use function get_class;

/**
* Factory responsible of producing proxy objects
*/
Expand Down Expand Up @@ -67,7 +65,7 @@ public function createProxy(
array $prefixInterceptors = [],
array $suffixInterceptors = []
): AccessInterceptorInterface {
$proxyClassName = $this->generateProxy(get_class($instance));
$proxyClassName = $this->generateProxy($instance::class);

/**
* We ignore type checks here, since `staticProxyConstructor` is not interfaced (by design)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use ProxyManager\Signature\Exception\InvalidSignatureException;
use ProxyManager\Signature\Exception\MissingSignatureException;

use function get_class;

/**
* Factory responsible of producing proxy objects
*/
Expand Down Expand Up @@ -69,7 +67,7 @@ public function createProxy(
array $prefixInterceptors = [],
array $suffixInterceptors = []
): AccessInterceptorValueHolderInterface {
$proxyClassName = $this->generateProxy(get_class($instance));
$proxyClassName = $this->generateProxy($instance::class);

/**
* We ignore type checks here, since `staticProxyConstructor` is not interfaced (by design)
Expand Down
3 changes: 1 addition & 2 deletions src/ProxyManager/Factory/NullObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use ProxyManager\Signature\Exception\InvalidSignatureException;
use ProxyManager\Signature\Exception\MissingSignatureException;

use function get_class;
use function is_object;

/**
Expand Down Expand Up @@ -45,7 +44,7 @@ public function __construct(?Configuration $configuration = null)
*/
public function createProxy(object|string $instanceOrClassName): NullObjectInterface
{
$className = is_object($instanceOrClassName) ? get_class($instanceOrClassName) : $instanceOrClassName;
$className = is_object($instanceOrClassName) ? $instanceOrClassName::class : $instanceOrClassName;
$proxyClassName = $this->generateProxy($className);

/**
Expand Down
18 changes: 5 additions & 13 deletions src/ProxyManager/Factory/RemoteObject/Adapter/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,16 @@
*/
abstract class BaseAdapter implements AdapterInterface
{
protected Client $client;

/**
* Service name mapping
*
* @var array<string, string>
*/
protected array $map = [];

/**
* Constructor
*
* @param array<string, string> $map map of service names to their aliases
*/
public function __construct(Client $client, array $map = [])
{
$this->client = $client;
$this->map = $map;
public function __construct(
protected Client $client,
// Service name mapping
protected array $map = []
) {
}

/**
Expand Down
11 changes: 3 additions & 8 deletions src/ProxyManager/Factory/RemoteObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,23 @@
use ProxyManager\Signature\Exception\InvalidSignatureException;
use ProxyManager\Signature\Exception\MissingSignatureException;

use function get_class;
use function is_object;

/**
* Factory responsible of producing remote proxy objects
*/
class RemoteObjectFactory extends AbstractBaseFactory
{
protected AdapterInterface $adapter;
private ?RemoteObjectGenerator $generator;

/**
* {@inheritDoc}
*
* @param AdapterInterface $adapter
* @param Configuration $configuration
* @param Configuration $configuration
*/
public function __construct(AdapterInterface $adapter, ?Configuration $configuration = null)
public function __construct(protected AdapterInterface $adapter, ?Configuration $configuration = null)
{
parent::__construct($configuration);

$this->adapter = $adapter;
$this->generator = new RemoteObjectGenerator();
}

Expand All @@ -54,7 +49,7 @@ public function __construct(AdapterInterface $adapter, ?Configuration $configura
public function createProxy(string|object $instanceOrClassName): RemoteObjectInterface
{
$proxyClassName = $this->generateProxy(
is_object($instanceOrClassName) ? get_class($instanceOrClassName) : $instanceOrClassName
is_object($instanceOrClassName) ? $instanceOrClassName::class : $instanceOrClassName
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
*/
class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
{
protected FileLocatorInterface $fileLocator;
private Closure $emptyErrorHandler;

public function __construct(FileLocatorInterface $fileLocator)
public function __construct(protected FileLocatorInterface $fileLocator)
{
$this->fileLocator = $fileLocator;
$this->emptyErrorHandler = static function (): void {
};
}
Expand Down
4 changes: 1 addition & 3 deletions src/ProxyManager/Inflector/ClassNameInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

final class ClassNameInflector implements ClassNameInflectorInterface
{
protected string $proxyNamespace;
/** @var int @TODO annotation still needed for phpstan to understand this */
private int $proxyMarkerLength;
private string $proxyMarker;
private ParameterHasher $parameterHasher;

public function __construct(string $proxyNamespace)
public function __construct(protected string $proxyNamespace)
{
$this->proxyNamespace = $proxyNamespace;
$this->proxyMarker = '\\' . self::PROXY_MARKER . '\\';
$this->proxyMarkerLength = strlen($this->proxyMarker);
$this->parameterHasher = new ParameterHasher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public static function createInterceptedMethodBody(
'{{$suffixInterceptorsName}}' => $suffixInterceptors->getName(),
'{{$suffixEarlyReturnExpression}}' => ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod),
'{{$returnExpression}}' => ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod),
'{{$paramsString}}' => 'array(' . implode(', ', array_map(static function (ParameterGenerator $parameter): string {
return var_export($parameter->getName(), true) . ' => $' . $parameter->getName();
}, $method->getParameters())) . ')',
'{{$paramsString}}' => 'array(' . implode(', ', array_map(static fn (ParameterGenerator $parameter): string => var_export($parameter->getName(), true) . ' => $' . $parameter->getName(), $method->getParameters())) . ')',
];

return str_replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ private function buildMethodInterceptor(
MethodPrefixInterceptors $prefixInterceptors,
MethodSuffixInterceptors $suffixInterceptors
): callable {
return static function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors): InterceptedMethod {
return InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$prefixInterceptors,
$suffixInterceptors
);
};
return static fn (ReflectionMethod $method): InterceptedMethod => InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$prefixInterceptors,
$suffixInterceptors
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ private function buildMethodInterceptor(
MethodSuffixInterceptors $suffixes,
ValueHolderProperty $valueHolder
): callable {
return static function (ReflectionMethod $method) use ($prefixes, $suffixes, $valueHolder): InterceptedMethod {
return InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$valueHolder,
$prefixes,
$suffixes
);
};
return static fn (ReflectionMethod $method): InterceptedMethod => InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$valueHolder,
$prefixes,
$suffixes
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ private static function hasNoAbstractProtectedMethods(ReflectionClass $originalC
{
$protectedAbstract = array_filter(
$originalClass->getMethods(),
static function (ReflectionMethod $method): bool {
return $method->isAbstract() && $method->isProtected();
}
static fn (ReflectionMethod $method): bool => $method->isAbstract() && $method->isProtected()
);

if ($protectedAbstract) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ private function getPropertyDefaultsAssignments(array $properties): string
return implode(
"\n",
array_map(
function (ReflectionProperty $property): string {
return ' $instance->' . $property->getName()
. ' = ' . $this->getExportedPropertyDefaultValue($property) . ';';
},
fn (ReflectionProperty $property): string => ' $instance->' . $property->getName()
. ' = ' . $this->getExportedPropertyDefaultValue($property) . ';',
$properties
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator;

use Closure;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\PropertyGenerator;
use ProxyManager\Generator\MethodGenerator;
Expand All @@ -21,7 +22,7 @@ public function __construct(PropertyGenerator $initializerProperty)
{
parent::__construct(
'setProxyInitializer',
[(new ParameterGenerator('initializer', 'Closure'))->setDefaultValue(null)],
[(new ParameterGenerator('initializer', Closure::class))->setDefaultValue(null)],
self::FLAG_PUBLIC,
'$this->' . $initializerProperty->getName() . ' = $initializer;'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ private function buildLazyLoadingMethodInterceptor(
InitializerProperty $initializer,
ValueHolderProperty $valueHolder
): callable {
return static function (ReflectionMethod $method) use ($initializer, $valueHolder): LazyLoadingMethodInterceptor {
return LazyLoadingMethodInterceptor::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$initializer,
$valueHolder
);
};
return static fn (ReflectionMethod $method): LazyLoadingMethodInterceptor => LazyLoadingMethodInterceptor::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$initializer,
$valueHolder
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public function __construct(ReflectionClass $originalClass)
parent::__construct('staticProxyConstructor', [], self::FLAG_PUBLIC | self::FLAG_STATIC);

$nullableProperties = array_map(
static function (ReflectionProperty $publicProperty): string {
return '$instance->' . $publicProperty->getName() . ' = null;';
},
static fn (ReflectionProperty $publicProperty): string => '$instance->' . $publicProperty->getName() . ' = null;',
Properties::fromReflectionClass($originalClass)
->onlyNullableProperties()
->getPublicProperties()
Expand Down
12 changes: 5 additions & 7 deletions src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ static function (MethodGenerator $generatedMethod) use ($originalClass, $classGe
},
array_merge(
array_map(
static function (ReflectionMethod $method) use ($adapter, $originalClass): RemoteObjectMethod {
return RemoteObjectMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$adapter,
$originalClass
);
},
static fn (ReflectionMethod $method): RemoteObjectMethod => RemoteObjectMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$adapter,
$originalClass
),
ProxiedMethodsFilter::getProxiedMethods(
$originalClass,
['__get', '__set', '__isset', '__unset']
Expand Down
20 changes: 6 additions & 14 deletions src/ProxyManager/ProxyGenerator/Util/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@
*/
final class Properties
{
/** @var ReflectionProperty[] */
private array $properties;

/**
* @param ReflectionProperty[] $properties
*/
private function __construct(array $properties)
private function __construct(private array $properties)
{
$this->properties = $properties;
}

public static function fromReflectionClass(ReflectionClass $reflection): self
Expand All @@ -46,15 +42,11 @@ public static function fromReflectionClass(ReflectionClass $reflection): self
} while ($class);

return new self(array_merge(
...array_map(static function (ReflectionClass $class): array {
return array_values(array_filter(
$class->getProperties(),
static function (ReflectionProperty $property) use ($class): bool {
return $class->getName() === $property->getDeclaringClass()->getName()
&& ! $property->isStatic();
}
));
}, $parentClasses)
...array_map(static fn (ReflectionClass $class): array => array_values(array_filter(
$class->getProperties(),
static fn (ReflectionProperty $property): bool => $class->getName() === $property->getDeclaringClass()->getName()
&& ! $property->isStatic()
)), $parentClasses)
));
}

Expand Down
10 changes: 4 additions & 6 deletions src/ProxyManager/ProxyGenerator/Util/ProxiedMethodsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ private static function doFilter(ReflectionClass $class, array $excluded, bool $

return array_values(array_filter(
$class->getMethods(ReflectionMethod::IS_PUBLIC),
static function (ReflectionMethod $method) use ($ignored, $requireAbstract): bool {
return (! $requireAbstract || $method->isAbstract()) && ! (
array_key_exists(strtolower($method->getName()), $ignored)
|| self::methodCannotBeProxied($method)
);
}
static fn (ReflectionMethod $method): bool => (! $requireAbstract || $method->isAbstract()) && ! (
array_key_exists(strtolower($method->getName()), $ignored)
|| self::methodCannotBeProxied($method)
)
));
}

Expand Down
Loading

0 comments on commit f916f65

Please sign in to comment.