Skip to content

Commit

Permalink
Merge pull request #7 from cycle/feature/php7.4.1-compatibility
Browse files Browse the repository at this point in the history
Add compatibility with php7.4.1
  • Loading branch information
vvval authored Dec 24, 2019
2 parents 923b5a1 + 972049d commit eadb908
Show file tree
Hide file tree
Showing 42 changed files with 431 additions and 307 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- "7.2"
- "7.3"
- "7.4snapshot"
- 7.2
- 7.3
- 7.4

before_install:
- composer self-update
Expand Down
7 changes: 4 additions & 3 deletions src/Promise/Declaration/Declaration/ReflectionDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Promise/Declaration/Declarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 8 additions & 6 deletions src/Promise/Declaration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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),
Expand All @@ -57,18 +59,18 @@ 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;
}

try {
$cloneMethod = $reflection->getMethod('__clone');
} catch (\ReflectionException $exception) {
} catch (ReflectionException $exception) {
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Promise/Declaration/Extractor/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
47 changes: 26 additions & 21 deletions src/Promise/Declaration/Extractor/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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()) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/Promise/Declaration/Extractor/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down
37 changes: 27 additions & 10 deletions src/Promise/Declaration/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -24,7 +28,7 @@ final class Structure
/** @var bool */
public $hasClone;

/** @var \SplObjectStorage */
/** @var SplObjectStorage */
private $properties;

/**
Expand All @@ -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 {
Expand All @@ -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();
}
}
Expand All @@ -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();
Expand All @@ -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();
}
Expand All @@ -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;
}
}
Loading

0 comments on commit eadb908

Please sign in to comment.