Skip to content

Commit 8d4796d

Browse files
committed
Revert "Fix various issues with final-overriden class assumptions"
This reverts commit 7848b38.
1 parent 7848b38 commit 8d4796d

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

Diff for: phpstan-baseline.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ parameters:
14191419
-
14201420
message: '#^Doing instanceof PHPStan\\Type\\ObjectType is error\-prone and deprecated\. Use Type\:\:isObject\(\) or Type\:\:getObjectClassNames\(\) instead\.$#'
14211421
identifier: phpstanApi.instanceofType
1422-
count: 2
1422+
count: 3
14231423
path: src/Type/ObjectType.php
14241424

14251425
-

Diff for: src/Type/ObjectType.php

+58-29
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function getUnresolvedPropertyPrototype(string $propertyName, ClassMember
182182
} else {
183183
$canAccessProperty = $scope->getClassReflection()->getName();
184184
}
185-
$description = $this->describe(VerbosityLevel::cache());
185+
$description = $this->describeCache();
186186

187187
if (isset(self::$properties[$description][$propertyName][$canAccessProperty])) {
188188
return self::$properties[$description][$propertyName][$canAccessProperty];
@@ -321,8 +321,14 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
321321
return IsSuperTypeOfResult::createNo();
322322
}
323323

324-
$thisDescription = $this->describe(VerbosityLevel::cache());
325-
$description = $type->describe(VerbosityLevel::cache());
324+
$thisDescription = $this->describeCache();
325+
326+
if ($type instanceof self) {
327+
$description = $type->describeCache();
328+
} else {
329+
$description = $type->describe(VerbosityLevel::cache());
330+
}
331+
326332
if (isset(self::$superTypes[$thisDescription][$description])) {
327333
return self::$superTypes[$thisDescription][$description];
328334
}
@@ -510,33 +516,15 @@ public function describe(VerbosityLevel $level): string
510516
$preciseNameCallback,
511517
$preciseWithSubtracted,
512518
function () use ($preciseWithSubtracted): string {
513-
if ($this->cachedDescription !== null) {
514-
return $this->cachedDescription;
515-
}
516-
517-
$description = $preciseWithSubtracted();
518-
519-
if ($this instanceof GenericObjectType) {
520-
$description .= '<';
521-
$typeDescriptions = [];
522-
foreach ($this->getTypes() as $type) {
523-
$typeDescriptions[] = $type->describe(VerbosityLevel::cache());
524-
}
525-
$description .= '<' . implode(', ', $typeDescriptions) . '>';
526-
}
527-
528519
$reflection = $this->classReflection;
520+
$line = '';
529521
if ($reflection !== null) {
530-
$description .= '-';
531-
$description .= (string) $reflection->getNativeReflection()->getStartLine();
532-
$description .= '-';
533-
534-
if ($reflection->hasFinalByKeywordOverride()) {
535-
$description .= 'f=' . ($reflection->isFinalByKeyword() ? 't' : 'f');
536-
}
522+
$line .= '-';
523+
$line .= (string) $reflection->getNativeReflection()->getStartLine();
524+
$line .= '-';
537525
}
538526

539-
return $this->cachedDescription = $description . $this->describeAdditionalCacheKey();
527+
return $preciseWithSubtracted() . '-' . static::class . '-' . $line . $this->describeAdditionalCacheKey();
540528
},
541529
);
542530
}
@@ -546,6 +534,47 @@ protected function describeAdditionalCacheKey(): string
546534
return '';
547535
}
548536

537+
private function describeCache(): string
538+
{
539+
if ($this->cachedDescription !== null) {
540+
return $this->cachedDescription;
541+
}
542+
543+
if (static::class !== self::class) {
544+
return $this->cachedDescription = $this->describe(VerbosityLevel::cache());
545+
}
546+
547+
$description = $this->className;
548+
549+
if ($this instanceof GenericObjectType) {
550+
$description .= '<';
551+
$typeDescriptions = [];
552+
foreach ($this->getTypes() as $type) {
553+
$typeDescriptions[] = $type->describe(VerbosityLevel::cache());
554+
}
555+
$description .= '<' . implode(', ', $typeDescriptions) . '>';
556+
}
557+
558+
if ($this->subtractedType !== null) {
559+
$description .= $this->subtractedType instanceof UnionType
560+
? sprintf('~(%s)', $this->subtractedType->describe(VerbosityLevel::cache()))
561+
: sprintf('~%s', $this->subtractedType->describe(VerbosityLevel::cache()));
562+
}
563+
564+
$reflection = $this->classReflection;
565+
if ($reflection !== null) {
566+
$description .= '-';
567+
$description .= (string) $reflection->getNativeReflection()->getStartLine();
568+
$description .= '-';
569+
570+
if ($reflection->hasFinalByKeywordOverride()) {
571+
$description .= 'f=' . ($reflection->isFinalByKeyword() ? 't' : 'f');
572+
}
573+
}
574+
575+
return $this->cachedDescription = $description;
576+
}
577+
549578
public function toNumber(): Type
550579
{
551580
if ($this->isInstanceOf('SimpleXMLElement')->yes()) {
@@ -748,7 +777,7 @@ public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAcce
748777
} else {
749778
$canCallMethod = $scope->getClassReflection()->getName();
750779
}
751-
$description = $this->describe(VerbosityLevel::cache());
780+
$description = $this->describeCache();
752781
if (isset(self::$methods[$description][$methodName][$canCallMethod])) {
753782
return self::$methods[$description][$methodName][$canCallMethod];
754783
}
@@ -1237,7 +1266,7 @@ public function getEnumCases(): array
12371266
return [];
12381267
}
12391268

1240-
$cacheKey = $this->describe(VerbosityLevel::cache());
1269+
$cacheKey = $this->describeCache();
12411270
if (array_key_exists($cacheKey, self::$enumCases)) {
12421271
return self::$enumCases[$cacheKey];
12431272
}
@@ -1500,7 +1529,7 @@ public function getAncestorWithClassName(string $className): ?self
15001529
return $this->currentAncestors[$className];
15011530
}
15021531

1503-
$description = $this->describe(VerbosityLevel::cache());
1532+
$description = $this->describeCache();
15041533
if (
15051534
array_key_exists($description, self::$ancestors)
15061535
&& array_key_exists($className, self::$ancestors[$description])

0 commit comments

Comments
 (0)