Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Reflection/InitializerExprContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,19 @@ public static function fromScope(Scope $scope): self
{
$function = $scope->getFunction();

// __FILE__ and __DIR__ are resolved at compile time, so in a trait context they
// point at the file the trait is declared in, while Scope::getFile() returns the
// file of the class that uses the trait.
$file = $scope->getFile();
if ($scope->isInTrait()) {
$traitFileName = $scope->getTraitReflection()->getFileName();
if ($traitFileName !== null) {
$file = $traitFileName;
}
}

return new self(
$scope->getFile(),
$file,
$scope->getNamespace(),
$scope->isInClass() ? $scope->getClassReflection()->getName() : null,
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
Expand Down
10 changes: 5 additions & 5 deletions src/Rules/Keywords/RequireFileExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ private function doesFileExist(string $path, Scope $scope): bool
}

/**
* The "calling script's own directory" fallback of a relative include is resolved
* at compile time, so inside a trait it is the directory of the file the trait is
* declared in - not the file of the class that uses it, which is what
* Scope::getFile() returns in a trait context.
* Both `__DIR__` and the "calling script's own directory" fallback of a relative
* include are resolved at compile time, so inside a trait they point at the file
* the trait is declared in - not at the file of the class that uses it, which is
* what Scope::getFile() returns in a trait context.
*/
private function getScopeFile(Scope $scope): string
{
Expand Down Expand Up @@ -198,7 +198,7 @@ private function resolveFilePaths(Expr $expr, Scope $scope, bool &$magicDirFallb

$paths = [];
foreach ($scope->getType($expr->right)->getConstantStrings() as $constantString) {
$paths[] = new ConstantStringType(dirname($scope->getFile()) . $constantString->getValue());
$paths[] = new ConstantStringType(dirname($this->getScopeFile($scope)) . $constantString->getValue());
}
return $paths;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/PathConstantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ public static function getAdditionalConfigFiles(): array
];
}

protected static function getAdditionalAnalysedFiles(): array
{
return [
__DIR__ . '/data/path-constants-trait/PathConstantsTrait.php',
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PathConstantsTestTrait;

use function PHPStan\Testing\assertType;

trait PathConstantsTrait
{

public function doFoo(): void
{
assertType('\'path-constants-trait\'', substr(__DIR__, -20));
assertType('\'PathConstantsTrait.php\'', substr(__FILE__, -22));
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/pathConstants-win.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@

\PHPStan\Testing\assertType('\'Analyser\\\\data\'', substr(__DIR__, -13));
\PHPStan\Testing\assertType('\'pathConstants-win.php\'', substr(__FILE__, -21));

class PathConstantsClassWin
{

use \PathConstantsTestTrait\PathConstantsTrait;

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/pathConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@

\PHPStan\Testing\assertType('\'Analyser/data\'', substr(__DIR__, -13));
\PHPStan\Testing\assertType('\'pathConstants.php\'', substr(__FILE__, -17));

class PathConstantsClass
{

use \PathConstantsTestTrait\PathConstantsTrait;

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ public function testRelativePathInTrait(): void
]);
}

public function testBug15015(): void
{
$this->analyse([
__DIR__ . '/data/bug-15015/trait-dir/Bug15015Trait.php',
__DIR__ . '/data/bug-15015/class-dir/Bug15015Class.php',
], [
[
"Path in include() __DIR__ . '/only-in-class-dir.php' is not a file or it does not exist.",
15,
],
]);
}

public function testInFileExists(): void
{
$this->analyse([__DIR__ . '/data/include-in-file-exists.php'], []);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Bug15015;

class Bug15015Class
{

use Bug15015Trait;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return ['only-in-class-dir' => true];
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bug15015;

trait Bug15015Trait
{

public function magicDirInTraitDir(): void
{
include __DIR__ . '/only-in-trait-dir.php';
}

public function magicDirInClassDir(): void
{
include __DIR__ . '/only-in-class-dir.php';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return ['only-in-trait-dir' => true];
Loading