Skip to content

Commit 0ed74bc

Browse files
committed
Fix findBy method reflection to accept a parameter and return the correct type
1 parent b9f791d commit 0ed74bc

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/Reflection/RepositoryFindByMethodReflection.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,36 @@
33

44
namespace SaschaEgerer\PhpstanTypo3\Reflection;
55

6+
use PHPStan\Broker\Broker;
67
use PHPStan\Reflection\ClassReflection;
78
use PHPStan\Reflection\MethodReflection;
89
use PHPStan\Type\ObjectType;
910
use PHPStan\Type\Type;
10-
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
11+
use TYPO3\CMS\Core\Utility\ClassNamingUtility;
12+
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
1113

1214
class RepositoryFindByMethodReflection implements MethodReflection
1315
{
14-
16+
/**
17+
* @var \PHPStan\Reflection\ClassReflection
18+
*/
1519
private $classReflection;
1620

21+
/**
22+
* @var string
23+
*/
1724
private $name;
1825

19-
public function __construct(ClassReflection $classReflection, string $name)
26+
/**
27+
* @var \PHPStan\Broker\Broker
28+
*/
29+
private $broker;
30+
31+
public function __construct(ClassReflection $classReflection, string $name, Broker $broker)
2032
{
2133
$this->classReflection = $classReflection;
2234
$this->name = $name;
35+
$this->broker = $broker;
2336
}
2437

2538
public function getDeclaringClass(): ClassReflection
@@ -52,9 +65,27 @@ public function getName(): string
5265
return $this->name;
5366
}
5467

68+
private function getPropertyName(): string
69+
{
70+
return lcfirst(substr($this->getName(), 6));
71+
}
72+
73+
private function getModelName(): string
74+
{
75+
$className = $this->classReflection->getName();
76+
77+
return ClassNamingUtility::translateRepositoryNameToModelName($className);
78+
}
79+
5580
public function getParameters(): array
5681
{
57-
return [];
82+
$modelReflection = $this->broker->getClass($this->getModelName());
83+
84+
$type = $modelReflection->getNativeProperty($this->getPropertyName())->getType();
85+
86+
return [
87+
new RepositoryFindByParameterReflection('arg', $type)
88+
];
5889
}
5990

6091
public function isVariadic(): bool
@@ -64,6 +95,6 @@ public function isVariadic(): bool
6495

6596
public function getReturnType(): Type
6697
{
67-
return new ObjectType(ObjectStorage::class);
98+
return new ObjectType(QueryResultInterface::class);
6899
}
69100
}

src/Reflection/RepositoryFindOneByParameterReflection.php renamed to src/Reflection/RepositoryFindByParameterReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPStan\Type\Type;
99
use PHPStan\Type\TypeCombinator;
1010

11-
class RepositoryFindOneByParameterReflection implements ParameterReflection
11+
class RepositoryFindByParameterReflection implements ParameterReflection
1212
{
1313
/**
1414
* @var string

src/Reflection/RepositoryFindOneByMethodReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getParameters(): array
8383
$type = $modelReflection->getNativeProperty($this->getPropertyName())->getType();
8484

8585
return [
86-
new RepositoryFindOneByParameterReflection('arg', $type)
86+
new RepositoryFindByParameterReflection('arg', $type)
8787
];
8888
}
8989

src/Reflection/RepositoryMethodsClassReflectionExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
3636
if (0 === strpos($methodName, 'findOneBy')) {
3737
$methodReflection = new RepositoryFindOneByMethodReflection($classReflection, $methodName, $this->broker);
3838
} else {
39-
$methodReflection = new RepositoryFindByMethodReflection($classReflection, $methodName);
39+
$methodReflection = new RepositoryFindByMethodReflection($classReflection, $methodName, $this->broker);
4040
}
4141

4242
return $methodReflection;

0 commit comments

Comments
 (0)