Skip to content

Commit

Permalink
ObjectType - make class line part of the cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 16, 2021
1 parent 36649d3 commit be27584
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
}
Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5639.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug5639;

if (\version_compare(\PHP_VERSION, '7.0', '<'))
{
class Foo extends \Exception {
function __toString(): string {
$result = \sprintf("%s\n\nin %s on line %s", $this->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;
}
}
}

0 comments on commit be27584

Please sign in to comment.