Skip to content

Commit

Permalink
#8 fix method params namespace issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin V / vvval committed Jan 8, 2020
1 parent 69c0dd6 commit 2345403
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 50 deletions.
99 changes: 50 additions & 49 deletions src/Promise/Declaration/Extractor/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,48 +145,9 @@ private function defineReturnType(ReflectionMethod $method): ?Node
return null;
}

$returnType = $method->getReturnType();
$name = $this->defineType($method, $method->getReturnType());

if ($returnType === null) {
return null;
}

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

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

if ($returnType->allowsNull()) {
$name = '?' . $name;
}

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();
return $name !== null ? new Node\Identifier($name) : null;
}

/**
Expand All @@ -212,7 +173,7 @@ private function packParams(ReflectionMethod $method): array
$param->makeByRef();
}

$type = $this->defineParamReturnType($parameter);
$type = $this->defineParamType($parameter, $method);
if ($type !== null) {
$param->setType($type);
}
Expand All @@ -225,25 +186,65 @@ private function packParams(ReflectionMethod $method): array

/**
* @param ReflectionParameter $parameter
* @param ReflectionMethod $method
* @return string|null
*/
private function defineParamReturnType(ReflectionParameter $parameter): ?string
private function defineParamType(ReflectionParameter $parameter, ReflectionMethod $method): ?string
{
if (!$parameter->hasType()) {
return null;
}

$typeReflection = $parameter->getType();
if ($typeReflection === null) {
return $this->defineType($method, $parameter->getType());
}

/**
* @param ReflectionMethod $method
* @param ReflectionType|null $type
* @return string|null
*/
private function defineType(ReflectionMethod $method, ?ReflectionType $type): ?string
{
if ($type === null) {
return null;
}

$type = $typeReflection->getName();
if ($typeReflection->allowsNull()) {
$type = "?$type";
$name = $type->getName();
$name = $this->replacedSelfTypeName($method, $name);

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

if ($type->allowsNull()) {
$name = "?$name";
}

return $type;
return $name;
}

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

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

return !$returnType->isBuiltin();
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Promise/ProxyPrinter/Methods/Fixtures/ArgsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures;

use Cycle\ORM\Promise\Tests\ProxyPrinter;
use Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures\ChildFixture as Child;

class ArgsFixture
{
public function referencedSetter(string $a, &$b, int $c): void
Expand All @@ -23,4 +26,28 @@ public function defaultsSetter(string $a, $b = [], int $c = 3, bool $d): void
public function variadicSetter($a, string ...$b): void
{
}

public function shortNameSetter(ChildFixture $child): ChildFixture
{
return new Child();
}

public function halfNameSetter(ProxyPrinter\Methods\Fixtures\ChildFixture $child): ProxyPrinter\Methods\Fixtures\ChildFixture
{
return new Child();
}

public function longNameSetter(\Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures\ChildFixture $child): \Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures\ChildFixture
{
return new Child();
}

public function aliasNameSetter(Child $child): ?Child
{
return new Child();
}

public function selfSetter(self $child): void
{
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/promises/FileTestThree.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<?php
namespace Cycle\ORM\Promise\Tests\Promises; class FileTestThree {}
// phpcs:ignoreFile
namespace Cycle\ORM\Promise\Tests\Promises; class FileTestThree {}

0 comments on commit 2345403

Please sign in to comment.