-
Notifications
You must be signed in to change notification settings - Fork 576
Fix "Differing behaviors when requiring or including relatively vs using __DIR__" #5862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+110
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/PHPStan/Rules/Keywords/RequireFileExistsRuleNoConstantPathTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Keywords; | ||
|
|
||
| use PHPStan\Node\Printer\ExprPrinter; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<RequireFileExistsRule> | ||
| */ | ||
| class RequireFileExistsRuleNoConstantPathTest extends RuleTestCase | ||
| { | ||
|
|
||
| private string $currentWorkingDirectory = __DIR__ . '/../'; | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new RequireFileExistsRule( | ||
| $this->currentWorkingDirectory, | ||
| self::getContainer()->getByType(ExprPrinter::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testBug12203NoConstantPath(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/bug-12203.php'], [ | ||
| [ | ||
| 'Path in require_once() "../bug-12203-sure-does-not-exist.php" is not a file or it does not exist.', | ||
| 5, | ||
| ], | ||
| [ | ||
| "Path in require_once() __DIR__ . '/../bug-12203-sure-does-not-exist.php' is not a file or it does not exist.", | ||
| 6, | ||
| ], | ||
| [ | ||
| "Path in require_once() __DIR__ . '/' . \$path . '/' . \$file is not a file or it does not exist.", | ||
| 10, | ||
| ], | ||
| [ | ||
| 'Path in require_once() __DIR__ . "{$path}/{$file}" is not a file or it does not exist.', | ||
| 12, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testInFileExists(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/include-in-file-exists.php'], []); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,13 @@ | |
|
|
||
| namespace PHPStan\Rules\Keywords; | ||
|
|
||
| use PHPStan\Node\Printer\ExprPrinter; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use function get_include_path; | ||
| use function implode; | ||
| use function realpath; | ||
| use function set_include_path; | ||
| use const DIRECTORY_SEPARATOR; | ||
| use const PATH_SEPARATOR; | ||
|
|
||
| /** | ||
|
|
@@ -21,7 +21,10 @@ class RequireFileExistsRuleTest extends RuleTestCase | |
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new RequireFileExistsRule($this->currentWorkingDirectory); | ||
| return new RequireFileExistsRule( | ||
| $this->currentWorkingDirectory, | ||
| self::getContainer()->getByType(ExprPrinter::class), | ||
| ); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
|
|
@@ -130,9 +133,17 @@ public function testBug12203(): void | |
| 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.', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| "Path in require_once() __DIR__ . '/../bug-12203-sure-does-not-exist.php' is not a file or it does not exist.", | ||
| 6, | ||
| ], | ||
| [ | ||
| "Path in require_once() __DIR__ . '/' . \$path . '/' . \$file is not a file or it does not exist.", | ||
| 10, | ||
| ], | ||
| [ | ||
| 'Path in require_once() __DIR__ . "{$path}/{$file}" is not a file or it does not exist.', | ||
| 12, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.phpbut withoutusePathConstantsAsConstantString: truedefined via neon-config