Skip to content

Commit

Permalink
Merge pull request #16 from cycle/php8.0
Browse files Browse the repository at this point in the history
Php8.0
  • Loading branch information
vvval authored Jan 20, 2021
2 parents 952fd29 + c6a3189 commit f07abe7
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4']
php-versions: ['7.2', '7.3', '7.4', '8.0']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"psr-4": {
"Cycle\\ORM\\Promise\\Tests\\": "tests/Promise/",
"Cycle\\ORM\\Promise\\Tests74\\": "tests/php74/",
"Cycle\\ORM\\Promise\\Tests80\\": "tests/php80/",
"Cycle\\ORM\\Promise\\Tests\\Promises\\": "tests/fixtures/promises/",
"": "tests/fixtures/withoutNamespace/"
}
Expand Down
5 changes: 4 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
<testsuite name="Proxy Factory">
<directory>./tests/Promise/</directory>
</testsuite>
<testsuite name="Proxy Factory 74">
<testsuite name="Proxy Factory 7.4">
<directory phpVersion="7.4.0-dev" phpVersionOperator="ge">./tests/php74/</directory>
</testsuite>
<testsuite name="Proxy Factory 8.0">
<directory phpVersion="8.0.0-dev" phpVersionOperator="ge">./tests/php80/</directory>
</testsuite>
</testsuites>

<filter>
Expand Down
30 changes: 27 additions & 3 deletions src/Promise/Declaration/Extractor/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionType;

Expand Down Expand Up @@ -208,18 +209,41 @@ private function defineParamType(ReflectionParameter $parameter, ReflectionMetho
*/
private function defineType(ReflectionMethod $method, ?ReflectionType $type): ?string
{
if ($type === null) {
return null;
if ($type instanceof ReflectionNamedType) {
return $this->defineSingularType($method, $type);
}

if ($type instanceof \ReflectionUnionType) {
$types = array_map(
function (ReflectionNamedType $type) use ($method): ?string {
return $this->defineSingularType($method, $type);
},
$type->getTypes()
);

if ($types) {
return implode('|', array_filter($types));
}
}

return null;
}

/**
* @param ReflectionMethod $method
* @param ReflectionNamedType $type
* @return string|null
*/
private function defineSingularType(ReflectionMethod $method, ReflectionNamedType $type): ?string
{
$name = $type->getName();
$name = $this->replacedSelfTypeName($method, $name);

if ($this->typeShouldBeQualified($type, $name)) {
$name = '\\' . $name;
}

if ($type->allowsNull()) {
if ($name !== 'mixed' && $type->allowsNull()) {
$name = "?$name";
}

Expand Down
9 changes: 7 additions & 2 deletions src/Promise/Materizalizer/ModificationInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public function getLastModifiedDate(ReflectionClass $reflection): DateTime
*/
private function getLatestParentsModifiedDate(ReflectionClass $reflection): DateTime
{
$modifiedDate = new DateTime('@' . filemtime($reflection->getFileName()));
$modifiedDate = new DateTime('@' . $this->getDatetime($reflection));

$parent = $reflection->getParentClass();
while ($parent !== false) {
$parentsModifiedDate = new DateTime('@' . filemtime($parent->getFileName()));
$parentsModifiedDate = new DateTime('@' . $this->getDatetime($reflection));

if ($parentsModifiedDate > $modifiedDate) {
$modifiedDate = $parentsModifiedDate;
Expand All @@ -69,4 +69,9 @@ private function getLatestParentsModifiedDate(ReflectionClass $reflection): Date

return $modifiedDate;
}

private function getDatetime(ReflectionClass $reflection)
{
return $reflection->getFileName() ? filemtime($reflection->getFileName()) : time();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function typedSetter(string $a, $b, int $c): void
}


public function defaultsSetter(string $a, $b = [], int $c = 3, bool $d): void
public function defaultsSetter(string $a, $b = [], int $c = 3, ?bool $d = null): void
{
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Promise/ProxyPrinter/Methods/MethodArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public function testArgDefaults(): void
$output = $this->makeOutput(Fixtures\ArgsFixture::class, self::NS . __CLASS__ . __LINE__);

//Long syntax by default
$this->assertStringContainsString('defaultsSetter(string $a, $b = array(), int $c = 3, bool $d)', $output);
$this->assertStringContainsString(
'defaultsSetter(string $a, $b = array(), int $c = 3, ?bool $d = null)',
$output
);
}

/**
Expand Down
92 changes: 92 additions & 0 deletions tests/php80/ConstantsPHP80Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Spiral Framework. Cycle ProxyFactory
*
* @license MIT
* @author Valentin V (Vvval)
*/

declare(strict_types=1);

namespace Cycle\ORM\Promise\Tests80;

use Cycle\ORM\Promise\Declaration\DeclarationInterface;
use Cycle\ORM\Promise\Declaration\Declarations;
use Cycle\ORM\Promise\Exception\ProxyFactoryException;
use Cycle\ORM\Promise\Printer;
use PhpParser\PrettyPrinter\Standard;
use PhpParser\PrettyPrinterAbstract;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionException;
use Spiral\Core\Container;
use Throwable;

use function Cycle\ORM\Promise\phpVersionBetween;

class ConstantsPHP80Test extends TestCase
{
protected const NS = 'Cycle\ORM\Promise\Tests80\Promises';

/** @var Container */
protected $container;

public function setUp(): void
{
parent::setUp();

$this->container = new Container();
}

/**
* @throws ProxyFactoryException
* @throws ReflectionException
* @throws Throwable
*/
public function testConstValues(): void
{
$classname = Fixtures\EntityWithUnionTypes::class;
$as = self::NS . __CLASS__ . __LINE__;
$reflection = new ReflectionClass($classname);

$parent = Declarations::createParentFromReflection($reflection);
$class = Declarations::createClassFromName($as, $parent);

$output = $this->make($reflection, $class, $parent);
$output = ltrim($output, '<?php');

$this->assertStringContainsString(
"PUBLIC_PROPERTIES = ['throwable'];",
$output
);
}

/**
* @param ReflectionClass $reflection
* @param DeclarationInterface $class
* @param DeclarationInterface $parent
* @return string
* @throws ProxyFactoryException
* @throws ReflectionException
* @throws Throwable
*/
protected function make(
ReflectionClass $reflection,
DeclarationInterface $class,
DeclarationInterface $parent
): string {
return $this->proxyCreator()->make($reflection, $class, $parent);
}

/**
* @return Printer
* @throws Throwable
*/
private function proxyCreator(): Printer
{
$this->container->bind(PrettyPrinterAbstract::class, Standard::class);

return $this->container->get(Printer::class);
}
}
20 changes: 20 additions & 0 deletions tests/php80/Fixtures/EntityWithMixedArg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Spiral Framework. Cycle ProxyFactory
*
* @license MIT
* @author Valentin V (Vvval)
*/

declare(strict_types=1);

namespace Cycle\ORM\Promise\Tests80\Fixtures;

class EntityWithMixedArg
{
public function method(mixed $arg): mixed
{
return $arg;
}
}
22 changes: 22 additions & 0 deletions tests/php80/Fixtures/EntityWithUnionTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Spiral Framework. Cycle ProxyFactory
*
* @license MIT
* @author Valentin V (Vvval)
*/

declare(strict_types=1);

namespace Cycle\ORM\Promise\Tests80\Fixtures;

class EntityWithUnionTypes
{
public \Exception | \Error $throwable;

public function method(\Exception | \Error $arg): \Exception | \Error
{
return $arg;
}
}
72 changes: 72 additions & 0 deletions tests/php80/MethodsPHP80Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Promise\Tests80;

use Cycle\ORM\Promise\Declaration\Declarations;
use Cycle\ORM\Promise\Exception\ProxyFactoryException;
use Cycle\ORM\Promise\Tests\ProxyPrinter\BaseProxyPrinterTest;
use Cycle\ORM\Promise\Tests80\Fixtures;
use ReflectionClass;
use ReflectionException;
use Throwable;

class MethodsPHP80Test extends BaseProxyPrinterTest
{
/**
* @throws ProxyFactoryException
* @throws ReflectionException
* @throws Throwable
*/
public function testUnionTypes(): void
{
$output = $this->makeOutput(Fixtures\EntityWithUnionTypes::class, self::NS . __CLASS__ . __LINE__);

$this->assertStringContainsString(
'public function method(\Exception|\Error $arg): \Exception|\Error',
$output
);
}

/**
* @throws ReflectionException
* @throws ProxyFactoryException
* @throws Throwable
*/
public function testNullableMixedArg(): void
{
$output = $this->makeOutput(Fixtures\EntityWithMixedArg::class, self::NS . __CLASS__ . __LINE__);

$this->assertStringContainsString(
'public function method(mixed $arg): mixed',
$output
);
}

/**
* @param string $classname
* @param string $as
* @return string
* @throws ReflectionException
* @throws ProxyFactoryException
* @throws Throwable
*/
private function makeOutput(string $classname, string $as): string
{
$reflection = new ReflectionClass($classname);

$parent = Declarations::createParentFromReflection($reflection);
$class = Declarations::createClassFromName($as, $parent);

$output = $this->make($reflection, $class, $parent);
$output = ltrim($output, '<?php');
$output = str_replace(') : ', '): ', $output);

$this->assertFalse(class_exists($class->getFullName()));

eval($output);

return $output;
}
}

0 comments on commit f07abe7

Please sign in to comment.