PSR-4 namespace/path mismatch in PHPStan rule causes autoloading failure
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3584090. -->
Reported by: [balagan](https://www.drupal.org/user/1230072)
Related to !58
>>>
<p>Summary:</p>
<h3>Problem</h3>
<p>The file phpstan-rules/BuildFieldTraitCacheableMetadataRule.php declares:</p>
<p>namespace Drupal\pluggable_entity_view_builder\PHPStan\Rules;<br>
But composer.json maps the Drupal\pluggable_entity_view_builder\PHPStan\ namespace prefix to phpstan-rules/:</p>
<p>"autoload": {<br>
"psr-4": {<br>
"Drupal\\pluggable_entity_view_builder\\PHPStan\\": "phpstan-rules/"<br>
}<br>
}<br>
Under PSR-4, the \Rules sub-namespace requires the file to live at phpstan-rules/Rules/BuildFieldTraitCacheableMetadataRule.php. Since the file is at phpstan-rules/BuildFieldTraitCacheableMetadataRule.php, Composer cannot autoload the class.</p>
<p>phpstan-extension.neon registers the class as a PHPStan service, and because phpstan/extension-installer is listed as an allowed plugin, any downstream project that installs this module will automatically include the extension — and immediately hit the autoload error.</p>
<h3>Steps to reproduce</h3>
<p>Install the module in a project that has phpstan/phpstan and phpstan/extension-installer.<br>
Run vendor/bin/phpstan analyse.<br>
PHPStan fails with a class-not-found error for Drupal\pluggable_entity_view_builder\PHPStan\Rules\BuildFieldTraitCacheableMetadataRule.</p>
<h3>Proposed fix</h3>
<p>Two options:</p>
<p>Move the file to phpstan-rules/Rules/BuildFieldTraitCacheableMetadataRule.php to match the declared namespace.<br>
Drop \Rules from the namespace — change the namespace in the file to Drupal\pluggable_entity_view_builder\PHPStan and update the class reference in phpstan-extension.neon accordingly.<br>
Option 2 is simpler as it avoids moving the file and changing the directory structure.</p>
<h3>Affected versions</h3>
<p>only 1.2.6 is affected</p>
issue