Skip to content

Commit

Permalink
Fix parent keyword case sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored May 14, 2024
1 parent d50bb22 commit 4e4120a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2484,15 +2484,16 @@ public function resolveName(Name $name): string
{
$originalClass = (string) $name;
if ($this->isInClass()) {
if (in_array(strtolower($originalClass), [
$lowerClass = strtolower($originalClass);
if (in_array($lowerClass, [
'self',
'static',
], true)) {
if ($this->inClosureBindScopeClasses !== [] && $this->inClosureBindScopeClasses !== ['static']) {
return $this->inClosureBindScopeClasses[0];
}
return $this->getClassReflection()->getName();
} elseif ($originalClass === 'parent') {
} elseif ($lowerClass === 'parent') {
$currentClassReflection = $this->getClassReflection();
if ($currentClassReflection->getParentClass() !== null) {
return $currentClassReflection->getParentClass()->getName();
Expand Down
8 changes: 5 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ public function getClassConstFetchTypeByReflection(Name|Expr $class, string $con
}
if (in_array(strtolower($constantClass), $namesToResolve, true)) {
$resolvedName = $this->resolveName($class, $classReflection);
if ($resolvedName === 'parent' && strtolower($constantName) === 'class') {
if (strtolower($resolvedName) === 'parent' && strtolower($constantName) === 'class') {
return new ClassStringType();
}
$constantClassType = $this->resolveTypeByName($class, $classReflection);
Expand Down Expand Up @@ -2024,12 +2024,14 @@ private function resolveName(Name $name, ?ClassReflection $classReflection): str
{
$originalClass = (string) $name;
if ($classReflection !== null) {
if (in_array(strtolower($originalClass), [
$lowerClass = strtolower($originalClass);

if (in_array($lowerClass, [
'self',
'static',
], true)) {
return $classReflection->getName();
} elseif ($originalClass === 'parent') {
} elseif ($lowerClass === 'parent') {
if ($classReflection->getParentClass() !== null) {
return $classReflection->getParentClass()->getName();
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10834.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952b.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/case-insensitive-parent.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10893.php');
}

Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/data/case-insensitive-parent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace CaseInsensitiveParent;

use function PHPStan\Testing\assertType;

class A {
const myConst = '1';

public function doFoo():string {
return "hello";
}

}

class B extends A {
public function doFoo():string {
assertType('string', PARENT::doFoo());
assertType('string', parent::doFoo());

assertType("'1'", PARENT::myConst);
assertType("'1'", parent::myConst);

assertType("'CaseInsensitiveParent\\\\A'", PARENT::class);

return PARENT::doFoo();
}
}

class C extends UnknownParent {
public function doFoo():string {
assertType('class-string', PARENT::class);
}

}

0 comments on commit 4e4120a

Please sign in to comment.