Skip to content

Commit 4e4120a

Browse files
authored
Fix parent keyword case sensitivity
1 parent d50bb22 commit 4e4120a

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,15 +2484,16 @@ public function resolveName(Name $name): string
24842484
{
24852485
$originalClass = (string) $name;
24862486
if ($this->isInClass()) {
2487-
if (in_array(strtolower($originalClass), [
2487+
$lowerClass = strtolower($originalClass);
2488+
if (in_array($lowerClass, [
24882489
'self',
24892490
'static',
24902491
], true)) {
24912492
if ($this->inClosureBindScopeClasses !== [] && $this->inClosureBindScopeClasses !== ['static']) {
24922493
return $this->inClosureBindScopeClasses[0];
24932494
}
24942495
return $this->getClassReflection()->getName();
2495-
} elseif ($originalClass === 'parent') {
2496+
} elseif ($lowerClass === 'parent') {
24962497
$currentClassReflection = $this->getClassReflection();
24972498
if ($currentClassReflection->getParentClass() !== null) {
24982499
return $currentClassReflection->getParentClass()->getName();

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@ public function getClassConstFetchTypeByReflection(Name|Expr $class, string $con
17911791
}
17921792
if (in_array(strtolower($constantClass), $namesToResolve, true)) {
17931793
$resolvedName = $this->resolveName($class, $classReflection);
1794-
if ($resolvedName === 'parent' && strtolower($constantName) === 'class') {
1794+
if (strtolower($resolvedName) === 'parent' && strtolower($constantName) === 'class') {
17951795
return new ClassStringType();
17961796
}
17971797
$constantClassType = $this->resolveTypeByName($class, $classReflection);
@@ -2024,12 +2024,14 @@ private function resolveName(Name $name, ?ClassReflection $classReflection): str
20242024
{
20252025
$originalClass = (string) $name;
20262026
if ($classReflection !== null) {
2027-
if (in_array(strtolower($originalClass), [
2027+
$lowerClass = strtolower($originalClass);
2028+
2029+
if (in_array($lowerClass, [
20282030
'self',
20292031
'static',
20302032
], true)) {
20312033
return $classReflection->getName();
2032-
} elseif ($originalClass === 'parent') {
2034+
} elseif ($lowerClass === 'parent') {
20332035
if ($classReflection->getParentClass() !== null) {
20342036
return $classReflection->getParentClass()->getName();
20352037
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,7 @@ public function dataFileAsserts(): iterable
14781478
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10834.php');
14791479
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952.php');
14801480
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952b.php');
1481+
yield from $this->gatherAssertTypes(__DIR__ . '/data/case-insensitive-parent.php');
14811482
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10893.php');
14821483
}
14831484

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace CaseInsensitiveParent;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class A {
8+
const myConst = '1';
9+
10+
public function doFoo():string {
11+
return "hello";
12+
}
13+
14+
}
15+
16+
class B extends A {
17+
public function doFoo():string {
18+
assertType('string', PARENT::doFoo());
19+
assertType('string', parent::doFoo());
20+
21+
assertType("'1'", PARENT::myConst);
22+
assertType("'1'", parent::myConst);
23+
24+
assertType("'CaseInsensitiveParent\\\\A'", PARENT::class);
25+
26+
return PARENT::doFoo();
27+
}
28+
}
29+
30+
class C extends UnknownParent {
31+
public function doFoo():string {
32+
assertType('class-string', PARENT::class);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)