I’m writing a custom PHPStan rule that suggests marking a class as readonly
(introduced in PHP 8.2) when it meets all the necessary conditions.
One of the requirements for safely applying readonly
is ensuring that the class has no child classes, because in PHP 8.2, if a parent is readonly
, all its child classes must be readonly
too.
The problem is, PHPStan doesn’t provide a simple, reliable way to check if a class is extended within the project during static analysis.
I’ve considered the following approaches:
- Using Composer’s
autoload_classmap.php
, but withPSR-4 autoloading
, it often doesn’t contain project classes. - Forcing
composer dump-autoload -o --classmap-authoritative
, which solves the problem but isn’t always acceptable for development setups. - Writing a custom file scanner that parses PHP files to extract namespace and class names to build a class map (either via simple regex or using
nikic/php-parser
).
Question:
What is the recommended or common approach to reliably detect child classes in a PHPStan custom rule, considering static analysis only?
Are there any best practices for this scenario, or do people generally rely on their own project-specific class scanning logic?