[DeadCode] Add RemovePsr4AutoloadedIncludeRector - #8149
Conversation
|
I think this should skip if the class has side effect directly in target source: namespace App;
class Cli
{
public static function init() {
// init cli ...
}
}
// here init on purpose
Cli::init();for example |
|
@TomasVotruba Would it be OK if I opened a small PR against this branch to address the side-effect skip @samsonasik mentioned? The idea would be to bail out unless the target file's top level contains only type declarations (class-like declarations, |
|
@lll-lll-lll-lll Sure, go for it 👌 |
…ire already covered by composer PSR-4 autoload
…ants in RemovePsr4AutoloadedIncludeRector (#8175)
e0fbd47 to
0456bcf
Compare
|
I didn't merge this yet because I was not sure about its application. Idea is here if needed 👍 |
Inspired at https://www.reddit.com/r/PHP/comments/1unb59g/i_built_a_static_analysis_tool_that_finds_require/
I'm surprised we don't have this rule yet, so obvious :)
Caveats
Adds
RemovePsr4AutoloadedIncludeRector(DeadCode). Removes a standaloneinclude/requirewhen the target file is already covered by a PSR-4 autoload mapping incomposer.json— the composer autoloader loads the class anyway, so the manualrequireis dead.Before / after
with
composer.json:{ "autoload": { "psr-4": { "App\\": "src" } } }How it decides
require X;(skips$x = require ...;).__DIR__ . '/rel.php', absolute, or relative literal) against the current file dir.requirewould drop the other definitions.autoload+autoload-devPSR-4 fromcomposer.json(auto-found at project root viagetcwd(), or setCOMPOSER_JSON_PATH).Skips (covered by fixtures)
$x = require ...;(non-standalone include)Configurable rule (does filesystem I/O), so it is not added to the
dead-codelevel set — opt-in via config.