Skip to content

Commit 4d76dde

Browse files
authored
chore: refacto extractor (#9)
1 parent c437554 commit 4d76dde

15 files changed

+40
-47
lines changed

Exception/MissingTypeException.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public static function createForProperty(\ReflectionProperty $property): self
2424
return new self(sprintf('%s::$%s', $property->getDeclaringClass()->getName(), $property->getName()), 'property');
2525
}
2626

27-
// TODO ReturnType -> FunctionReturn
28-
2927
public static function createForFunctionReturn(\ReflectionFunctionAbstract $function): self
3028
{
3129
/** @var \ReflectionClass<object>|null $declaringClass */

Hook/Marshal/PropertyHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private function name(\ReflectionProperty $property, string $propertyIdentifier,
6767
private function type(\ReflectionProperty $property, ?\ReflectionFunction $propertyFormatter, array $context): string
6868
{
6969
return null !== $propertyFormatter
70-
? $this->typeExtractor->extractFromReturnType($propertyFormatter)
70+
? $this->typeExtractor->extractFromFunctionReturn($propertyFormatter)
7171
: $this->typeExtractor->extractFromProperty($property);
7272
}
7373

Hook/Marshal/TypeHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function type(string $type, ?\ReflectionFunction $typeFormatter, array $
4848
$currentPropertyClass = $context['symfony']['marshal']['current_property_class'] ?? null;
4949

5050
if (null !== $typeFormatter) {
51-
$type = $this->typeExtractor->extractFromReturnType($typeFormatter);
51+
$type = $this->typeExtractor->extractFromFunctionReturn($typeFormatter);
5252

5353
// if method doesn't belong to the current class, ignore generic search
5454
if ($typeFormatter->getClosureScopeClass()?->getName() !== $currentPropertyClass) {

Hook/Unmarshal/PropertyHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __invoke(\ReflectionClass $class, object $object, string $key, c
3939
$propertyFormatterReflection = new \ReflectionFunction($propertyFormatter);
4040
$this->validateFormatter($propertyFormatterReflection, $propertyIdentifier);
4141

42-
$valueType = $this->typeExtractor->extractFromParameter($propertyFormatterReflection->getParameters()[0]);
42+
$valueType = $this->typeExtractor->extractFromFunctionParameter($propertyFormatterReflection->getParameters()[0]);
4343
}
4444

4545
$valueType ??= $this->typeExtractor->extractFromProperty(new \ReflectionProperty($object, $propertyName));

Internal/Parser/Parser.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ private function instantiateObject(\ReflectionClass $reflection): object
180180
continue;
181181
}
182182

183-
// TODO throw or collect depending on context (like in AbstractNormalizer)
184-
185183
return $reflection->newInstanceWithoutConstructor();
186184
}
187185

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
- tests (unmarshal from component)
1414
- create dedicated exceptions and wrap native ones
1515
- try catch property assignment (to not have TypeError)
16+
- same for object construction (like in serializer)
1617
- collect errors mode (optional) -> throw at the end with errors and decoded
1718
object
18-
- not internal exceptions (move it to public folder)
1919
- bench in CI
2020

2121
## Questions

Tests/Hook/Marshal/PropertyHookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testUpdateTypeAccessorAndContextFromFormatter(string $expectedTy
5959
{
6060
$typeExtractor = $this->createStub(TypeExtractorInterface::class);
6161
$typeExtractor->method('extractFromProperty')->willReturn('int');
62-
$typeExtractor->method('extractFromReturnType')->willReturn('string');
62+
$typeExtractor->method('extractFromFunctionReturn')->willReturn('string');
6363

6464
$context = [
6565
'symfony' => [

Tests/Hook/Marshal/TypeHookTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class TypeHookTest extends TestCase
2626
public function testUpdateTypeAndAccessorFromFormatter(string $expectedType, string $expectedAccessor, string $formatterReturnType, array $typeFormatters): void
2727
{
2828
$typeExtractor = $this->createStub(TypeExtractorInterface::class);
29-
$typeExtractor->method('extractFromReturnType')->willReturn($formatterReturnType);
29+
$typeExtractor->method('extractFromFunctionReturn')->willReturn($formatterReturnType);
3030

3131
$context = [
3232
'symfony' => [
@@ -74,7 +74,7 @@ public function testConvertGenericTypes(): void
7474
public function testDoNotConvertGenericTypesWhenFormatterDoesNotBelongToCurrentClass(): void
7575
{
7676
$typeExtractor = $this->createStub(TypeExtractorInterface::class);
77-
$typeExtractor->method('extractFromReturnType')->willReturn('T');
77+
$typeExtractor->method('extractFromFunctionReturn')->willReturn('T');
7878

7979
$context = [
8080
'symfony' => [

Tests/Hook/Unmarshal/PropertyHookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testRetrieveActualPropertyName(): void
6868
public function testFormatValue(): void
6969
{
7070
$typeExtractor = $this->createStub(TypeExtractorInterface::class);
71-
$typeExtractor->method('extractFromParameter')->willReturn('string');
71+
$typeExtractor->method('extractFromFunctionParameter')->willReturn('string');
7272

7373
$object = new class() {
7474
public string $foo;

Tests/Type/PhpstanTypeExtractorTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,35 @@ public function testCannotHandleUnknownNode(): void
5959
/**
6060
* @dataProvider typesDataProvider
6161
*/
62-
public function testExtractFromReturnType(string $expectedType, string $method): void
62+
public function testExtractFromFunctionReturn(string $expectedType, string $method): void
6363
{
6464
$fallbackExtractor = $this->createStub(TypeExtractorInterface::class);
65-
$fallbackExtractor->method('extractFromReturnType')->willReturn('FALLBACK');
65+
$fallbackExtractor->method('extractFromFunctionReturn')->willReturn('FALLBACK');
6666

6767
$extractor = new PhpstanTypeExtractor($fallbackExtractor);
6868
$reflectionMethod = (new \ReflectionClass(PhpstanExtractableDummy::class))->getMethod($method);
6969

70-
$this->assertSame($expectedType, $extractor->extractFromReturnType($reflectionMethod));
70+
$this->assertSame($expectedType, $extractor->extractFromFunctionReturn($reflectionMethod));
7171
}
7272

73-
public function testFallbackOnVoidAndNeverReturnType(): void
73+
public function testFallbackOnVoidAndNeverFunctionReturn(): void
7474
{
7575
$fallbackExtractor = $this->createStub(TypeExtractorInterface::class);
76-
$fallbackExtractor->method('extractFromReturnType')->willReturn('FALLBACK');
76+
$fallbackExtractor->method('extractFromFunctionReturn')->willReturn('FALLBACK');
7777

7878
$extractor = new PhpstanTypeExtractor($fallbackExtractor);
7979

8080
$voidReflectionMethod = (new \ReflectionClass(PhpstanExtractableDummy::class))->getMethod('void');
8181
$neverReflectionMethod = (new \ReflectionClass(PhpstanExtractableDummy::class))->getMethod('never');
8282

83-
$this->assertSame('FALLBACK', $extractor->extractFromReturnType($voidReflectionMethod));
84-
$this->assertSame('FALLBACK', $extractor->extractFromReturnType($neverReflectionMethod));
83+
$this->assertSame('FALLBACK', $extractor->extractFromFunctionReturn($voidReflectionMethod));
84+
$this->assertSame('FALLBACK', $extractor->extractFromFunctionReturn($neverReflectionMethod));
8585
}
8686

87-
public function testExtractClassTypeFromFunctionReturnType(): void
87+
public function testExtractClassTypeFromFunctionFunctionReturn(): void
8888
{
8989
$fallbackExtractor = $this->createStub(TypeExtractorInterface::class);
90-
$fallbackExtractor->method('extractFromReturnType')->willReturn('FALLBACK');
90+
$fallbackExtractor->method('extractFromFunctionReturn')->willReturn('FALLBACK');
9191

9292
$extractor = new PhpstanTypeExtractor($fallbackExtractor);
9393

@@ -96,7 +96,7 @@ public function testExtractClassTypeFromFunctionReturnType(): void
9696
return $this;
9797
});
9898

99-
$this->assertSame(self::class, $extractor->extractFromReturnType($selfReflectionFunction));
99+
$this->assertSame(self::class, $extractor->extractFromFunctionReturn($selfReflectionFunction));
100100
}
101101

102102
/**
@@ -105,27 +105,27 @@ public function testExtractClassTypeFromFunctionReturnType(): void
105105
public function testExtractFromParameter(string $expectedType, string $method): void
106106
{
107107
$fallbackExtractor = $this->createStub(TypeExtractorInterface::class);
108-
$fallbackExtractor->method('extractFromParameter')->willReturn('FALLBACK');
108+
$fallbackExtractor->method('extractFromFunctionParameter')->willReturn('FALLBACK');
109109

110110
$extractor = new PhpstanTypeExtractor($fallbackExtractor);
111111

112112
$reflectionParameter = (new \ReflectionClass(PhpstanExtractableDummy::class))->getMethod($method)->getParameters()[0];
113113

114-
$this->assertSame($expectedType, $extractor->extractFromParameter($reflectionParameter));
114+
$this->assertSame($expectedType, $extractor->extractFromFunctionParameter($reflectionParameter));
115115
}
116116

117117
public function testExtractClassTypeFromParameter(): void
118118
{
119119
$fallbackExtractor = $this->createStub(TypeExtractorInterface::class);
120-
$fallbackExtractor->method('extractFromParameter')->willReturn('FALLBACK');
120+
$fallbackExtractor->method('extractFromFunctionParameter')->willReturn('FALLBACK');
121121

122122
$extractor = new PhpstanTypeExtractor($fallbackExtractor);
123123

124124
/** @param self $_ */
125125
$selfReflectionFunction = new \ReflectionFunction(function ($_) {
126126
});
127127

128-
$this->assertSame(self::class, $extractor->extractFromParameter($selfReflectionFunction->getParameters()[0]));
128+
$this->assertSame(self::class, $extractor->extractFromFunctionParameter($selfReflectionFunction->getParameters()[0]));
129129
}
130130

131131
/**

Tests/Type/ReflectionTypeExtractorTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public function testThrowIfCannotFindPropertyType(): void
5050
/**
5151
* @dataProvider typesDataProvider
5252
*/
53-
public function testExtractFromReturnType(string $expectedType, string $method): void
53+
public function testExtractFromFunctionReturn(string $expectedType, string $method): void
5454
{
5555
$reflectionMethod = (new \ReflectionClass(ReflectionExtractableDummy::class))->getMethod($method);
5656

57-
$this->assertSame($expectedType, (new ReflectionTypeExtractor())->extractFromReturnType($reflectionMethod));
57+
$this->assertSame($expectedType, (new ReflectionTypeExtractor())->extractFromFunctionReturn($reflectionMethod));
5858
}
5959

6060
public function testExtractClassTypeFromFunctionReturnType(): void
@@ -63,13 +63,13 @@ public function testExtractClassTypeFromFunctionReturnType(): void
6363
return $this;
6464
});
6565

66-
$this->assertSame(self::class, (new ReflectionTypeExtractor())->extractFromReturnType($selfReflectionFunction));
66+
$this->assertSame(self::class, (new ReflectionTypeExtractor())->extractFromFunctionReturn($selfReflectionFunction));
6767

6868
$parentReflectionFunction = new \ReflectionFunction(function (): parent {
6969
return $this;
7070
});
7171

72-
$this->assertSame(parent::class, (new ReflectionTypeExtractor())->extractFromReturnType($parentReflectionFunction));
72+
$this->assertSame(parent::class, (new ReflectionTypeExtractor())->extractFromFunctionReturn($parentReflectionFunction));
7373
}
7474

7575
public function testCannotHandleIntersectionReturnType(): void
@@ -78,7 +78,7 @@ public function testCannotHandleIntersectionReturnType(): void
7878

7979
$this->expectException(UnsupportedTypeException::class);
8080

81-
(new ReflectionTypeExtractor())->extractFromReturnType($reflectionMethod);
81+
(new ReflectionTypeExtractor())->extractFromFunctionReturn($reflectionMethod);
8282
}
8383

8484
public function testCannotHandleVoidReturnType(): void
@@ -87,7 +87,7 @@ public function testCannotHandleVoidReturnType(): void
8787

8888
$this->expectException(UnsupportedTypeException::class);
8989

90-
(new ReflectionTypeExtractor())->extractFromReturnType($reflectionMethod);
90+
(new ReflectionTypeExtractor())->extractFromFunctionReturn($reflectionMethod);
9191
}
9292

9393
public function testCannotHandleNeverReturnType(): void
@@ -96,7 +96,7 @@ public function testCannotHandleNeverReturnType(): void
9696

9797
$this->expectException(UnsupportedTypeException::class);
9898

99-
(new ReflectionTypeExtractor())->extractFromReturnType($reflectionMethod);
99+
(new ReflectionTypeExtractor())->extractFromFunctionReturn($reflectionMethod);
100100
}
101101

102102
public function testThrowIfCannotFindReturnType(): void
@@ -105,7 +105,7 @@ public function testThrowIfCannotFindReturnType(): void
105105

106106
$reflectionMethod = (new \ReflectionClass(ReflectionExtractableDummy::class))->getMethod('undefined');
107107

108-
(new ReflectionTypeExtractor())->extractFromReturnType($reflectionMethod);
108+
(new ReflectionTypeExtractor())->extractFromFunctionReturn($reflectionMethod);
109109
}
110110

111111
/**
@@ -115,7 +115,7 @@ public function testExtractFromParameter(string $expectedType, string $method):
115115
{
116116
$reflectionParameter = (new \ReflectionClass(ReflectionExtractableDummy::class))->getMethod($method)->getParameters()[0];
117117

118-
$this->assertSame($expectedType, (new ReflectionTypeExtractor())->extractFromParameter($reflectionParameter));
118+
$this->assertSame($expectedType, (new ReflectionTypeExtractor())->extractFromFunctionParameter($reflectionParameter));
119119
}
120120

121121
public function testThrowIfCannotFindParameterType(): void
@@ -124,7 +124,7 @@ public function testThrowIfCannotFindParameterType(): void
124124

125125
$reflectionParameter = (new \ReflectionClass(ReflectionExtractableDummy::class))->getMethod('undefined')->getParameters()[0];
126126

127-
(new ReflectionTypeExtractor())->extractFromParameter($reflectionParameter);
127+
(new ReflectionTypeExtractor())->extractFromFunctionParameter($reflectionParameter);
128128
}
129129

130130
/**

Type/PhpstanTypeExtractor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function extractFromProperty(\ReflectionProperty $property): string
5050
return $this->phpstanTypeHelper->getType($typeNode, $property->getDeclaringClass()->getName(), $this->getTemplateNodes($property->getDeclaringClass()));
5151
}
5252

53-
public function extractFromReturnType(\ReflectionFunctionAbstract $function): string
53+
public function extractFromFunctionReturn(\ReflectionFunctionAbstract $function): string
5454
{
5555
if (null === $typeNode = $this->getTypeNode($function)) {
56-
return $this->decoratedTypeExtractor->extractFromReturnType($function);
56+
return $this->decoratedTypeExtractor->extractFromFunctionReturn($function);
5757
}
5858

5959
$declaringClass = $function instanceof \ReflectionMethod ? $function->getDeclaringClass() : $function->getClosureScopeClass();
@@ -64,10 +64,10 @@ public function extractFromReturnType(\ReflectionFunctionAbstract $function): st
6464
return $this->phpstanTypeHelper->getType($typeNode, $declaringClass->getName(), $this->getTemplateNodes($declaringClass));
6565
}
6666

67-
public function extractFromParameter(\ReflectionParameter $parameter): string
67+
public function extractFromFunctionParameter(\ReflectionParameter $parameter): string
6868
{
6969
if (null === $typeNode = $this->getTypeNode($parameter)) {
70-
return $this->decoratedTypeExtractor->extractFromParameter($parameter);
70+
return $this->decoratedTypeExtractor->extractFromFunctionParameter($parameter);
7171
}
7272

7373
$function = $parameter->getDeclaringFunction();

Type/ReflectionTypeExtractor.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public function extractFromProperty(\ReflectionProperty $property): string
2626
return $this->extractFromReflection($type, $property->getDeclaringClass());
2727
}
2828

29-
// TODO FunctionReturn
30-
public function extractFromReturnType(\ReflectionFunctionAbstract $function): string
29+
public function extractFromFunctionReturn(\ReflectionFunctionAbstract $function): string
3130
{
3231
/** @var \ReflectionClass<object>|null $declaringClass */
3332
$declaringClass = $function instanceof \ReflectionMethod ? $function->getDeclaringClass() : $function->getClosureScopeClass();
@@ -39,8 +38,7 @@ public function extractFromReturnType(\ReflectionFunctionAbstract $function): st
3938
return $this->extractFromReflection($type, $declaringClass);
4039
}
4140

42-
// TODO FunctionParameter
43-
public function extractFromParameter(\ReflectionParameter $parameter): string
41+
public function extractFromFunctionParameter(\ReflectionParameter $parameter): string
4442
{
4543
$function = $parameter->getDeclaringFunction();
4644

Type/TypeExtractorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ interface TypeExtractorInterface
1616
{
1717
public function extractFromProperty(\ReflectionProperty $property): string;
1818

19-
public function extractFromReturnType(\ReflectionFunctionAbstract $function): string;
19+
public function extractFromFunctionReturn(\ReflectionFunctionAbstract $function): string;
2020

21-
public function extractFromParameter(\ReflectionParameter $parameter): string;
21+
public function extractFromFunctionParameter(\ReflectionParameter $parameter): string;
2222

2323
/**
2424
* @param \ReflectionClass<object> $class

Type/TypeNameResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*
1818
* @internal
1919
*/
20-
// TODO test
2120
final class TypeNameResolver
2221
{
2322
/**

0 commit comments

Comments
 (0)