Skip to content

Commit 92a6ef2

Browse files
xificurkclaude
andauthored
Rule constructor no-reflection used test (#359)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e7afb98 commit 92a6ef2

5 files changed

Lines changed: 78 additions & 9 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "^7.4 || ^8.0",
14-
"phpstan/phpstan": "^2.1.32"
14+
"phpstan/phpstan": "^2.1.33"
1515
},
1616
"require-dev": {
1717
"editorconfig-checker/editorconfig-checker": "^10.6.0",

composer.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
betterReflectionSourceLocator:
3+
class: ShipMonk\PHPStan\ThrowingSourceLocator
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonk\PHPStan;
4+
5+
use PHPStan\Testing\PHPStanTestCase;
6+
use function array_merge;
7+
8+
class NoReflectionInRuleConstructorTest extends PHPStanTestCase
9+
{
10+
11+
/**
12+
* @return string[]
13+
*/
14+
public static function getAdditionalConfigFiles(): array
15+
{
16+
return array_merge(
17+
parent::getAdditionalConfigFiles(),
18+
[
19+
__DIR__ . '/../rules.neon',
20+
__DIR__ . '/NoReflectionInRuleConstructorTest.neon',
21+
],
22+
);
23+
}
24+
25+
public function test(): void
26+
{
27+
self::expectNotToPerformAssertions();
28+
29+
self::getContainer()->getServicesByTag('phpstan.rules.rule');
30+
}
31+
32+
}

tests/ThrowingSourceLocator.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonk\PHPStan;
4+
5+
use LogicException;
6+
use PHPStan\BetterReflection\Identifier\Identifier;
7+
use PHPStan\BetterReflection\Identifier\IdentifierType;
8+
use PHPStan\BetterReflection\Reflection\Reflection;
9+
use PHPStan\BetterReflection\Reflector\Reflector;
10+
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
11+
12+
class ThrowingSourceLocator implements SourceLocator
13+
{
14+
15+
public function locateIdentifier(
16+
Reflector $reflector,
17+
Identifier $identifier
18+
): ?Reflection // @phpstan-ignore return.internalInterface
19+
{
20+
throw new LogicException('Reflection must not be called during rule construction');
21+
}
22+
23+
/**
24+
* @return list<Reflection>
25+
*/
26+
public function locateIdentifiersByType(
27+
Reflector $reflector,
28+
IdentifierType $identifierType
29+
): array // @phpstan-ignore return.internalInterface
30+
{
31+
throw new LogicException('Reflection must not be called during rule construction');
32+
}
33+
34+
}

0 commit comments

Comments
 (0)