From 972049d720eb332098a1de2626e39fa84a0daeb3 Mon Sep 17 00:00:00 2001 From: Valentin V / vvval Date: Tue, 24 Dec 2019 13:02:05 +0300 Subject: [PATCH] Add compatibility with php7.4.1 See https://github.com/php/php-src/pull/4974 Now ONLY FOR 7.4.0 <= php version < 7.4.1 proxy does not unset public typed properties with defaults --- .travis.yml | 6 +- .../Declaration/ReflectionDeclaration.php | 7 ++- src/Promise/Declaration/Declarations.php | 6 +- src/Promise/Declaration/Extractor.php | 14 +++-- .../Declaration/Extractor/Constants.php | 6 +- src/Promise/Declaration/Extractor/Methods.php | 47 +++++++++------- .../Declaration/Extractor/Properties.php | 16 ++++-- src/Promise/Declaration/Structure.php | 37 +++++++++---- .../Exception/ProxyFactoryException.php | 9 ++- src/Promise/MaterializerInterface.php | 10 ++-- .../Materizalizer/EvalMaterializer.php | 3 +- .../Materizalizer/FileMaterializer.php | 6 +- .../Materizalizer/ModificationInspector.php | 24 ++++---- src/Promise/Names.php | 8 ++- src/Promise/PHPDoc.php | 2 +- src/Promise/Printer.php | 8 ++- src/Promise/ProxyFactory.php | 27 +++++---- src/Promise/Visitor/AddPropertiesConst.php | 6 +- src/Promise/Visitor/AddProxiedMethods.php | 2 +- src/Promise/Visitor/DeclareClass.php | 2 +- src/functions/utils.php | 2 +- src/functions/version.php | 6 +- tests/Promise/BaseTest.php | 3 +- .../ConflictResolver/ConflictResolverTest.php | 7 ++- .../Promise/ConflictResolver/SequenceTest.php | 7 ++- tests/Promise/Declaration/DeclarationTest.php | 36 ++++++------ tests/Promise/Declaration/ExtractorTest.php | 33 ++++++----- tests/Promise/FactoryTest.php | 30 +++++----- tests/Promise/MaterializerTest.php | 16 ++++-- tests/Promise/ModificationInspectorTest.php | 21 ++++--- .../ProxyPrinter/BaseProxyPrinterTest.php | 23 +++++--- tests/Promise/ProxyPrinter/ConstantsTest.php | 32 ++++++----- .../Promise/ProxyPrinter/DeclarationTest.php | 22 +++++--- .../ProxyPrinter/Fixtures/ConflictEntity.php | 2 +- .../ProxyPrinter/Methods/MethodArgsTest.php | 36 ++++++------ .../ProxyPrinter/Methods/MethodsTest.php | 55 +++++++++++-------- .../ProxyPrinter/Methods/ReturnStmtTest.php | 44 ++++++++------- tests/Promise/ProxyPrinter/NamespaceTest.php | 24 ++++---- tests/Promise/ProxyPrinter/PropertiesTest.php | 29 ++++++---- tests/Promise/ProxyPrinter/UseStmtsTest.php | 23 ++++---- tests/fixtures/promises/FileTestThree.php | 3 +- tests/php74/ConstantsPHP74Test.php | 38 ++++++++----- 42 files changed, 431 insertions(+), 307 deletions(-) diff --git a/.travis.yml b/.travis.yml index f991f4a..29f45aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: php php: - - "7.2" - - "7.3" - - "7.4snapshot" + - 7.2 + - 7.3 + - 7.4 before_install: - composer self-update diff --git a/src/Promise/Declaration/Declaration/ReflectionDeclaration.php b/src/Promise/Declaration/Declaration/ReflectionDeclaration.php index 3720761..d3f6c27 100644 --- a/src/Promise/Declaration/Declaration/ReflectionDeclaration.php +++ b/src/Promise/Declaration/Declaration/ReflectionDeclaration.php @@ -12,16 +12,17 @@ namespace Cycle\ORM\Promise\Declaration\Declaration; use Cycle\ORM\Promise\Declaration\DeclarationInterface; +use ReflectionClass; final class ReflectionDeclaration implements DeclarationInterface { - /** @var \ReflectionClass */ + /** @var ReflectionClass */ private $reflection; /** - * @param \ReflectionClass $class + * @param ReflectionClass $class */ - public function __construct(\ReflectionClass $class) + public function __construct(ReflectionClass $class) { $this->reflection = $class; } diff --git a/src/Promise/Declaration/Declarations.php b/src/Promise/Declaration/Declarations.php index d8369f8..2ded229 100644 --- a/src/Promise/Declaration/Declarations.php +++ b/src/Promise/Declaration/Declarations.php @@ -11,13 +11,15 @@ namespace Cycle\ORM\Promise\Declaration; +use ReflectionClass; + final class Declarations { /** - * @param \ReflectionClass $parent + * @param ReflectionClass $parent * @return DeclarationInterface */ - public static function createParentFromReflection(\ReflectionClass $parent): DeclarationInterface + public static function createParentFromReflection(ReflectionClass $parent): DeclarationInterface { return new Declaration\ReflectionDeclaration($parent); } diff --git a/src/Promise/Declaration/Extractor.php b/src/Promise/Declaration/Extractor.php index be6b62e..9a30275 100644 --- a/src/Promise/Declaration/Extractor.php +++ b/src/Promise/Declaration/Extractor.php @@ -14,6 +14,8 @@ use Cycle\ORM\Promise\Declaration\Extractor\Constants; use Cycle\ORM\Promise\Declaration\Extractor\Methods; use Cycle\ORM\Promise\Declaration\Extractor\Properties; +use ReflectionClass; +use ReflectionException; final class Extractor { @@ -42,11 +44,11 @@ public function __construct( } /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @return Structure - * @throws \ReflectionException + * @throws ReflectionException */ - public function extract(\ReflectionClass $reflection): Structure + public function extract(ReflectionClass $reflection): Structure { return Structure::create( $this->constants->getConstants($reflection), @@ -57,10 +59,10 @@ public function extract(\ReflectionClass $reflection): Structure } /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @return bool */ - private function hasCloneMethod(\ReflectionClass $reflection): bool + private function hasCloneMethod(ReflectionClass $reflection): bool { if (!$reflection->hasMethod('__clone')) { return false; @@ -68,7 +70,7 @@ private function hasCloneMethod(\ReflectionClass $reflection): bool try { $cloneMethod = $reflection->getMethod('__clone'); - } catch (\ReflectionException $exception) { + } catch (ReflectionException $exception) { return false; } diff --git a/src/Promise/Declaration/Extractor/Constants.php b/src/Promise/Declaration/Extractor/Constants.php index 149cccf..eeea54a 100644 --- a/src/Promise/Declaration/Extractor/Constants.php +++ b/src/Promise/Declaration/Extractor/Constants.php @@ -11,13 +11,15 @@ namespace Cycle\ORM\Promise\Declaration\Extractor; +use ReflectionClass; + final class Constants { /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @return array */ - public function getConstants(\ReflectionClass $reflection): array + public function getConstants(ReflectionClass $reflection): array { $properties = []; diff --git a/src/Promise/Declaration/Extractor/Methods.php b/src/Promise/Declaration/Extractor/Methods.php index 80e8e4e..576d981 100644 --- a/src/Promise/Declaration/Extractor/Methods.php +++ b/src/Promise/Declaration/Extractor/Methods.php @@ -17,6 +17,11 @@ use PhpParser\Node; use PhpParser\Parser; use PhpParser\ParserFactory; +use ReflectionClass; +use ReflectionException; +use ReflectionMethod; +use ReflectionParameter; +use ReflectionType; final class Methods { @@ -58,11 +63,11 @@ public function __construct( } /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @return array - * @throws \ReflectionException + * @throws ReflectionException */ - public function getMethods(\ReflectionClass $reflection): array + public function getMethods(ReflectionClass $reflection): array { $parents = [$reflection->name => $reflection]; foreach ($reflection->getMethods() as $method) { @@ -91,10 +96,10 @@ public function getMethods(\ReflectionClass $reflection): array } /** - * @param \ReflectionMethod $method + * @param ReflectionMethod $method * @return bool */ - private function isIgnoredMethod(\ReflectionMethod $method): bool + private function isIgnoredMethod(ReflectionMethod $method): bool { return $method->isPrivate() || $method->isStatic() @@ -113,10 +118,10 @@ private function isMagicMethod(string $name): bool } /** - * @param \ReflectionMethod $method + * @param ReflectionMethod $method * @return int */ - private function packFlags(\ReflectionMethod $method): int + private function packFlags(ReflectionMethod $method): int { $flags = []; if ($method->isPublic()) { @@ -131,10 +136,10 @@ private function packFlags(\ReflectionMethod $method): int } /** - * @param \ReflectionMethod $method + * @param ReflectionMethod $method * @return Node|null */ - private function defineReturnType(\ReflectionMethod $method): ?Node + private function defineReturnType(ReflectionMethod $method): ?Node { if (!$method->hasReturnType()) { return null; @@ -161,21 +166,21 @@ private function defineReturnType(\ReflectionMethod $method): ?Node } /** - * @param \ReflectionMethod $method - * @param string $name + * @param ReflectionMethod $method + * @param string $name * @return string */ - private function replacedSelfReturnTypeName(\ReflectionMethod $method, string $name): string + private function replacedSelfReturnTypeName(ReflectionMethod $method, string $name): string { return $name === 'self' ? $method->class : $name; } /** - * @param \ReflectionType $returnType - * @param string $name + * @param ReflectionType $returnType + * @param string $name * @return bool */ - private function returnTypeShouldBeQualified(\ReflectionType $returnType, string $name): bool + private function returnTypeShouldBeQualified(ReflectionType $returnType, string $name): bool { if (in_array($name, self::RESERVED_UNQUALIFIED_RETURN_TYPES, true)) { return false; @@ -185,11 +190,11 @@ private function returnTypeShouldBeQualified(\ReflectionType $returnType, string } /** - * @param \ReflectionMethod $method + * @param ReflectionMethod $method * @return array - * @throws \ReflectionException + * @throws ReflectionException */ - private function packParams(\ReflectionMethod $method): array + private function packParams(ReflectionMethod $method): array { $params = []; foreach ($method->getParameters() as $parameter) { @@ -219,10 +224,10 @@ private function packParams(\ReflectionMethod $method): array } /** - * @param \ReflectionParameter $parameter + * @param ReflectionParameter $parameter * @return string|null */ - private function defineParamReturnType(\ReflectionParameter $parameter): ?string + private function defineParamReturnType(ReflectionParameter $parameter): ?string { if (!$parameter->hasType()) { return null; @@ -242,7 +247,7 @@ private function defineParamReturnType(\ReflectionParameter $parameter): ?string } /** - * @param \ReflectionClass[] $reflections + * @param ReflectionClass[] $reflections * @return Node\Stmt\ClassMethod[] */ private function getExtendedMethodNodes(array $reflections): array diff --git a/src/Promise/Declaration/Extractor/Properties.php b/src/Promise/Declaration/Extractor/Properties.php index 85bdcbe..89b4161 100644 --- a/src/Promise/Declaration/Extractor/Properties.php +++ b/src/Promise/Declaration/Extractor/Properties.php @@ -11,15 +11,19 @@ namespace Cycle\ORM\Promise\Declaration\Extractor; +use ReflectionClass; +use ReflectionProperty; +use SplObjectStorage; + final class Properties { /** - * @param \ReflectionClass $reflection - * @return \SplObjectStorage + * @param ReflectionClass $reflection + * @return SplObjectStorage */ - public function getProperties(\ReflectionClass $reflection): \SplObjectStorage + public function getProperties(ReflectionClass $reflection): SplObjectStorage { - $properties = new \SplObjectStorage(); + $properties = new SplObjectStorage(); $defaults = $reflection->getDefaultProperties(); foreach ($reflection->getProperties() as $property) { @@ -34,10 +38,10 @@ public function getProperties(\ReflectionClass $reflection): \SplObjectStorage } /** - * @param \ReflectionProperty $property + * @param ReflectionProperty $property * @return bool */ - private function isIgnoredProperty(\ReflectionProperty $property): bool + private function isIgnoredProperty(ReflectionProperty $property): bool { return $property->isPrivate() || $property->isStatic(); } diff --git a/src/Promise/Declaration/Structure.php b/src/Promise/Declaration/Structure.php index 2fcab58..42eb07b 100644 --- a/src/Promise/Declaration/Structure.php +++ b/src/Promise/Declaration/Structure.php @@ -12,6 +12,10 @@ namespace Cycle\ORM\Promise\Declaration; use PhpParser\Node\Stmt\ClassMethod; +use ReflectionProperty; +use SplObjectStorage; + +use function Cycle\ORM\Promise\phpVersionBetween; final class Structure { @@ -24,7 +28,7 @@ final class Structure /** @var bool */ public $hasClone; - /** @var \SplObjectStorage */ + /** @var SplObjectStorage */ private $properties; /** @@ -35,15 +39,15 @@ protected function __construct() } /** - * @param array $constants - * @param \SplObjectStorage $properties - * @param bool $hasClone - * @param ClassMethod ...$methods + * @param array $constants + * @param SplObjectStorage $properties + * @param bool $hasClone + * @param ClassMethod ...$methods * @return Structure */ public static function create( array $constants, - \SplObjectStorage $properties, + SplObjectStorage $properties, bool $hasClone, ClassMethod ...$methods ): Structure { @@ -57,14 +61,15 @@ public static function create( } /** + * A list of properties to be unset due to they are initialized (have a default value). * @return string[] */ public function toBeUnsetProperties(): array { $names = []; - /** @var \ReflectionProperty $property */ + /** @var ReflectionProperty $property */ foreach ($this->properties as $property) { - if ($this->properties[$property] === true && $property->isPublic()) { + if ($this->doesDefaultMatter($property) && $property->isPublic()) { $names[] = $property->getName(); } } @@ -73,12 +78,13 @@ public function toBeUnsetProperties(): array } /** + * A list of public properties. Any access to them be proxied. * @return string[] */ public function publicProperties(): array { $names = []; - /** @var \ReflectionProperty $property */ + /** @var ReflectionProperty $property */ foreach ($this->properties as $property) { if ($property->isPublic()) { $names[] = $property->getName(); @@ -94,7 +100,7 @@ public function publicProperties(): array public function properties(): array { $names = []; - /** @var \ReflectionProperty $property */ + /** @var ReflectionProperty $property */ foreach ($this->properties as $property) { $names[] = $property->getName(); } @@ -114,4 +120,15 @@ public function methodNames(): array return $names; } + + /** + * Since php7.4.1 the behaviour changed as it was before php7.4.0. All properties should be unset. + * @see https://github.com/php/php-src/pull/4974 + * @param ReflectionProperty $property + * @return bool + */ + private function doesDefaultMatter(ReflectionProperty $property): bool + { + return !phpVersionBetween('7.4.0', '7.4.1') || $this->properties[$property] === true; + } } diff --git a/src/Promise/Exception/ProxyFactoryException.php b/src/Promise/Exception/ProxyFactoryException.php index bdb6d61..2857ab8 100644 --- a/src/Promise/Exception/ProxyFactoryException.php +++ b/src/Promise/Exception/ProxyFactoryException.php @@ -11,13 +11,16 @@ namespace Cycle\ORM\Promise\Exception; -class ProxyFactoryException extends \Exception +use Exception; +use Throwable; + +class ProxyFactoryException extends Exception { /** - * @param \Throwable $e + * @param Throwable $e * @return ProxyFactoryException */ - public static function wrap(\Throwable $e): self + public static function wrap(Throwable $e): self { return new self($e->getMessage(), $e->getCode(), $e); } diff --git a/src/Promise/MaterializerInterface.php b/src/Promise/MaterializerInterface.php index fb9bd07..3abede8 100644 --- a/src/Promise/MaterializerInterface.php +++ b/src/Promise/MaterializerInterface.php @@ -11,12 +11,14 @@ namespace Cycle\ORM\Promise; +use ReflectionClass; + interface MaterializerInterface { /** - * @param string $code - * @param string $shortClassName - * @param \ReflectionClass $reflection + * @param string $code + * @param string $shortClassName + * @param ReflectionClass $reflection */ - public function materialize(string $code, string $shortClassName, \ReflectionClass $reflection): void; + public function materialize(string $code, string $shortClassName, ReflectionClass $reflection): void; } diff --git a/src/Promise/Materizalizer/EvalMaterializer.php b/src/Promise/Materizalizer/EvalMaterializer.php index 6c53fec..485259f 100644 --- a/src/Promise/Materizalizer/EvalMaterializer.php +++ b/src/Promise/Materizalizer/EvalMaterializer.php @@ -12,6 +12,7 @@ namespace Cycle\ORM\Promise\Materizalizer; use Cycle\ORM\Promise\MaterializerInterface; +use ReflectionClass; use function Cycle\ORM\Promise\trimPHPOpenTag; @@ -21,7 +22,7 @@ final class EvalMaterializer implements MaterializerInterface * {@inheritdoc} * If class already exists - do nothing (prevent from memory leaking) */ - public function materialize(string $code, string $shortClassName, \ReflectionClass $reflection): void + public function materialize(string $code, string $shortClassName, ReflectionClass $reflection): void { eval(trimPHPOpenTag($code)); } diff --git a/src/Promise/Materizalizer/FileMaterializer.php b/src/Promise/Materizalizer/FileMaterializer.php index e6928ab..ad4aa5f 100644 --- a/src/Promise/Materizalizer/FileMaterializer.php +++ b/src/Promise/Materizalizer/FileMaterializer.php @@ -12,6 +12,8 @@ namespace Cycle\ORM\Promise\Materizalizer; use Cycle\ORM\Promise\MaterializerInterface; +use Exception; +use ReflectionClass; use Spiral\Core\Container\SingletonInterface; use function Cycle\ORM\Promise\trimPHPOpenTag; @@ -39,9 +41,9 @@ public function __construct(ModificationInspector $inspector, string $directory) /** * {@inheritdoc} - * @throws \Exception + * @throws Exception */ - public function materialize(string $code, string $shortClassName, \ReflectionClass $reflection): void + public function materialize(string $code, string $shortClassName, ReflectionClass $reflection): void { $modifiedDate = $this->inspector->getLastModifiedDate($reflection); $filename = $this->makeFilename($shortClassName); diff --git a/src/Promise/Materizalizer/ModificationInspector.php b/src/Promise/Materizalizer/ModificationInspector.php index 4de934b..9db8069 100644 --- a/src/Promise/Materizalizer/ModificationInspector.php +++ b/src/Promise/Materizalizer/ModificationInspector.php @@ -11,15 +11,19 @@ namespace Cycle\ORM\Promise\Materizalizer; +use DateTime; +use Exception; +use ReflectionClass; + final class ModificationInspector { /** - * @param \ReflectionClass $reflection - * @return \DateTime + * @param ReflectionClass $reflection + * @return DateTime * - * @throws \Exception + * @throws Exception */ - public function getLastModifiedDate(\ReflectionClass $reflection): \DateTime + public function getLastModifiedDate(ReflectionClass $reflection): DateTime { $modifiedDate = $this->getLatestParentsModifiedDate($reflection); @@ -43,18 +47,18 @@ public function getLastModifiedDate(\ReflectionClass $reflection): \DateTime } /** - * @param \ReflectionClass $reflection - * @return \DateTime + * @param ReflectionClass $reflection + * @return DateTime * - * @throws \Exception + * @throws Exception */ - private function getLatestParentsModifiedDate(\ReflectionClass $reflection): \DateTime + private function getLatestParentsModifiedDate(ReflectionClass $reflection): DateTime { - $modifiedDate = new \DateTime('@' . filemtime($reflection->getFileName())); + $modifiedDate = new DateTime('@' . filemtime($reflection->getFileName())); $parent = $reflection->getParentClass(); while ($parent !== false) { - $parentsModifiedDate = new \DateTime('@' . filemtime($parent->getFileName())); + $parentsModifiedDate = new DateTime('@' . filemtime($parent->getFileName())); if ($parentsModifiedDate > $modifiedDate) { $modifiedDate = $parentsModifiedDate; diff --git a/src/Promise/Names.php b/src/Promise/Names.php index f853a24..a6e3a78 100644 --- a/src/Promise/Names.php +++ b/src/Promise/Names.php @@ -11,14 +11,16 @@ namespace Cycle\ORM\Promise; +use ReflectionClass; + final class Names { /** - * @param \ReflectionClass $reflection - * @param string|null $namespace + * @param ReflectionClass $reflection + * @param string|null $namespace * @return string */ - public function make(\ReflectionClass $reflection, ?string $namespace = null): string + public function make(ReflectionClass $reflection, ?string $namespace = null): string { $hash = hash('sha256', $reflection->name . $reflection->getFileName()); diff --git a/src/Promise/PHPDoc.php b/src/Promise/PHPDoc.php index cecfba9..11ea6ac 100644 --- a/src/Promise/PHPDoc.php +++ b/src/Promise/PHPDoc.php @@ -47,6 +47,6 @@ public static function writeProperty(string $type): Doc */ private static function makeComment($comment): Doc { - return new Doc(is_array($comment) ? join("\n", $comment) : $comment); + return new Doc(is_array($comment) ? implode("\n", $comment) : $comment); } } diff --git a/src/Promise/Printer.php b/src/Promise/Printer.php index bbb7374..856899c 100644 --- a/src/Promise/Printer.php +++ b/src/Promise/Printer.php @@ -19,6 +19,8 @@ use PhpParser\Parser; use PhpParser\PrettyPrinter\Standard; use PhpParser\PrettyPrinterAbstract; +use ReflectionClass; +use ReflectionException; final class Printer { @@ -101,15 +103,15 @@ public function __construct( } /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @param Declaration\DeclarationInterface $class * @param Declaration\DeclarationInterface $parent * @return string * @throws ProxyFactoryException - * @throws \ReflectionException + * @throws ReflectionException */ public function make( - \ReflectionClass $reflection, + ReflectionClass $reflection, Declaration\DeclarationInterface $class, Declaration\DeclarationInterface $parent ): string { diff --git a/src/Promise/ProxyFactory.php b/src/Promise/ProxyFactory.php index b5837cb..ea61281 100644 --- a/src/Promise/ProxyFactory.php +++ b/src/Promise/ProxyFactory.php @@ -18,7 +18,10 @@ use Cycle\ORM\Promise\Materizalizer\EvalMaterializer; use Cycle\ORM\PromiseFactoryInterface; use Cycle\ORM\Schema; +use Doctrine\Instantiator\Exception\ExceptionInterface; use Doctrine\Instantiator\Instantiator; +use ReflectionClass; +use ReflectionException; use Spiral\Core\Container\SingletonInterface; final class ProxyFactory implements PromiseFactoryInterface, SingletonInterface @@ -69,8 +72,8 @@ public function __construct( * @param array $scope * @return PromiseInterface * @throws ProxyFactoryException - * @throws \Doctrine\Instantiator\Exception\ExceptionInterface - * @throws \ReflectionException + * @throws ExceptionInterface + * @throws ReflectionException */ public function promise(ORMInterface $orm, string $role, array $scope): PromiseInterface { @@ -80,8 +83,8 @@ public function promise(ORMInterface $orm, string $role, array $scope): PromiseI } try { - $reflection = new \ReflectionClass($class); - } catch (\ReflectionException $e) { + $reflection = new ReflectionClass($class); + } catch (ReflectionException $e) { throw ProxyFactoryException::wrap($e); } @@ -106,17 +109,17 @@ public function promise(ORMInterface $orm, string $role, array $scope): PromiseI } /** - * @param \ReflectionClass $reflection - * @param string $className - * @param ORMInterface $orm - * @param string $role - * @param array $scope + * @param ReflectionClass $reflection + * @param string $className + * @param ORMInterface $orm + * @param string $role + * @param array $scope * @return PromiseInterface - * @throws \Doctrine\Instantiator\Exception\ExceptionInterface - * @throws \ReflectionException + * @throws ExceptionInterface + * @throws ReflectionException */ private function instantiate( - \ReflectionClass $reflection, + ReflectionClass $reflection, string $className, ORMInterface $orm, string $role, diff --git a/src/Promise/Visitor/AddPropertiesConst.php b/src/Promise/Visitor/AddPropertiesConst.php index dba565d..8085aec 100644 --- a/src/Promise/Visitor/AddPropertiesConst.php +++ b/src/Promise/Visitor/AddPropertiesConst.php @@ -17,7 +17,7 @@ use function Cycle\ORM\Promise\inject; /** - * Add "unset properties" property + * Add const with properties list */ final class AddPropertiesConst extends NodeVisitorAbstract { @@ -63,13 +63,11 @@ private function buildProperty(): Node\Stmt\ClassConst $array[] = new Node\Expr\ArrayItem(new Node\Scalar\String_($value)); } - $const = new Node\Stmt\ClassConst([ + return new Node\Stmt\ClassConst([ new Node\Const_( $this->name, new Node\Expr\Array_($array, ['kind' => Node\Expr\Array_::KIND_SHORT]) ) ], Node\Stmt\Class_::MODIFIER_PRIVATE); - - return $const; } } diff --git a/src/Promise/Visitor/AddProxiedMethods.php b/src/Promise/Visitor/AddProxiedMethods.php index 9598b2a..9fdc386 100644 --- a/src/Promise/Visitor/AddProxiedMethods.php +++ b/src/Promise/Visitor/AddProxiedMethods.php @@ -152,7 +152,7 @@ private function modifyMethodMethod(Node\Stmt\ClassMethod $method, string $stmtW private function packMethodArgs(Node\Stmt\ClassMethod $method): array { $args = []; - /** @var \PhpParser\Node\Param $param */ + /** @var Node\Param $param */ foreach ($method->getParams() as $param) { $args[] = (new Param($param->var->name))->getNode(); } diff --git a/src/Promise/Visitor/DeclareClass.php b/src/Promise/Visitor/DeclareClass.php index 0748bcb..de1629b 100644 --- a/src/Promise/Visitor/DeclareClass.php +++ b/src/Promise/Visitor/DeclareClass.php @@ -63,7 +63,7 @@ public function leaveNode(Node $node) private function canBeImplemented(Node\Stmt\Class_ $node): bool { foreach ($node->implements as $implement) { - $name = join('\\', $implement->parts); + $name = implode('\\', $implement->parts); if ($name === $this->implements) { return false; } diff --git a/src/functions/utils.php b/src/functions/utils.php index 9994610..bb40641 100644 --- a/src/functions/utils.php +++ b/src/functions/utils.php @@ -23,7 +23,7 @@ function getStubContent(): string 'class ProxyStub {}' ]; - return join("\n", $lines); + return implode("\n", $lines); } /** diff --git a/src/functions/version.php b/src/functions/version.php index ee4b2b0..deddf8f 100644 --- a/src/functions/version.php +++ b/src/functions/version.php @@ -12,11 +12,13 @@ namespace Cycle\ORM\Promise; /** + * @param string $v1 + * @param string $v2 * @return bool */ -function php74(): bool +function phpVersionBetween(string $v1, string $v2): bool { - return version_compare(phpVersion(), '7.4.0', '>='); + return version_compare(phpVersion(), $v1, '>=') && version_compare(phpVersion(), $v2, '<'); } /** diff --git a/tests/Promise/BaseTest.php b/tests/Promise/BaseTest.php index 61fd469..837819b 100644 --- a/tests/Promise/BaseTest.php +++ b/tests/Promise/BaseTest.php @@ -14,6 +14,7 @@ use Cycle\ORM\Config\RelationConfig; use Cycle\ORM\Factory; use Cycle\ORM\ORM; +use Cycle\ORM\ORMInterface; use Cycle\ORM\SchemaInterface; use PHPUnit\Framework\TestCase; use Spiral\Database\Config\DatabaseConfig; @@ -111,7 +112,7 @@ public function tearDown(): void * Calculates missing parameters for typecasting. * * @param SchemaInterface $schema - * @return ORM|\Cycle\ORM\ORMInterface + * @return ORM|ORMInterface */ public function withSchema(SchemaInterface $schema) { diff --git a/tests/Promise/ConflictResolver/ConflictResolverTest.php b/tests/Promise/ConflictResolver/ConflictResolverTest.php index 2828acb..b8311fd 100644 --- a/tests/Promise/ConflictResolver/ConflictResolverTest.php +++ b/tests/Promise/ConflictResolver/ConflictResolverTest.php @@ -14,6 +14,7 @@ use Cycle\ORM\Promise\ConflictResolver; use PHPUnit\Framework\TestCase; use Spiral\Core\Container; +use Throwable; class ConflictResolverTest extends TestCase { @@ -23,7 +24,7 @@ class ConflictResolverTest extends TestCase * @param array $reserved * @param array $input * @param array $expected - * @throws \Throwable + * @throws Throwable */ public function testFind(array $reserved, array $input, array $expected): void { @@ -51,8 +52,8 @@ public function cdProvider(): array } /** - * @return \Cycle\ORM\Promise\ConflictResolver - * @throws \Throwable + * @return ConflictResolver + * @throws Throwable */ private function conflictResolver(): ConflictResolver { diff --git a/tests/Promise/ConflictResolver/SequenceTest.php b/tests/Promise/ConflictResolver/SequenceTest.php index 32e66fb..3cf9168 100644 --- a/tests/Promise/ConflictResolver/SequenceTest.php +++ b/tests/Promise/ConflictResolver/SequenceTest.php @@ -14,6 +14,7 @@ use Cycle\ORM\Promise\ConflictResolver\Sequences; use PHPUnit\Framework\TestCase; use Spiral\Core\Container; +use Throwable; class SequenceTest extends TestCase { @@ -23,7 +24,7 @@ class SequenceTest extends TestCase * @param array $sequence * @param int $pos * @param int $expected - * @throws \Throwable + * @throws Throwable */ public function testFind(array $sequence, int $pos, int $expected): void { @@ -50,8 +51,8 @@ public function findProvider(): array } /** - * @return \Cycle\ORM\Promise\ConflictResolver\Sequences - * @throws \Throwable + * @return Sequences + * @throws Throwable */ private function sequences(): Sequences { diff --git a/tests/Promise/Declaration/DeclarationTest.php b/tests/Promise/Declaration/DeclarationTest.php index 441059d..3550dcc 100644 --- a/tests/Promise/Declaration/DeclarationTest.php +++ b/tests/Promise/Declaration/DeclarationTest.php @@ -13,18 +13,20 @@ use Cycle\ORM\Promise\Declaration\Declarations; use Cycle\ORM\Promise\Tests\Declaration\Fixtures\HasNamespaceExample; +use Example; use PHPUnit\Framework\TestCase; +use ReflectionClass; class DeclarationTest extends TestCase { /** * @dataProvider nameProvider * - * @param \ReflectionClass $reflection - * @param string $name - * @param string $expected + * @param ReflectionClass $reflection + * @param string $name + * @param string $expected */ - public function testShortName(\ReflectionClass $reflection, string $name, string $expected): void + public function testShortName(ReflectionClass $reflection, string $name, string $expected): void { $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($name, $parent); @@ -34,7 +36,7 @@ public function testShortName(\ReflectionClass $reflection, string $name, string public function nameProvider(): array { - $r = new \ReflectionClass(\Example::class); + $r = new ReflectionClass(Example::class); return [ [$r, 'ExampleProxy\\', 'ExampleProxy'], @@ -47,11 +49,11 @@ public function nameProvider(): array /** * @dataProvider namespaceProvider * - * @param \ReflectionClass $reflection - * @param string $name - * @param string|null $expected + * @param ReflectionClass $reflection + * @param string $name + * @param string|null $expected */ - public function testNamespace(\ReflectionClass $reflection, string $name, ?string $expected): void + public function testNamespace(ReflectionClass $reflection, string $name, ?string $expected): void { $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($name, $parent); @@ -61,8 +63,8 @@ public function testNamespace(\ReflectionClass $reflection, string $name, ?strin public function namespaceProvider(): array { - $r1 = new \ReflectionClass(\Example::class); - $r2 = new \ReflectionClass(HasNamespaceExample::class); + $r1 = new ReflectionClass(Example::class); + $r2 = new ReflectionClass(HasNamespaceExample::class); return [ [$r1, 'ExampleProxy\\', null], @@ -79,11 +81,11 @@ public function namespaceProvider(): array /** * @dataProvider fullNameProvider * - * @param \ReflectionClass $reflection - * @param string $name - * @param string|null $expected + * @param ReflectionClass $reflection + * @param string $name + * @param string|null $expected */ - public function testFullName(\ReflectionClass $reflection, string $name, ?string $expected): void + public function testFullName(ReflectionClass $reflection, string $name, ?string $expected): void { $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($name, $parent); @@ -93,8 +95,8 @@ public function testFullName(\ReflectionClass $reflection, string $name, ?string public function fullNameProvider(): array { - $r1 = new \ReflectionClass(\Example::class); - $r2 = new \ReflectionClass(HasNamespaceExample::class); + $r1 = new ReflectionClass(Example::class); + $r2 = new ReflectionClass(HasNamespaceExample::class); return [ [$r1, 'ExampleProxy', '\ExampleProxy'], diff --git a/tests/Promise/Declaration/ExtractorTest.php b/tests/Promise/Declaration/ExtractorTest.php index 55b070b..cf63110 100644 --- a/tests/Promise/Declaration/ExtractorTest.php +++ b/tests/Promise/Declaration/ExtractorTest.php @@ -18,13 +18,16 @@ use Cycle\ORM\Promise\Tests\Fixtures\ChildEntity; use Cycle\ORM\Promise\Tests\Fixtures\ParentEntity; use PHPUnit\Framework\TestCase; +use ReflectionClass; +use ReflectionException; use Spiral\Core\Container; +use Throwable; class ExtractorTest extends TestCase { /** - * @throws \ReflectionException - * @throws \Throwable + * @throws ReflectionException + * @throws Throwable */ public function testExtractProperties(): void { @@ -38,8 +41,8 @@ public function testExtractProperties(): void } /** - * @throws \ReflectionException - * @throws \Throwable + * @throws ReflectionException + * @throws Throwable */ public function testExtractParentMethods(): void { @@ -53,9 +56,9 @@ public function testExtractParentMethods(): void } /** - * @throws \ReflectionException - * @throws \Throwable - * @throws \Throwable + * @throws ReflectionException + * @throws Throwable + * @throws Throwable */ public function testExtractMethods(): void { @@ -70,8 +73,8 @@ public function testExtractMethods(): void } /** - * @throws \ReflectionException - * @throws \Throwable + * @throws ReflectionException + * @throws Throwable */ public function testSelfReturnTypes(): void { @@ -87,18 +90,18 @@ public function testSelfReturnTypes(): void /** * @param string $class - * @return \Cycle\ORM\Promise\Declaration\Structure - * @throws \ReflectionException - * @throws \Throwable + * @return Structure + * @throws ReflectionException + * @throws Throwable */ private function getDeclaration(string $class): Structure { - return $this->extractor()->extract(new \ReflectionClass($class)); + return $this->extractor()->extract(new ReflectionClass($class)); } /** - * @return \Cycle\ORM\Promise\Declaration\Extractor - * @throws \Throwable + * @return Extractor + * @throws Throwable */ private function extractor(): Extractor { diff --git a/tests/Promise/FactoryTest.php b/tests/Promise/FactoryTest.php index 23ca9f0..d5ebca3 100644 --- a/tests/Promise/FactoryTest.php +++ b/tests/Promise/FactoryTest.php @@ -17,18 +17,22 @@ use Cycle\ORM\Promise\MaterializerInterface; use Cycle\ORM\Promise\Materizalizer\EvalMaterializer; use Cycle\ORM\Promise\Materizalizer\FileMaterializer; +use Cycle\ORM\Promise\PromiseInterface; use Cycle\ORM\Promise\ProxyFactory; +use Cycle\ORM\Promise\Resolver; use Cycle\ORM\Promise\Tests\Fixtures\SchematicEntity; use Cycle\ORM\Transaction; use Cycle\Schema; +use PDO; use Spiral\Core\Container; use Spiral\Database\Driver\SQLite\SQLiteDriver; +use Throwable; class FactoryTest extends BaseTest { public const DRIVER = 'sqlite'; - /** @var \Spiral\Core\Container */ + /** @var Container */ private $container; public function setUp(): void @@ -40,7 +44,7 @@ public function setUp(): void 'sqlite' => [ 'driver' => SQLiteDriver::class, 'check' => static function () { - return !in_array('sqlite', \PDO::getAvailableDrivers(), true); + return !in_array('sqlite', PDO::getAvailableDrivers(), true); }, 'conn' => 'sqlite::memory:', 'user' => 'sqlite', @@ -67,8 +71,8 @@ public function setUp(): void * @param string $materializer * @param array $params * - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws Throwable */ public function testPromise(string $materializer, array $params): void { @@ -84,7 +88,7 @@ public function testPromise(string $materializer, array $params): void $this->bindMaterializer($this->container->make($materializer, $params)); - /** @var SchematicEntity|\Cycle\ORM\Promise\Resolver $promise */ + /** @var SchematicEntity|Resolver $promise */ $promise = $this->factory()->promise($orm, $role, $scope); $this->assertInstanceOf($role, $promise); @@ -118,7 +122,7 @@ public function testPromise(string $materializer, array $params): void * @param array $params * * @throws ProxyFactoryException - * @throws \Throwable + * @throws Throwable */ public function testNullScope(string $materializer, array $params): void { @@ -134,7 +138,7 @@ public function testNullScope(string $materializer, array $params): void $this->bindMaterializer($this->container->make($materializer, $params)); - /** @var SchematicEntity|\Cycle\ORM\Promise\PromiseInterface $promise */ + /** @var SchematicEntity|PromiseInterface $promise */ $promise = $this->factory()->promise($orm, $role, $scope); $this->assertInstanceOf($role, $promise); @@ -150,7 +154,7 @@ public function testNullScope(string $materializer, array $params): void * @param array $params * * @throws ProxyFactoryException - * @throws \Throwable + * @throws Throwable */ public function testUnknownScope(string $materializer, array $params): void { @@ -166,7 +170,7 @@ public function testUnknownScope(string $materializer, array $params): void $this->bindMaterializer($this->container->make($materializer, $params)); - /** @var SchematicEntity|\Cycle\ORM\Promise\PromiseInterface $promise */ + /** @var SchematicEntity|PromiseInterface $promise */ $promise = $this->factory()->promise($orm, $role, $scope); $this->assertInstanceOf($role, $promise); @@ -182,7 +186,7 @@ public function testUnknownScope(string $materializer, array $params): void * @param array $params * * @throws ProxyFactoryException - * @throws \Throwable + * @throws Throwable */ public function testUnknownProperty(string $materializer, array $params): void { @@ -198,7 +202,7 @@ public function testUnknownProperty(string $materializer, array $params): void $this->bindMaterializer($this->container->make($materializer, $params)); - /** @var SchematicEntity|\Cycle\ORM\Promise\PromiseInterface $promise */ + /** @var SchematicEntity|PromiseInterface $promise */ $promise = $this->factory()->promise($orm, $role, $scope); $this->assertInstanceOf($role, $promise); @@ -236,8 +240,8 @@ private function orm(): ORMInterface } /** - * @return \Cycle\ORM\Promise\ProxyFactory - * @throws \Throwable + * @return ProxyFactory + * @throws Throwable */ private function factory(): ProxyFactory { diff --git a/tests/Promise/MaterializerTest.php b/tests/Promise/MaterializerTest.php index f6253cc..ac5c2ff 100644 --- a/tests/Promise/MaterializerTest.php +++ b/tests/Promise/MaterializerTest.php @@ -14,8 +14,12 @@ use Cycle\ORM\Promise\Materizalizer\EvalMaterializer; use Cycle\ORM\Promise\Materizalizer\FileMaterializer; use Cycle\ORM\Promise\Tests\Fixtures\Entity; +use Exception; use PHPUnit\Framework\TestCase; +use ReflectionClass; +use ReflectionException; use Spiral\Core\Container; +use Throwable; class MaterializerTest extends TestCase { @@ -37,14 +41,14 @@ public function setUp(): void * @param string $className * @param string $code * - * @throws \ReflectionException + * @throws ReflectionException */ public function testEvalMaterialize(string $className, string $code): void { $this->assertFalse(class_exists($className)); $materializer = new EvalMaterializer(); - $materializer->materialize($code, $className, new \ReflectionClass(Entity::class)); + $materializer->materialize($code, $className, new ReflectionClass(Entity::class)); $this->assertTrue(class_exists($className)); } @@ -64,9 +68,9 @@ public function evalDataProvider(): array * @param string $className * @param string $code * - * @throws \ReflectionException - * @throws \Exception - * @throws \Throwable + * @throws ReflectionException + * @throws Exception + * @throws Throwable */ public function testFileMaterializer(string $className, string $code): void { @@ -76,7 +80,7 @@ public function testFileMaterializer(string $className, string $code): void $container = new Container(); /** @var FileMaterializer $materializer */ $materializer = $container->make(FileMaterializer::class, ['directory' => $this->filesDirectory()]); - $materializer->materialize($code, $className, new \ReflectionClass(Entity::class)); + $materializer->materialize($code, $className, new ReflectionClass(Entity::class)); $this->assertTrue(class_exists($fullClassName)); } diff --git a/tests/Promise/ModificationInspectorTest.php b/tests/Promise/ModificationInspectorTest.php index d303e37..3b3915f 100644 --- a/tests/Promise/ModificationInspectorTest.php +++ b/tests/Promise/ModificationInspectorTest.php @@ -13,17 +13,22 @@ use Cycle\ORM\Promise\Materizalizer\ModificationInspector; use Cycle\ORM\Promise\Tests\Fixtures\ModificationInspector\Inspected; +use DateTime; +use Exception; use PHPUnit\Framework\TestCase; +use ReflectionClass; +use ReflectionException; use Spiral\Core\Container; +use Throwable; class ModificationInspectorTest extends TestCase { /** - * @throws \ReflectionException - * @throws \Exception - * @throws \Exception - * @throws \Throwable + * @throws ReflectionException + * @throws Exception + * @throws Exception + * @throws Throwable */ public function testDate(): void { @@ -32,14 +37,14 @@ public function testDate(): void foreach ($files as $file) { if (is_file($file)) { - $date = new \DateTime('@' . filemtime($file)); + $date = new DateTime('@' . filemtime($file)); if ($date > $lastDate) { $lastDate = $date; } } } - $this->assertEquals($lastDate, $this->inspector()->getLastModifiedDate(new \ReflectionClass(Inspected::class))); + $this->assertEquals($lastDate, $this->inspector()->getLastModifiedDate(new ReflectionClass(Inspected::class))); } private function filesDirectory(): string @@ -48,8 +53,8 @@ private function filesDirectory(): string } /** - * @return \Cycle\ORM\Promise\Materizalizer\ModificationInspector - * @throws \Throwable + * @return ModificationInspector + * @throws Throwable */ private function inspector(): ModificationInspector { diff --git a/tests/Promise/ProxyPrinter/BaseProxyPrinterTest.php b/tests/Promise/ProxyPrinter/BaseProxyPrinterTest.php index 7f64655..689df51 100644 --- a/tests/Promise/ProxyPrinter/BaseProxyPrinterTest.php +++ b/tests/Promise/ProxyPrinter/BaseProxyPrinterTest.php @@ -14,20 +14,25 @@ use Cycle\Annotated\Entities; use Cycle\ORM\ORMInterface; use Cycle\ORM\Promise\Declaration\DeclarationInterface; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\Printer; use Cycle\ORM\Promise\Tests\BaseTest; use Cycle\Schema; +use PDO; use PhpParser\PrettyPrinter\Standard; use PhpParser\PrettyPrinterAbstract; +use ReflectionClass; +use ReflectionException; use Spiral\Core\Container; use Spiral\Database\Driver\SQLite\SQLiteDriver; +use Throwable; abstract class BaseProxyPrinterTest extends BaseTest { public const DRIVER = 'sqlite'; protected const NS = 'Cycle\ORM\Promise\Tests\Promises'; - /** @var \Spiral\Core\Container */ + /** @var Container */ protected $container; public function setUp(): void @@ -39,7 +44,7 @@ public function setUp(): void 'sqlite' => [ 'driver' => SQLiteDriver::class, 'check' => static function () { - return !in_array('sqlite', \PDO::getAvailableDrivers(), true); + return !in_array('sqlite', PDO::getAvailableDrivers(), true); }, 'conn' => 'sqlite::memory:', 'user' => 'sqlite', @@ -54,16 +59,16 @@ public function setUp(): void } /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @param DeclarationInterface $class * @param DeclarationInterface $parent * @return string - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ protected function make( - \ReflectionClass $reflection, + ReflectionClass $reflection, DeclarationInterface $class, DeclarationInterface $parent ): string { @@ -87,8 +92,8 @@ private function orm(): ORMInterface } /** - * @return \Cycle\ORM\Promise\Printer - * @throws \Throwable + * @return Printer + * @throws Throwable */ private function proxyCreator(): Printer { diff --git a/tests/Promise/ProxyPrinter/ConstantsTest.php b/tests/Promise/ProxyPrinter/ConstantsTest.php index 4ec9af2..be51667 100644 --- a/tests/Promise/ProxyPrinter/ConstantsTest.php +++ b/tests/Promise/ProxyPrinter/ConstantsTest.php @@ -12,20 +12,24 @@ namespace Cycle\ORM\Promise\Tests\ProxyPrinter; use Cycle\ORM\Promise\Declaration\Declarations; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\Printer; +use ReflectionClass; +use ReflectionException; +use Throwable; class ConstantsTest extends BaseProxyPrinterTest { /** - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ public function testConstValues(): void { $classname = Fixtures\EntityWithProperties::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -45,15 +49,15 @@ public function testConstValues(): void } /** - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ public function testWithoutConflicts(): void { $classname = Fixtures\EntityWithoutConstConflicts::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -70,22 +74,22 @@ public function testWithoutConflicts(): void eval($output); - $reflection = new \ReflectionClass($as); + $reflection = new ReflectionClass($as); $this->assertArrayHasKey('PUBLIC_CONST', $reflection->getConstants()); $this->assertArrayHasKey('PROTECTED_CONST', $reflection->getConstants()); $this->assertArrayHasKey(Printer::PUBLIC_PROPERTIES_CONST, $reflection->getConstants()); } /** - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ public function testWithConflicts(): void { $classname = Fixtures\EntityWithConstConflicts::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -103,7 +107,7 @@ public function testWithConflicts(): void eval($output); - $reflection = new \ReflectionClass($as); + $reflection = new ReflectionClass($as); $this->assertArrayHasKey('PUBLIC_CONST', $reflection->getConstants()); $this->assertArrayHasKey('PROTECTED_CONST', $reflection->getConstants()); $this->assertArrayHasKey(Printer::PUBLIC_PROPERTIES_CONST, $reflection->getConstants()); diff --git a/tests/Promise/ProxyPrinter/DeclarationTest.php b/tests/Promise/ProxyPrinter/DeclarationTest.php index 283104e..2f3370f 100644 --- a/tests/Promise/ProxyPrinter/DeclarationTest.php +++ b/tests/Promise/ProxyPrinter/DeclarationTest.php @@ -12,23 +12,27 @@ namespace Cycle\ORM\Promise\Tests\ProxyPrinter; use Cycle\ORM\Promise\Declaration\Declarations; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\PromiseInterface; +use ReflectionClass; +use ReflectionException; +use Throwable; use function Cycle\ORM\Promise\shortName; class DeclarationTest extends BaseProxyPrinterTest { /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testDeclaration(): void { $classname = Fixtures\Entity::class; $as = 'EntityProxy' . __LINE__; - $r = new \ReflectionClass($classname); + $r = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($r); $class = Declarations::createClassFromName($as, $parent); $output = $this->make($r, $class, $parent); @@ -59,13 +63,13 @@ public function testDeclaration(): void * @param string $classname * @param string $as * - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testTraits(string $classname, string $as): void { - $r = new \ReflectionClass($classname); + $r = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($r); $class = Declarations::createClassFromName($as, $parent); $this->assertStringNotContainsString(' use ', $this->make($r, $class, $parent)); @@ -83,7 +87,7 @@ public function traitsDataProvider(): array * @param string $className * @param string $proxyFullName * @return object - * @throws \Throwable + * @throws Throwable */ private function makeProxyObject(string $className, string $proxyFullName) { diff --git a/tests/Promise/ProxyPrinter/Fixtures/ConflictEntity.php b/tests/Promise/ProxyPrinter/Fixtures/ConflictEntity.php index 734b205..ecfedaf 100644 --- a/tests/Promise/ProxyPrinter/Fixtures/ConflictEntity.php +++ b/tests/Promise/ProxyPrinter/Fixtures/ConflictEntity.php @@ -13,7 +13,7 @@ class ConflictEntity { - protected const UNSET_PROPERTIES = 4; + protected const UNSET_PROPERTIES = 4; protected const PUBLIC_PROPERTIES = 4; protected $resolver; diff --git a/tests/Promise/ProxyPrinter/Methods/MethodArgsTest.php b/tests/Promise/ProxyPrinter/Methods/MethodArgsTest.php index 251a0c5..c2e7ce3 100644 --- a/tests/Promise/ProxyPrinter/Methods/MethodArgsTest.php +++ b/tests/Promise/ProxyPrinter/Methods/MethodArgsTest.php @@ -5,14 +5,18 @@ namespace Cycle\ORM\Promise\Tests\ProxyPrinter\Methods; use Cycle\ORM\Promise\Declaration\Declarations; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\Tests\ProxyPrinter\BaseProxyPrinterTest; +use ReflectionClass; +use ReflectionException; +use Throwable; class MethodArgsTest extends BaseProxyPrinterTest { /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testHasArgType(): void { @@ -22,9 +26,9 @@ public function testHasArgType(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testArgDefaults(): void { @@ -35,9 +39,9 @@ public function testArgDefaults(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testVariadicArg(): void { @@ -47,9 +51,9 @@ public function testVariadicArg(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testReferencedArg(): void { @@ -62,13 +66,13 @@ public function testReferencedArg(): void * @param string $classname * @param string $as * @return string - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ private function makeOutput(string $classname, string $as): string { - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); diff --git a/tests/Promise/ProxyPrinter/Methods/MethodsTest.php b/tests/Promise/ProxyPrinter/Methods/MethodsTest.php index 35b0b84..c09b412 100644 --- a/tests/Promise/ProxyPrinter/Methods/MethodsTest.php +++ b/tests/Promise/ProxyPrinter/Methods/MethodsTest.php @@ -14,22 +14,29 @@ use Cycle\ORM\Promise\Declaration\Declarations; use Cycle\ORM\Promise\Declaration\Extractor; use Cycle\ORM\Promise\Declaration\Structure; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\PromiseInterface; use Cycle\ORM\Promise\Tests\ProxyPrinter\BaseProxyPrinterTest; +use PhpParser\Node\Stmt\ClassMethod; +use ReflectionClass; +use ReflectionException; +use ReflectionMethod; use Spiral\Core\Container; +use Throwable; +use UnexpectedValueException; class MethodsTest extends BaseProxyPrinterTest { /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testPromiseMethods(): void { $classname = Fixtures\EntityWithMethods::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -42,7 +49,7 @@ public function testPromiseMethods(): void eval($output); $methods = []; - $reflection = new \ReflectionClass($as); + $reflection = new ReflectionClass($as); foreach ($reflection->getMethods() as $method) { $methods[$method->name] = $method->name; } @@ -55,16 +62,16 @@ public function testPromiseMethods(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable + * @throws Throwable */ public function testInheritedMethods(): void { $classname = Fixtures\ChildEntityWithMethods::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -78,7 +85,7 @@ public function testInheritedMethods(): void //There're only public and protected methods inside (not static and not final) $originMethods = []; - foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) { + foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED) as $method) { if ($method->isStatic() || $method->isFinal()) { continue; } @@ -117,25 +124,25 @@ public function testInheritedMethods(): void "Proxied method `{$name}` expected to be public" ); } else { - throw new \UnexpectedValueException("\"{$method->name->toString()}\" method not found"); + throw new UnexpectedValueException("\"{$method->name->toString()}\" method not found"); } } } /** - * @param \ReflectionClass $class - * @return \Cycle\ORM\Promise\Declaration\Structure - * @throws \ReflectionException - * @throws \Throwable + * @param ReflectionClass $class + * @return Structure + * @throws ReflectionException + * @throws Throwable */ - private function getStructure(\ReflectionClass $class): Structure + private function getStructure(ReflectionClass $class): Structure { return $this->extractor()->extract($class); } /** - * @return \Cycle\ORM\Promise\Declaration\Extractor - * @throws \Throwable + * @return Extractor + * @throws Throwable */ private function extractor(): Extractor { @@ -146,15 +153,15 @@ private function extractor(): Extractor /** * @param string $classname - * @return \PhpParser\Node\Stmt\ClassMethod[] - * @throws \ReflectionException - * @throws \Throwable + * @return ClassMethod[] + * @throws ReflectionException + * @throws Throwable */ private function promiseMethods(string $classname): array { $interfaceMethods = $this->interfaceMethods(); $methods = []; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); foreach ($this->getStructure($reflection)->methods as $method) { if (isset($interfaceMethods[$method->name->name]) || $method->isMagic()) { continue; @@ -169,7 +176,7 @@ private function promiseMethods(string $classname): array private function interfaceMethods(): array { $methods = []; - $reflection = new \ReflectionClass(PromiseInterface::class); + $reflection = new ReflectionClass(PromiseInterface::class); foreach ($reflection->getMethods() as $method) { $methods[$method->name] = $method->name; } diff --git a/tests/Promise/ProxyPrinter/Methods/ReturnStmtTest.php b/tests/Promise/ProxyPrinter/Methods/ReturnStmtTest.php index c45894d..b8d4eac 100644 --- a/tests/Promise/ProxyPrinter/Methods/ReturnStmtTest.php +++ b/tests/Promise/ProxyPrinter/Methods/ReturnStmtTest.php @@ -12,20 +12,24 @@ namespace Cycle\ORM\Promise\Tests\ProxyPrinter\Methods; use Cycle\ORM\Promise\Declaration\Declarations; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\Tests\ProxyPrinter\BaseProxyPrinterTest; +use ReflectionClass; +use ReflectionException; +use Throwable; class ReturnStmtTest extends BaseProxyPrinterTest { /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testSetter(): void { $classname = Fixtures\ChildFixture::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -41,15 +45,15 @@ public function testSetter(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testGetter(): void { $classname = Fixtures\ChildFixture::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -65,15 +69,15 @@ public function testGetter(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testConditionalReturn(): void { $classname = Fixtures\ChildFixture::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -88,15 +92,15 @@ public function testConditionalReturn(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testVoidReturn(): void { $classname = Fixtures\ChildFixture::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -112,15 +116,15 @@ public function testVoidReturn(): void } /** - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ public function testRefReturn(): void { $classname = Fixtures\ChildFixture::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); diff --git a/tests/Promise/ProxyPrinter/NamespaceTest.php b/tests/Promise/ProxyPrinter/NamespaceTest.php index c33a23c..7f9a1c8 100644 --- a/tests/Promise/ProxyPrinter/NamespaceTest.php +++ b/tests/Promise/ProxyPrinter/NamespaceTest.php @@ -12,19 +12,23 @@ namespace Cycle\ORM\Promise\Tests\ProxyPrinter; use Cycle\ORM\Promise\Declaration\Declarations; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; +use ReflectionClass; +use ReflectionException; +use Throwable; class NamespaceTest extends BaseProxyPrinterTest { /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testSameNamespace(): void { $classname = Fixtures\EntityInNamespace::class; $as = 'EntityProxy' . str_replace('\\', '', __CLASS__) . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -36,20 +40,20 @@ public function testSameNamespace(): void eval($output); - $proxyReflection = new \ReflectionClass($class->getFullName()); + $proxyReflection = new ReflectionClass($class->getFullName()); $this->assertSame($reflection->getNamespaceName(), $proxyReflection->getNamespaceName()); } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testDistinctNamespace(): void { $classname = Fixtures\EntityInNamespace::class; $as = '\\EntityProxy' . str_replace('\\', '', __CLASS__) . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -61,7 +65,7 @@ public function testDistinctNamespace(): void eval($output); - $proxyReflection = new \ReflectionClass($class->getFullName()); + $proxyReflection = new ReflectionClass($class->getFullName()); $this->assertSame('', (string)$proxyReflection->getNamespaceName()); $this->assertStringNotContainsString('namespace ', $output); } diff --git a/tests/Promise/ProxyPrinter/PropertiesTest.php b/tests/Promise/ProxyPrinter/PropertiesTest.php index a475b5f..a5638cf 100644 --- a/tests/Promise/ProxyPrinter/PropertiesTest.php +++ b/tests/Promise/ProxyPrinter/PropertiesTest.php @@ -12,20 +12,25 @@ namespace Cycle\ORM\Promise\Tests\ProxyPrinter; use Cycle\ORM\Promise\Declaration\Declarations; +use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\Tests\ProxyPrinter\Fixtures; +use ReflectionClass; +use ReflectionException; +use ReflectionProperty; +use Throwable; class PropertiesTest extends BaseProxyPrinterTest { /** - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ public function testWithoutConflicts(): void { $classname = Fixtures\EntityWithoutPropConflicts::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -43,9 +48,9 @@ public function testWithoutConflicts(): void eval($output); - $reflection = new \ReflectionClass($class->getFullName()); + $reflection = new ReflectionClass($class->getFullName()); - /** @var \ReflectionProperty[] $properties */ + /** @var ReflectionProperty[] $properties */ $properties = []; foreach ($reflection->getProperties() as $property) { $properties[$property->name] = $property; @@ -62,15 +67,15 @@ public function testWithoutConflicts(): void } /** - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ public function testWithConflicts(): void { $classname = Fixtures\EntityWithPropConflicts::class; $as = self::NS . __CLASS__ . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -89,9 +94,9 @@ public function testWithConflicts(): void eval($output); - $reflection = new \ReflectionClass($class->getFullName()); + $reflection = new ReflectionClass($class->getFullName()); - /** @var \ReflectionProperty[] $properties */ + /** @var ReflectionProperty[] $properties */ $properties = []; foreach ($reflection->getProperties() as $property) { $properties[$property->name] = $property; diff --git a/tests/Promise/ProxyPrinter/UseStmtsTest.php b/tests/Promise/ProxyPrinter/UseStmtsTest.php index c1b6225..69878db 100644 --- a/tests/Promise/ProxyPrinter/UseStmtsTest.php +++ b/tests/Promise/ProxyPrinter/UseStmtsTest.php @@ -16,6 +16,9 @@ use Cycle\ORM\Promise\Exception\ProxyFactoryException; use Cycle\ORM\Promise\PromiseInterface; use Cycle\ORM\Promise\Resolver; +use ReflectionClass; +use ReflectionException; +use Throwable; class UseStmtsTest extends BaseProxyPrinterTest { @@ -27,15 +30,15 @@ class UseStmtsTest extends BaseProxyPrinterTest ]; /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testSameNamespace(): void { $classname = Fixtures\Entity::class; $as = 'EntityProxy' . str_replace('\\', '', __CLASS__) . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -53,15 +56,15 @@ public function testSameNamespace(): void } /** - * @throws \ReflectionException - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \Throwable + * @throws ReflectionException + * @throws ProxyFactoryException + * @throws Throwable */ public function testDistinctNamespace(): void { $classname = Fixtures\Entity::class; $as = "\EntityProxy" . str_replace('\\', '', __CLASS__) . __LINE__; - $reflection = new \ReflectionClass($classname); + $reflection = new ReflectionClass($classname); $parent = Declarations::createParentFromReflection($reflection); $class = Declarations::createClassFromName($as, $parent); @@ -98,11 +101,11 @@ private function fetchUseStatements(string $code): array * @param string $class * @param array $types * @return array - * @throws \ReflectionException + * @throws ReflectionException */ private function fetchExternalDependencies(string $class, array $types = []): array { - $reflection = new \ReflectionClass($class); + $reflection = new ReflectionClass($class); foreach ($reflection->getConstructor()->getParameters() as $parameter) { if (!$parameter->hasType() || $parameter->getType()->isBuiltin()) { diff --git a/tests/fixtures/promises/FileTestThree.php b/tests/fixtures/promises/FileTestThree.php index 39d8217..507b3e5 100644 --- a/tests/fixtures/promises/FileTestThree.php +++ b/tests/fixtures/promises/FileTestThree.php @@ -1,3 +1,2 @@ assertStringContainsString( - "UNSET_PROPERTIES = ['publicProperty', 'publicPropertyWithDefaults'];", + phpVersionBetween('7.4.0', '7.4.1') + ? "UNSET_PROPERTIES = ['publicProperty', 'publicPropertyWithDefaults'];" + : "UNSET_PROPERTIES = ['publicProperty', 'publicTypedProperty', 'publicPropertyWithDefaults'];", $output ); } /** - * @param \ReflectionClass $reflection + * @param ReflectionClass $reflection * @param DeclarationInterface $class * @param DeclarationInterface $parent * @return string - * @throws \Cycle\ORM\Promise\Exception\ProxyFactoryException - * @throws \ReflectionException - * @throws \Throwable + * @throws ProxyFactoryException + * @throws ReflectionException + * @throws Throwable */ protected function make( - \ReflectionClass $reflection, + ReflectionClass $reflection, DeclarationInterface $class, DeclarationInterface $parent ): string { @@ -79,8 +91,8 @@ protected function make( } /** - * @return \Cycle\ORM\Promise\Printer - * @throws \Throwable + * @return Printer + * @throws Throwable */ private function proxyCreator(): Printer {