Skip to content

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

Open
staabm wants to merge 12 commits into
phpstan:2.2.xfrom
staabm:dir-path
Open

Fix "Differing behaviors when requiring or including relatively vs using __DIR__"#5862
staabm wants to merge 12 commits into
phpstan:2.2.xfrom
staabm:dir-path

Conversation

@staabm

@staabm staabm commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

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 on lines +3 to +5
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

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.

in addition to the initial issue, this PR also prevents a error when a given path was checked with file_exists/is_file before require/include

@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

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__

3 participants