Skip to content

Commit

Permalink
Fix result cache getting stale because of trait @use
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 24, 2022
1 parent 6a4eb02 commit 08703d1
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ jobs:
mv src/Bar.php.orig src/Bar.php
echo -n > phpstan-baseline.neon
../../bin/phpstan -vvv
- script: |
cd e2e/result-cache-3
echo -n > phpstan-baseline.neon
../../bin/phpstan -vvv
patch -b src/Baz.php < patch-1.patch
cat baseline-1.neon > phpstan-baseline.neon
../../bin/phpstan -vvv
mv src/Baz.php.orig src/Baz.php
echo -n > phpstan-baseline.neon
../../bin/phpstan -vvv
steps:
- name: "Checkout"
Expand Down
6 changes: 6 additions & 0 deletions e2e/result-cache-3/baseline-1.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Type TestResultCache3\\\\Baz in generic type TestResultCache3\\\\BarTrait\\<TestResultCache3\\\\Baz\\> in PHPDoc tag @use is not subtype of template type T of Exception of trait TestResultCache3\\\\BarTrait\\.$#"
count: 1
path: src/Foo.php
11 changes: 11 additions & 0 deletions e2e/result-cache-3/patch-1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- src/Baz.php 2022-10-24 14:28:45.000000000 +0200
+++ src/Baz.php 2022-10-24 14:30:02.000000000 +0200
@@ -2,7 +2,7 @@

namespace TestResultCache3;

-class Baz extends \Exception
+class Baz extends \stdClass
{

}
Empty file.
7 changes: 7 additions & 0 deletions e2e/result-cache-3/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan-baseline.neon

parameters:
level: 8
paths:
- src
11 changes: 11 additions & 0 deletions e2e/result-cache-3/src/BarTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace TestResultCache3;

/**
* @template T of \Exception
*/
trait BarTrait
{

}
8 changes: 8 additions & 0 deletions e2e/result-cache-3/src/Baz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TestResultCache3;

class Baz extends \Exception
{

}
11 changes: 11 additions & 0 deletions e2e/result-cache-3/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace TestResultCache3;

class Foo
{

/** @use BarTrait<Baz> */
use BarTrait;

}
18 changes: 18 additions & 0 deletions src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Type;
use function array_merge;
use function count;
Expand All @@ -32,6 +33,7 @@ public function __construct(
private FileHelper $fileHelper,
private ReflectionProvider $reflectionProvider,
private ExportedNodeResolver $exportedNodeResolver,
private FileTypeMapper $fileTypeMapper,
)
{
}
Expand Down Expand Up @@ -260,6 +262,22 @@ public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
foreach ($node->traits as $traitName) {
$this->addClassToDependencies($traitName->toString(), $dependenciesReflections);
}

$docComment = $node->getDocComment();
if ($docComment !== null) {
$usesTags = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$scope->isInClass() ? $scope->getClassReflection()->getName() : null,
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
null,
$docComment->getText(),
)->getUsesTags();
foreach ($usesTags as $usesTag) {
foreach ($usesTag->getType()->getReferencedClasses() as $referencedClass) {
$this->addClassToDependencies($referencedClass, $dependenciesReflections);
}
}
}
} elseif ($node instanceof Node\Expr\Instanceof_) {
if ($node->class instanceof Name) {
$this->addClassToDependencies($scope->resolveName($node->class), $dependenciesReflections);
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
new NameResolver(),
self::getContainer(),
),
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, new ExprPrinter(new Printer()))),
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, new ExprPrinter(new Printer())), $fileTypeMapper),
new RuleErrorTransformer(),
$reportUnmatchedIgnoredErrors,
);
Expand Down

0 comments on commit 08703d1

Please sign in to comment.