Skip to content

Commit 0d4619b

Browse files
committed
Fixed __NAMESPACE__ magic constant value
1 parent fd5902c commit 0d4619b

3 files changed

Lines changed: 6 additions & 16 deletions

File tree

src/Analyser/Scope.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,17 +1079,7 @@ private function resolveType(Expr $node): Type
10791079
} elseif ($node instanceof Node\Scalar\MagicConst\File) {
10801080
return new ConstantStringType($this->getFile());
10811081
} elseif ($node instanceof Node\Scalar\MagicConst\Namespace_) {
1082-
if (!$this->isInClass()) {
1083-
return new ConstantStringType('');
1084-
}
1085-
1086-
$className = $this->getClassReflection()->getName();
1087-
$parts = explode('\\', $className);
1088-
if (count($parts) <= 1) {
1089-
return new ConstantStringType('');
1090-
}
1091-
1092-
return new ConstantStringType($parts[0]);
1082+
return new ConstantStringType($this->namespace ?? '');
10931083
} elseif ($node instanceof Node\Scalar\MagicConst\Method) {
10941084
if ($this->isInAnonymousFunction()) {
10951085
return new ConstantStringType('{closure}');

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,15 +2339,15 @@ public function dataBinaryOperations(): array
23392339
'$file',
23402340
],
23412341
[
2342-
'\'BinaryOperations\'',
2342+
'\'BinaryOperations\\\\NestedNamespace\'',
23432343
'$namespace',
23442344
],
23452345
[
2346-
'\'BinaryOperations\\\\Foo\'',
2346+
'\'BinaryOperations\\\\NestedNamespace\\\\Foo\'',
23472347
'$class',
23482348
],
23492349
[
2350-
'\'BinaryOperations\\\\Foo::doFoo\'',
2350+
'\'BinaryOperations\\\\NestedNamespace\\\\Foo::doFoo\'',
23512351
'$method',
23522352
],
23532353
[
@@ -2508,7 +2508,7 @@ public function dataBinaryOperations(): array
25082508
],
25092509
[
25102510
'true',
2511-
'$foo instanceof \BinaryOperations\Foo',
2511+
'$foo instanceof \BinaryOperations\NestedNamespace\Foo',
25122512
],
25132513
[
25142514
'bool',

tests/PHPStan/Analyser/data/binary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BinaryOperations;
3+
namespace BinaryOperations\NestedNamespace;
44

55
class Foo
66
{

0 commit comments

Comments
 (0)