-
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
base: 2.2.x
Are you sure you want to change the base?
Changes from all commits
e49edb3
3b43a97
0c3c91c
981b7d2
489a700
5d19ffd
25b11fa
aef3161
1d5e64d
0fa6512
b5cc8ad
d8a9e52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?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, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testInFileExists(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/include-in-file-exists.php'], []); | ||
| } | ||
|
|
||
| } | ||
| 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,10 +133,15 @@ 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, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testInFileExists(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/include-in-file-exists.php'], []); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| if (file_exists(__DIR__ . '/../vendor/autoload.php')) { | ||
| require __DIR__ . '/../vendor/autoload.php'; | ||
| } | ||
|
Comment on lines
+3
to
+5
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. in addition to the initial issue, this PR also prevents a error when a given path was checked with |
||
|
|
||
| if (is_file(__DIR__ . '/../vendor/autoload.php')) { | ||
| require __DIR__ . '/../vendor/autoload.php'; | ||
| } | ||
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