Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Oct 2, 2019
2 parents 1481c79 + dea64fb commit 4d8696c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
[![Build Status](https://travis-ci.org/cycle/proxy-factory.svg?branch=master)](https://travis-ci.org/cycle/proxy-factory)
[![Codecov](https://codecov.io/gh/cycle/proxy-factory/branch/master/graph/badge.svg)](https://codecov.io/gh/cycle/proxy-factory/)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cycle/proxy-factory/badges/quality-score.png)](https://scrutinizer-ci.com/g/cycle/proxy-factory/)

License:
--------
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained by [Spiral Scout](https://spiralscout.com).
33 changes: 30 additions & 3 deletions src/Declaration/Extractor/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class Methods
{
private const MAGIC_METHOD_NAMES = [
private const MAGIC_METHODS = [
'__construct',
'__destruct',
'__call',
Expand All @@ -31,6 +31,8 @@ final class Methods
'__debugInfo',
];

private const RESERVED_UNQUALIFIED_RETURN_TYPES = ['self', 'static', 'object'];

/**
* @param \ReflectionClass $reflection
* @return array
Expand Down Expand Up @@ -70,7 +72,7 @@ private function isIgnoredMethod(\ReflectionMethod $method): bool
*/
private function isMagicMethod(string $name): bool
{
return in_array($name, self::MAGIC_METHOD_NAMES, true);
return in_array($name, self::MAGIC_METHODS, true);
}

/**
Expand Down Expand Up @@ -108,8 +110,9 @@ private function defineReturnType(\ReflectionMethod $method): ?Node
}

$name = $returnType->getName();
$name = $this->replacedSelfReturnTypeName($method, $name);

if (!$returnType->isBuiltin()) {
if ($this->returnTypeShouldBeQualified($returnType, $name)) {
$name = '\\' . $name;
}

Expand All @@ -120,6 +123,30 @@ private function defineReturnType(\ReflectionMethod $method): ?Node
return new Node\Identifier($name);
}

/**
* @param \ReflectionMethod $method
* @param string $name
* @return string
*/
private function replacedSelfReturnTypeName(\ReflectionMethod $method, string $name): string
{
return $name === 'self' ? $method->class : $name;
}

/**
* @param \ReflectionType $returnType
* @param string $name
* @return bool
*/
private function returnTypeShouldBeQualified(\ReflectionType $returnType, string $name): bool
{
if (in_array($name, self::RESERVED_UNQUALIFIED_RETURN_TYPES, true)) {
return false;
}

return !$returnType->isBuiltin();
}

/**
* @param \ReflectionMethod $method
* @return array
Expand Down
13 changes: 13 additions & 0 deletions tests/Promise/Declaration/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cycle\ORM\Promise\Tests\Declaration\Fixtures\Entity;
use Cycle\ORM\Promise\Tests\Declaration\Fixtures\EntityWithConstructor;
use Cycle\ORM\Promise\Tests\Fixtures\ChildEntity;
use Cycle\ORM\Promise\Tests\Fixtures\ParentEntity;
use PHPUnit\Framework\TestCase;
use Spiral\Core\Container;

Expand Down Expand Up @@ -47,6 +48,18 @@ public function testExtractMethods(): void
$this->assertSame(['public', 'protected'], $this->getDeclaration(EntityWithConstructor::class)->properties);
}

public function testSelfReturnTypes(): void
{
$extracted = [];
foreach ($this->getDeclaration(ChildEntity::class)->methods as $method) {
$extracted[$method->name->name] = $method->returnType->name;
}

$this->assertNotContains('self', $extracted);
$this->assertContains('\\' . ChildEntity::class, $extracted);
$this->assertContains('\\' . ParentEntity::class, $extracted);
}

private function getDeclaration(string $class): Structure
{
return $this->extractor()->extract(new \ReflectionClass($class));
Expand Down
28 changes: 17 additions & 11 deletions tests/Promise/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ private function filesDirectory(): string
public function testPromise(string $materializer, array $params): void
{
$role = SchematicEntity::class;
$this->orm()->make($role, ['id' => 1, 'name' => 'my name']);
$this->orm()->make($role, ['id' => 2, 'name' => 'my second name', 'email' => 'my email']);
$orm = $this->orm();
$orm->make($role, ['id' => 1, 'name' => 'my name']);
$orm->make($role, ['id' => 2, 'name' => 'my second name', 'email' => 'my email']);

$tr = new Transaction($orm);
$tr->run();

$scope = ['id' => 2];

$this->bindMaterializer($this->container->make($materializer, $params));

/** @var SchematicEntity|\Cycle\ORM\Promise\Resolver $promise */
$promise = $this->factory()->promise($this->orm(), $role, $scope);
$promise = $this->factory()->promise($orm, $role, $scope);

$this->assertInstanceOf($role, $promise);
$this->assertNotNull($promise->__resolve());
Expand All @@ -89,12 +93,12 @@ public function testPromise(string $materializer, array $params): void

$promise->email = 'my second email';

$tr = new Transaction($this->orm());
$tr = new Transaction($orm);
$tr->persist($promise);
$tr->run();

/** @var SchematicEntity $o */
$o = $this->orm()->get($role, ['id' => 2]);
$o = $orm->get($role, ['id' => 2]);
$this->assertEquals('my third name', $o->getName());
$this->assertEquals('my second email', $o->email);

Expand All @@ -116,15 +120,16 @@ public function testPromise(string $materializer, array $params): void
public function testNullScope(string $materializer, array $params): void
{
$role = SchematicEntity::class;
$this->orm()->make($role, ['id' => 1, 'name' => 'my name']);
$this->orm()->make($role, ['id' => 2, 'name' => 'my second name', 'email' => 'my email']);
$orm = $this->orm();
$orm->make($role, ['id' => 1, 'name' => 'my name']);
$orm->make($role, ['id' => 2, 'name' => 'my second name', 'email' => 'my email']);

$scope = [];

$this->bindMaterializer($this->container->make($materializer, $params));

/** @var SchematicEntity|\Cycle\ORM\Promise\PromiseInterface $promise */
$promise = $this->factory()->promise($this->orm(), $role, $scope);
$promise = $this->factory()->promise($orm, $role, $scope);

$this->assertInstanceOf($role, $promise);
$this->assertNull($promise->__resolve());
Expand All @@ -145,15 +150,16 @@ public function testNullScope(string $materializer, array $params): void
public function testUnknownScope(string $materializer, array $params): void
{
$role = SchematicEntity::class;
$this->orm()->make($role, ['id' => 1, 'name' => 'my name']);
$this->orm()->make($role, ['id' => 2, 'name' => 'my second name', 'email' => 'my email']);
$orm = $this->orm();
$orm->make($role, ['id' => 1, 'name' => 'my name']);
$orm->make($role, ['id' => 2, 'name' => 'my second name', 'email' => 'my email']);

$scope = ['id' => 3];

$this->bindMaterializer($this->container->make($materializer, $params));

/** @var SchematicEntity|\Cycle\ORM\Promise\PromiseInterface $promise */
$promise = $this->factory()->promise($this->orm(), $role, $scope);
$promise = $this->factory()->promise($orm, $role, $scope);

$this->assertInstanceOf($role, $promise);
$this->assertNull($promise->__resolve());
Expand Down
5 changes: 5 additions & 0 deletions tests/Promise/Fixtures/ChildEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public function childProp(): string
return 'childPropValue';
}

public function childSelf(): self
{
return $this;
}

protected function childProtectedProp(): \stdClass
{
return new \stdClass();
Expand Down
5 changes: 5 additions & 0 deletions tests/Promise/Fixtures/ParentEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ protected function parentProtectedProp(): string
{
return 'childParentPropValue';
}

public function parentSelf(): self
{
return $this;
}
}

0 comments on commit 4d8696c

Please sign in to comment.