Skip to content

Commit e089fe5

Browse files
Merge pull request #93 from sabbelasichon/task/add-more-rule-tests
2 parents dba4460 + a84c98c commit e089fe5

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

src/Rule/ContextAspectValidationRule.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ public function getNodeType(): string
2828
return Node\Expr\MethodCall::class;
2929
}
3030

31+
/**
32+
* @param Node\Expr\MethodCall $node
33+
*/
3134
public function processNode(Node $node, Scope $scope): array
3235
{
3336
if (!$node->name instanceof Node\Identifier) {
3437
return [];
3538
}
3639

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\ContextAspectValidationRule;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use SaschaEgerer\PhpstanTypo3\Rule\ContextAspectValidationRule;
8+
use TYPO3\CMS\Core\Context\DateTimeAspect;
9+
10+
/**
11+
* @extends RuleTestCase<ContextAspectValidationRule>
12+
*/
13+
final class ContextAspectValidationRuleTest extends RuleTestCase
14+
{
15+
16+
public function testRuleWithErrors(): void
17+
{
18+
$this->analyse(
19+
[__DIR__ . '/Fixture/UseContextApiWithUndefinedAspect.php'],
20+
[
21+
[
22+
'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',
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.',
30+
],
31+
]
32+
);
33+
}
34+
35+
public static function getAdditionalConfigFiles(): array
36+
{
37+
return [__DIR__ . '/../../../../extension.neon'];
38+
}
39+
40+
public function testRuleWithoutErrors(): void
41+
{
42+
$this->analyse([__DIR__ . '/Fixture/UseContextApiWithDefinedAspect.php'], []);
43+
}
44+
45+
protected function getRule(): Rule
46+
{
47+
return new ContextAspectValidationRule([
48+
'date' => DateTimeAspect::class,
49+
]);
50+
}
51+
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\ContextAspectValidationRule\Fixture;
4+
5+
use TYPO3\CMS\Core\Context\Context;
6+
use TYPO3\CMS\Core\Context\DateTimeAspect;
7+
use TYPO3\CMS\Core\Utility\GeneralUtility;
8+
9+
final class UseContextApiWithDefinedAspect
10+
{
11+
12+
public function someMethod(): void
13+
{
14+
$dateAspect = GeneralUtility::makeInstance(Context::class)->getAspect('date');
15+
$dateAspect->get('bar');
16+
17+
$class = new class {
18+
19+
public function getAspect(string $name): \stdClass
20+
{
21+
return new \stdClass();
22+
}
23+
24+
};
25+
26+
$myCustomAspect = $class->getAspect('foo');
27+
$myCustomAspect->something = 'FooBarBaz';
28+
29+
$aspectWithoutDefiningName = GeneralUtility::makeInstance(Context::class)->getAspect();
30+
$aspectWithoutDefiningName->get('bar');
31+
32+
GeneralUtility::makeInstance(Context::class)->setAspect('dates', new DateTimeAspect(new \DateTimeImmutable()));
33+
}
34+
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\ContextAspectValidationRule\Fixture;
4+
5+
use TYPO3\CMS\Core\Context\Context;
6+
use TYPO3\CMS\Core\Utility\GeneralUtility;
7+
8+
final class UseContextApiWithUndefinedAspect
9+
{
10+
11+
public function someMethod(): void
12+
{
13+
$foo = GeneralUtility::makeInstance(Context::class)->getAspect('foo');
14+
$foo->get('bar');
15+
16+
GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('dates', 'foo');
17+
}
18+
19+
}

0 commit comments

Comments
 (0)