Skip to content

Fix "Differing behaviors when requiring or including relatively vs using __DIR__"#5862

Merged
staabm merged 1 commit into
phpstan:2.2.xfrom
staabm:dir-path
Jun 14, 2026
Merged

Fix "Differing behaviors when requiring or including relatively vs using __DIR__"#5862
staabm merged 1 commit into
phpstan:2.2.xfrom
staabm:dir-path

Conversation

@staabm

@staabm staabm commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

closes phpstan/phpstan#12203

before this PR the RequireFileExistsRule was only useful with usePathConstantsAsConstantString: true in NEON config - a parameter which was/is not documented and is considered internal.

after this PR the RequireFileExistsRule can resolve paths which contain __DIR__ even when usePathConstantsAsConstantString: false - which is the PHPStan default. This means the rule actually gets useful in the common case.

Comment on lines +159 to +161
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);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same test as in tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php but without usePathConstantsAsConstantString: true defined via neon-config

Comment thread tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php
@staabm staabm marked this pull request as draft June 13, 2026 14:54
@staabm staabm self-assigned this Jun 13, 2026
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.',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@staabm staabm marked this pull request as ready for review June 13, 2026 19:52
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@staabm staabm requested a review from VincentLanglet June 13, 2026 19:54

@VincentLanglet VincentLanglet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like __DIR__ . $foo . $bar
Might be worth thesting multiple concat

@staabm staabm merged commit 6afc6d4 into phpstan:2.2.x Jun 14, 2026
666 of 672 checks passed
@staabm staabm deleted the dir-path branch June 14, 2026 07:43
@claudepache

Copy link
Copy Markdown
Contributor

before this PR the RequireFileExistsRule was only useful with usePathConstantsAsConstantString: true in NEON config - a parameter which was/is not documented and is considered internal.

I had found the parameter in the documentation maybe several months ago. What do you mean by “is considered internal”?

@staabm

staabm commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Differing behaviors when requiring or including relatively vs using __DIR__

4 participants