Skip to content

Commit 0f05048

Browse files
authored
Ensure anonymous classes are not reflected (#195)
* Add test to verify anonymous classes are not reflected * Check anonymousClass attribute
1 parent e53fa7b commit 0f05048

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Rules/Deprecations/DeprecatedAnnotationsRuleBase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ public function processNode(Node $node, Scope $scope): array
4343
if ($node->name === null) {
4444
return [];
4545
}
46+
if ($node->isAbstract()) {
47+
return [];
48+
}
49+
// PHPStan gives anonymous classes a name, so we cannot determine if
50+
// a class is truly anonymous using the normal methods from php-parser.
51+
// @see \PHPStan\Reflection\BetterReflection\BetterReflectionProvider::getAnonymousClassReflection
52+
if ($node->hasAttribute('anonymousClass') && $node->getAttribute('anonymousClass') === true) {
53+
return [];
54+
}
4655
$className = $node->name->name;
4756
$namespace = $scope->getNamespace();
4857
$reflection = $this->reflectionProvider->getClass($namespace . '\\' . $className);

tests/fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
// Catches at level 4.
4+
use Drupal\Core\Block\BlockBase;
5+
46
function phpstan_fixtures_IfConstantConditionRule() {
57
$zero = 0;
68
if ($zero) {
@@ -27,3 +29,16 @@ function phpstan_fixtures_module_load_includes_negative_test(): void {
2729
$module_handler = \Drupal::moduleHandler();
2830
$module_handler->loadInclude('phpstan_fixtures', 'fetch.inc');
2931
}
32+
33+
function phpstan_fixtures_thing_with_anonymous_class(): void {
34+
$foo = new class(
35+
[],
36+
'foo',
37+
[]
38+
) extends BlockBase {
39+
public function build(): array
40+
{
41+
return [];
42+
}
43+
};
44+
}

0 commit comments

Comments
 (0)