From be27584056cbfe25b6a0e875d3e42e1fcd7efb31 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 16 Sep 2021 21:49:18 +0200 Subject: [PATCH] ObjectType - make class line part of the cache key --- src/Type/ObjectType.php | 17 +++++++++++++- .../Analyser/AnalyserIntegrationTest.php | 6 +++++ tests/PHPStan/Analyser/data/bug-5639.php | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Analyser/data/bug-5639.php diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index f52093a9f9..2214826e94 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -391,7 +391,15 @@ public function describe(VerbosityLevel $level): string $preciseNameCallback, $preciseWithSubtracted, function () use ($preciseWithSubtracted): string { - return $preciseWithSubtracted() . '-' . static::class . '-' . $this->describeAdditionalCacheKey(); + $reflection = $this->classReflection; + $line = ''; + if ($reflection !== null) { + $line .= '-'; + $line .= (string) $reflection->getNativeReflection()->getStartLine(); + $line .= '-'; + } + + return $preciseWithSubtracted() . '-' . static::class . '-' . $line . $this->describeAdditionalCacheKey(); } ); } @@ -412,6 +420,13 @@ private function describeCache(): string $description .= sprintf('~%s', $this->subtractedType->describe(VerbosityLevel::cache())); } + $reflection = $this->classReflection; + if ($reflection !== null) { + $description .= '-'; + $description .= (string) $reflection->getNativeReflection()->getStartLine(); + $description .= '-'; + } + return $description; } diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index 68235a4c5a..0583d9c9b1 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -441,6 +441,12 @@ public function testBug5527(): void $this->assertCount(0, $errors); } + public function testBug5639(): void + { + $errors = $this->runAnalyse(__DIR__ . '/data/bug-5639.php'); + $this->assertCount(0, $errors); + } + /** * @param string $file * @return \PHPStan\Analyser\Error[] diff --git a/tests/PHPStan/Analyser/data/bug-5639.php b/tests/PHPStan/Analyser/data/bug-5639.php new file mode 100644 index 0000000000..c0eeed3370 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-5639.php @@ -0,0 +1,22 @@ +message, $this->file, $this->line); + return $result; + } + } +} +else +{ + class Foo extends \Error { + function __toString(): string { + $result = \sprintf("%s\n\nin %s on line %s", $this->message, $this->file, $this->line); + return $result; + } + } +}