Skip to content

Commit a84c98c

Browse files
committed
TASK: Also add method getPropertyFromAspect
1 parent 43cc08e commit a84c98c

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

src/Rule/ContextAspectValidationRule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public function processNode(Node $node, Scope $scope): array
3838
}
3939

4040
$methodReflection = $scope->getMethodReflection($scope->getType($node->var), $node->name->toString());
41-
if ($methodReflection === null || $methodReflection->getName() !== 'getAspect') {
41+
42+
if ($methodReflection === null) {
43+
return [];
44+
}
45+
46+
if (!in_array($methodReflection->getName(), ['getAspect', 'getPropertyFromAspect'], true)) {
4247
return [];
4348
}
4449

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
<?php
2-
declare(strict_types=1);
3-
1+
<?php declare(strict_types = 1);
42

53
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\ContextAspectValidationRule;
64

7-
85
use PHPStan\Rules\Rule;
96
use PHPStan\Testing\RuleTestCase;
107
use SaschaEgerer\PhpstanTypo3\Rule\ContextAspectValidationRule;
@@ -15,23 +12,29 @@
1512
*/
1613
final class ContextAspectValidationRuleTest extends RuleTestCase
1714
{
15+
1816
public function testRuleWithErrors(): void
1917
{
2018
$this->analyse(
21-
[__DIR__.'/Fixture/UseContextApiWithUndefinedAspect.php'],
19+
[__DIR__ . '/Fixture/UseContextApiWithUndefinedAspect.php'],
2220
[
2321
[
2422
'There is no aspect "foo" configured so we can\'t figure out the exact type to return when calling TYPO3\CMS\Core\Context\Context::getAspect',
25-
15,
26-
'You should add custom aspects to the typo3.contextApiGetAspectMapping setting.'
23+
13,
24+
'You should add custom aspects to the typo3.contextApiGetAspectMapping setting.',
25+
],
26+
[
27+
'There is no aspect "dates" configured so we can\'t figure out the exact type to return when calling TYPO3\CMS\Core\Context\Context::getPropertyFromAspect',
28+
16,
29+
'You should add custom aspects to the typo3.contextApiGetAspectMapping setting.',
2730
],
2831
]
2932
);
3033
}
3134

3235
public static function getAdditionalConfigFiles(): array
3336
{
34-
return [__DIR__.'/../../../../extension.neon'];
37+
return [__DIR__ . '/../../../../extension.neon'];
3538
}
3639

3740
public function testRuleWithoutErrors(): void
@@ -42,7 +45,8 @@ public function testRuleWithoutErrors(): void
4245
protected function getRule(): Rule
4346
{
4447
return new ContextAspectValidationRule([
45-
'date' => DateTimeAspect::class
48+
'date' => DateTimeAspect::class,
4649
]);
4750
}
51+
4852
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
<?php
2-
declare(strict_types=1);
3-
1+
<?php declare(strict_types = 1);
42

53
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\ContextAspectValidationRule\Fixture;
64

7-
85
use TYPO3\CMS\Core\Context\Context;
96
use TYPO3\CMS\Core\Context\DateTimeAspect;
107
use TYPO3\CMS\Core\Utility\GeneralUtility;
118

129
final class UseContextApiWithDefinedAspect
1310
{
11+
1412
public function someMethod(): void
1513
{
1614
$dateAspect = GeneralUtility::makeInstance(Context::class)->getAspect('date');
1715
$dateAspect->get('bar');
1816

1917
$class = new class {
18+
2019
public function getAspect(string $name): \stdClass
2120
{
2221
return new \stdClass();
2322
}
23+
2424
};
2525

2626
$myCustomAspect = $class->getAspect('foo');
@@ -30,7 +30,6 @@ public function getAspect(string $name): \stdClass
3030
$aspectWithoutDefiningName->get('bar');
3131

3232
GeneralUtility::makeInstance(Context::class)->setAspect('dates', new DateTimeAspect(new \DateTimeImmutable()));
33-
34-
GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('dates', 'foo');
3533
}
34+
3635
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
<?php
2-
declare(strict_types=1);
3-
1+
<?php declare(strict_types = 1);
42

53
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\ContextAspectValidationRule\Fixture;
64

7-
85
use TYPO3\CMS\Core\Context\Context;
96
use TYPO3\CMS\Core\Utility\GeneralUtility;
107

118
final class UseContextApiWithUndefinedAspect
129
{
10+
1311
public function someMethod(): void
1412
{
1513
$foo = GeneralUtility::makeInstance(Context::class)->getAspect('foo');
1614
$foo->get('bar');
15+
16+
GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('dates', 'foo');
1717
}
18+
1819
}

0 commit comments

Comments
 (0)