8000 chore: refacto extractor (#9) · mtarld/symfony@4d76dde · GitHub
[go: up one dir, main page]

8000 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($propertyF B422 ormatter)
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< 2851 /td>-
$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;
F438 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
/**

0 commit comments

Comments
 (0)
0