Skip to content

Commit df96878

Browse files
committed
Discover DI attributes in attributeServicesDirectories
AttributeServicesDiscoverer finds candidate classes through the psr-4 path contract and vendor/composer/autoload_classmap.php and reads the @api attributes with native reflection: AutowiredService, NonAutowiredService, RegisteredRule, RegisteredCollector, GenerateFactory, Conditional, and AutowiredParameter on constructor parameters. No file is parsed. A content prefilter keeps classes without PHPStan attributes from being autoloaded. PHPStan-internal attributes, and files that mention a DI attribute but yield no autoloadable class, are errors. PHP 8.0 has no new in attribute arguments, so Conditional there takes [NewExpression marker => [Class::class, [arguments]]], the same array simple-downgrader writes and composer-attribute-collector understands. Discovery runs lazily while the container compiles, so a container cache hit loads no third-party class. The container cache key of the project's own directories hashes only the files the same prefilter accepts, so editing a class without PHPStan's attributes does not recompile the container. Amp-Thread-ID: https://ampcode.com/threads/T-01a19cd4-be31-75b4-bd15-67786eff5983
1 parent 9516c0b commit df96878

34 files changed

Lines changed: 1214 additions & 11 deletions

‎build/baseline-pre-8.0.neon‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ parameters:
2424
count: 2
2525
path: PHPStan/Build/TurboAttributeCollector.php
2626

27+
-
28+
rawMessage: 'Call to an undefined method ReflectionClass<object>::getAttributes().'
29+
identifier: method.notFound
30+
count: 1
31+
path: ../src/DependencyInjection/AttributeServices/AttributeServicesDiscoverer.php
32+
33+
-
34+
rawMessage: 'Call to an undefined method ReflectionParameter::getAttributes().'
35+
identifier: method.notFound
36+
count: 1
37+
path: ../src/DependencyInjection/AttributeServices/AttributeServicesDiscoverer.php
38+
39+
-
40+
message: '#^(Parameter \$attribute of method .+ has invalid type|Call to method \w+\(\) on an unknown class) ReflectionAttribute\.$#'
41+
count: 5
42+
path: ../src/DependencyInjection/AttributeServices/AttributeServicesDiscoverer.php
43+
2744
-
2845
rawMessage: 'Call to an undefined method ReflectionClass::isReadOnly().'
2946
identifier: method.notFound
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- src/Foo.php
2+
+++ src/Foo.php
3+
@@ -5,4 +5,8 @@
4+
class Foo
5+
{
6+
7+
+ public function bar(): void
8+
+ {
9+
+ }
10+
+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- src/MyRule.php
2+
+++ src/MyRule.php
3+
@@ -22,7 +22,7 @@
4+
5+
public function processNode(Node $node, Scope $scope): array
6+
{
7+
- return [RuleErrorBuilder::message('the rule says one thing')->identifier('test.myRule')->build()];
8+
+ return [RuleErrorBuilder::message('the rule says another thing')->identifier('test.myRule')->build()];
9+
}
10+
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2EAttributeServices;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\RegisteredRule;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
10+
11+
/**
12+
* @implements Rule<Node\Stmt\Class_>
13+
*/
14+
#[RegisteredRule(level: 0)]
15+
class MyRule implements Rule
16+
{
17+
18+
public function getNodeType(): string
19+
{
20+
return Node\Stmt\Class_::class;
21+
}
22+
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
return [RuleErrorBuilder::message('the rule says one thing')->identifier('test.myRule')->build()];
26+
}
27+
28+
}

‎src/DependencyInjection/AttributeServices/AttributeServicesDirectoriesResolver.php‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use PHPStan\File\CouldNotReadFileException;
66
use PHPStan\File\FileHelper;
7+
use PHPStan\File\FileReader;
78
use RecursiveDirectoryIterator;
89
use RecursiveIteratorIterator;
910
use SplFileInfo;
1011
use function count;
11-
use function hash_file;
12+
use function hash;
1213
use function intdiv;
1314
use function is_array;
1415
use function is_dir;
@@ -310,9 +311,9 @@ private function pathsIntersect(string $a, string $b): bool
310311
}
311312

312313
/**
313-
* Content hashes of all PHP files under the directory - the cache-key fallback when
314-
* a package version cannot stand in for the directory contents. Mirrors how config
315-
* files invalidate the container in Configurator::getAllConfigFilesHashes().
314+
* Content hashes of the PHP files under the directory that can carry PHPStan's DI attributes -
315+
* the cache-key fallback when a package version cannot stand in for the directory contents.
316+
* Mirrors how config files invalidate the container in Configurator::getAllConfigFilesHashes().
316317
*
317318
* @param list<string> $errors
318319
* @return array<string, string>
@@ -338,13 +339,18 @@ private function hashDirectory(string $directory, array &$errors): array
338339

339340
$hashes = [];
340341
foreach ($files as $file) {
341-
$hash = hash_file('sha256', $file);
342-
if ($hash === false) {
343-
$errors[] = (new CouldNotReadFileException($file))->getMessage();
342+
try {
343+
$contents = FileReader::read($file);
344+
} catch (CouldNotReadFileException $e) {
345+
$errors[] = $e->getMessage();
344346
continue;
345347
}
346348

347-
$hashes[$file] = $hash;
349+
if (!AttributeServicesDiscoverer::mayAffectDiscovery($contents)) {
350+
continue;
351+
}
352+
353+
$hashes[$file] = hash('sha256', $contents);
348354
}
349355

350356
ksort($hashes);

0 commit comments

Comments
 (0)