Skip to content

Commit 8c8c727

Browse files
committed
Use acutal return type in ObjectStorageReturnTypeExtension and refactor tests
1 parent a0a1139 commit 8c8c727

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/Type/ObjectStorageDynamicReturnTypeExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ public function getTypeFromMethodCall(
4343
$argumentType = $scope->getType($firstArgument->value);
4444

4545
if ((new StringType())->isSuperTypeOf($argumentType)->yes()) {
46-
return null;
46+
return $methodReflection->getVariants()[0]->getReturnType();
4747
}
48+
4849
if ((new IntegerType())->isSuperTypeOf($argumentType)->yes()) {
49-
return null;
50+
return $methodReflection->getVariants()[0]->getReturnType();
5051
}
52+
5153
return new MixedType();
5254
}
5355

tests/Unit/Type/data/object-storage-stub-files.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,44 @@
1313
class MyModel extends AbstractEntity
1414
{
1515

16-
public function foo(): void
16+
/**
17+
* @var ObjectStorage<self>
18+
*/
19+
protected ObjectStorage $testStorage;
20+
21+
public function checkObjectStorageType(): void
1722
{
1823
$myModel = new self();
1924
/** @var ObjectStorage<MyModel> $objectStorage */
2025
$objectStorage = new ObjectStorage();
2126
$objectStorage->attach($myModel);
2227

2328
assertType('TYPO3\CMS\Extbase\Persistence\ObjectStorage<' . self::class . '>', $objectStorage);
29+
}
2430

25-
foreach ($objectStorage as $key => $value) {
26-
31+
public function checkIteration(): void
32+
{
33+
foreach ($this->testStorage as $key => $value) {
2734
assertType('string', $key);
2835
assertType(self::class, $value);
2936
}
37+
}
3038

31-
assertType(self::class . '|null', $objectStorage->offsetGet(0));
32-
assertType(self::class . '|null', $objectStorage->offsetGet('0'));
33-
assertType(self::class . '|null', $objectStorage->current());
39+
public function checkArrayAccess(): void
40+
{
41+
assertType(self::class . '|null', $this->testStorage->offsetGet(0));
42+
assertType(self::class . '|null', $this->testStorage->offsetGet('0'));
43+
assertType(self::class . '|null', $this->testStorage->current());
3444

3545
// We ignore errors in the next line as this will produce an
3646
// "Offset 0 does not exist on TYPO3\CMS\Extbase\Persistence\ObjectStorage<ObjectStorage\My\Test\Extension\Domain\Model\MyModel>
3747
// due to the weird implementation of ArrayAccess in ObjectStorage::offsetGet()
3848
// @phpstan-ignore-next-line
39-
assertType(self::class . '|null', $objectStorage[0]);
49+
assertType(self::class . '|null', $this->testStorage[0]);
50+
51+
$myModel = new self();
4052

41-
assertType('mixed', $objectStorage->offsetGet($myModel));
53+
assertType('mixed', $this->testStorage->offsetGet($myModel));
4254
}
4355

4456
}

0 commit comments

Comments
 (0)