Skip to content

Commit 2345403

Browse files
author
Valentin V / vvval
committed
#8 fix method params namespace issue
1 parent 69c0dd6 commit 2345403

File tree

3 files changed

+79
-50
lines changed

3 files changed

+79
-50
lines changed

src/Promise/Declaration/Extractor/Methods.php

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -145,48 +145,9 @@ private function defineReturnType(ReflectionMethod $method): ?Node
145145
return null;
146146
}
147147

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

150-
if ($returnType === null) {
151-
return null;
152-
}
153-
154-
$name = $returnType->getName();
155-
$name = $this->replacedSelfReturnTypeName($method, $name);
156-
157-
if ($this->returnTypeShouldBeQualified($returnType, $name)) {
158-
$name = '\\' . $name;
159-
}
160-
161-
if ($returnType->allowsNull()) {
162-
$name = '?' . $name;
163-
}
164-
165-
return new Node\Identifier($name);
166-
}
167-
168-
/**
169-
* @param ReflectionMethod $method
170-
* @param string $name
171-
* @return string
172-
*/
173-
private function replacedSelfReturnTypeName(ReflectionMethod $method, string $name): string
174-
{
175-
return $name === 'self' ? $method->class : $name;
176-
}
177-
178-
/**
179-
* @param ReflectionType $returnType
180-
* @param string $name
181-
* @return bool
182-
*/
183-
private function returnTypeShouldBeQualified(ReflectionType $returnType, string $name): bool
184-
{
185-
if (in_array($name, self::RESERVED_UNQUALIFIED_RETURN_TYPES, true)) {
186-
return false;
187-
}
188-
189-
return !$returnType->isBuiltin();
150+
return $name !== null ? new Node\Identifier($name) : null;
190151
}
191152

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

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

226187
/**
227188
* @param ReflectionParameter $parameter
189+
* @param ReflectionMethod $method
228190
* @return string|null
229191
*/
230-
private function defineParamReturnType(ReflectionParameter $parameter): ?string
192+
private function defineParamType(ReflectionParameter $parameter, ReflectionMethod $method): ?string
231193
{
232194
if (!$parameter->hasType()) {
233195
return null;
234196
}
235197

236-
$typeReflection = $parameter->getType();
237-
if ($typeReflection === null) {
198+
return $this->defineType($method, $parameter->getType());
199+
}
200+
201+
/**
202+
* @param ReflectionMethod $method
203+
* @param ReflectionType|null $type
204+
* @return string|null
205+
*/
206+
private function defineType(ReflectionMethod $method, ?ReflectionType $type): ?string
207+
{
208+
if ($type === null) {
238209
return null;
239210
}
240211

241-
$type = $typeReflection->getName();
242-
if ($typeReflection->allowsNull()) {
243-
$type = "?$type";
212+
$name = $type->getName();
213+
$name = $this->replacedSelfTypeName($method, $name);
214+
215+
if ($this->typeShouldBeQualified($type, $name)) {
216+
$name = '\\' . $name;
217+
}
218+
219+
if ($type->allowsNull()) {
220+
$name = "?$name";
244221
}
245222

246-
return $type;
223+
return $name;
224+
}
225+
226+
/**
227+
* @param ReflectionMethod $method
228+
* @param string $name
229+
* @return string
230+
*/
231+
private function replacedSelfTypeName(ReflectionMethod $method, string $name): string
232+
{
233+
return $name === 'self' ? $method->class : $name;
234+
}
235+
236+
/**
237+
* @param ReflectionType $returnType
238+
* @param string $name
239+
* @return bool
240+
*/
241+
private function typeShouldBeQualified(ReflectionType $returnType, string $name): bool
242+
{
243+
if (in_array($name, self::RESERVED_UNQUALIFIED_RETURN_TYPES, true)) {
244+
return false;
245+
}
246+
247+
return !$returnType->isBuiltin();
247248
}
248249

249250
/**

tests/Promise/ProxyPrinter/Methods/Fixtures/ArgsFixture.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

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

8+
use Cycle\ORM\Promise\Tests\ProxyPrinter;
9+
use Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures\ChildFixture as Child;
10+
811
class ArgsFixture
912
{
1013
public function referencedSetter(string $a, &$b, int $c): void
@@ -23,4 +26,28 @@ public function defaultsSetter(string $a, $b = [], int $c = 3, bool $d): void
2326
public function variadicSetter($a, string ...$b): void
2427
{
2528
}
29+
30+
public function shortNameSetter(ChildFixture $child): ChildFixture
31+
{
32+
return new Child();
33+
}
34+
35+
public function halfNameSetter(ProxyPrinter\Methods\Fixtures\ChildFixture $child): ProxyPrinter\Methods\Fixtures\ChildFixture
36+
{
37+
return new Child();
38+
}
39+
40+
public function longNameSetter(\Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures\ChildFixture $child): \Cycle\ORM\Promise\Tests\ProxyPrinter\Methods\Fixtures\ChildFixture
41+
{
42+
return new Child();
43+
}
44+
45+
public function aliasNameSetter(Child $child): ?Child
46+
{
47+
return new Child();
48+
}
49+
50+
public function selfSetter(self $child): void
51+
{
52+
}
2653
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<?php
2-
namespace Cycle\ORM\Promise\Tests\Promises; class FileTestThree {}
2+
// phpcs:ignoreFile
3+
namespace Cycle\ORM\Promise\Tests\Promises; class FileTestThree {}

0 commit comments

Comments
 (0)