Fix "Differing behaviors when requiring or including relatively vs using __DIR__"#5862
Conversation
| if ($expr->left instanceof Node\Scalar\MagicConst\Dir && $expr->right instanceof Node\Scalar\String_) { | ||
| return new Node\Scalar\String_(dirname($scope->getFile()) . $expr->right->value); | ||
| } |
There was a problem hiding this comment.
instead of relying on InitializerExprTypeResolver->getType() to resolve the path based on the global usePathConstantsAsConstantString config, we resolve the path here independently of usePathConstantsAsConstantString, which allows us to report an error for the case in question
| ); | ||
| } | ||
|
|
||
| public function testBug12203NoConstantPath(): void |
There was a problem hiding this comment.
same test as in tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php but without usePathConstantsAsConstantString: true defined via neon-config
| 5, | ||
| ], | ||
| [ | ||
| 'Path in require_once() "' . __DIR__ . DIRECTORY_SEPARATOR . 'data/../bug-12203-sure-does-not-exist.php" is not a file or it does not exist.', |
There was a problem hiding this comment.
changed the reported error path to the actual expr in the source code, so we don't report a error containing a absolute file path, which cannot be properly used in baselines, because it likely differs between different computers
|
This pull request has been marked as ready for review. |
VincentLanglet
left a comment
There was a problem hiding this comment.
What about something like __DIR__ . $foo . $bar
Might be worth thesting multiple concat
I had found the parameter in the documentation maybe several months ago. What do you mean by “is considered internal”? |
|
I was refering to this comment: phpstan/phpstan#12203 (comment) Maybe it was documented meanwhile. Bottom line is that the rule now works in more cases with default settings |
closes phpstan/phpstan#12203
before this PR the
RequireFileExistsRulewas only useful withusePathConstantsAsConstantString: truein NEON config - a parameter which was/is not documented and is considered internal.after this PR the
RequireFileExistsRulecan resolve paths which contain__DIR__even whenusePathConstantsAsConstantString: false- which is the PHPStan default. This means the rule actually gets useful in the common case.