Repository navigation
Conversation
| if (! is_int($composerAutoloader)) { | ||
| // PHPStan calls the registered autoloaders for symbols that may already be declared; | ||
| // loading one again would fail with "Cannot redeclare" | ||
| if (class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) { |
There was a problem hiding this comment.
probably need additional:
|| (function_exists('enum_exists') && enum_exists($class, false))enum_exists only exists in php 8.0+, while rector support php 7.4
Better move function_exists('enum_exists') to a variable before spl_autoload_register() I guess.
There was a problem hiding this comment.
class_exists() already returns true for enums, since an enum is a class internally (e.g. enum E {} then class_exists('E', false) is true). enum_exists() can only be true when class_exists() already is, so the existing check covers enums on 8.1+, and there are none on 7.4. That keeps the condition free of the function_exists('enum_exists') guard. Happy to add it anyway if you prefer it spelled out.
|
This looks like wrong place to fix the issue. Modifing our autoload to fix static analysis error. Rector is a standalone binary with own vendor, not visible to the whole project outside |
|
Just curious, the |
|
What I use final class IsNullToIdenticalNullRector extends AbstractRector implements DocumentedRuleInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change is_null($value) to $value === null', [
new CodeSample('is_null($value);', '$value === null;'),
]);
}
// getNodeTypes() and refactor() as usual
}and its test turns each sample into a fixture and runs it through the rule: foreach ($rule->getRuleDefinition()->getCodeSamples() as $index => $codeSample) {
file_put_contents(
sprintf('%s/sample-%d.php.inc', $dir, $index),
sprintf("<?php\n\n%s\n-----\n<?php\n\n%s\n", $codeSample->getBadCode(), $codeSample->getGoodCode()),
);
}The test snippet is simplified. The real helper also loosens whitespace in the expected output, because Rector's printer breaks lines differently from how the samples are written. That detail doesn't change the point of the example, so I left it out. |
|
I see :) interesting usage of code samples. |
PHPStan crashes with a fatal error when it analyses a project whose classes implement Rector interfaces, such as a package of custom rules:
PHPStan's
AutoloadFunctionsSourceLocatorcalls the registered autoloaders directly, also for symbols that are already declared, which PHP itself never does. The autoloader inbootstrap.phpthen hands the name to the bundled class loader, which includes the file a second time. Logging inside the closure at the moment of the crash showed the interface already declared and its file already included.This returns early when the class, interface or trait (enums count as classes) already exists, so the closure only ever loads what is missing.
Reproducer
composer.json{ "require-dev": { "phpstan/phpstan": "2.3.7", "rector/rector": "2.8.5" }, "autoload": { "psr-4": { "Repro\\": "src/" } } }src/SomeRule.phpphpstan.neoncomposer install && vendor/bin/phpstan analysefails with the error above; with this change applied tovendor/rector/rector/bootstrap.phpit reports no errors.I have not found what declares the interface before PHPStan asks for it; that part may be worth a look on the PHPStan side as well.