Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,37 @@ jobs:
echo "$OUTPUT"
../bashunit assert contains 'Result cache not used because the metadata do not match: executedFilesHashes' "$OUTPUT"
../bashunit assert contains 'Foo.php:10:Method ResultCacheE2EBootstrap\Foo::doFoo() should return int but returns string.' "$OUTPUT"
- script: |
cd e2e/result-cache-trait-ignore
rm -rf tmp/cache tmp/resultCache.php
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
echo "$OUTPUT"
../bashunit assert contains 'Result cache is saved.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\A):10:Ignore with identifier method.notFound has no comment.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\B):10:Ignore with identifier method.notFound has no comment.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\A):12:Ignoring all errors on a line is not allowed.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\B):12:Ignoring all errors on a line is not allowed.' "$OUTPUT"
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
echo "$OUTPUT"
../bashunit assert contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\A):10:Ignore with identifier method.notFound has no comment.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\B):10:Ignore with identifier method.notFound has no comment.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\A):12:Ignoring all errors on a line is not allowed.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\B):12:Ignoring all errors on a line is not allowed.' "$OUTPUT"
cp src/A.php tmp/A.php
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw --tmp-file tmp/A.php --instead-of src/A.php")
echo "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\A):10:Ignore with identifier method.notFound has no comment.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\A):12:Ignoring all errors on a line is not allowed.' "$OUTPUT"
rm -f tmp/A.php
patch -b src/A.php < removeTrait.patch
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
echo "$OUTPUT"
../bashunit assert contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
../bashunit assert not_contains 'in context of class Repro\A' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\B):10:Ignore with identifier method.notFound has no comment.' "$OUTPUT"
../bashunit assert contains 'MyTrait.php (in context of class Repro\B):12:Ignoring all errors on a line is not allowed.' "$OUTPUT"
mv src/A.php.orig src/A.php
- script: |
cd e2e/result-cache-traits
../../bin/phpstan analyse
Expand Down
6 changes: 6 additions & 0 deletions e2e/result-cache-trait-ignore/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 5
paths:
- src
tmpDir: tmp
reportIgnoresWithoutComments: true
8 changes: 8 additions & 0 deletions e2e/result-cache-trait-ignore/removeTrait.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--- src/A.php
+++ src/A.php
@@ -4,5 +4,4 @@

class A
{
- use MyTrait;
}
8 changes: 8 additions & 0 deletions e2e/result-cache-trait-ignore/src/A.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Repro;

class A
{
use MyTrait;
}
8 changes: 8 additions & 0 deletions e2e/result-cache-trait-ignore/src/B.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Repro;

class B
{
use MyTrait;
}
14 changes: 14 additions & 0 deletions e2e/result-cache-trait-ignore/src/MyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Repro;

trait MyTrait
{
public function doFoo(): void
{
// @phpstan-ignore method.notFound
$this->undefinedMethod();
// @phpstan-ignore-next-line
$this->anotherUndefinedMethod();
}
}
2 changes: 2 additions & 0 deletions e2e/result-cache-trait-ignore/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*
9 changes: 3 additions & 6 deletions src/Analyser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,20 @@ public function getFilePath(): string

public function changeFilePath(string $newFilePath): self
{
if ($this->traitFilePath !== null) {
throw new ShouldNotHappenException('Errors in traits not yet supported');
}

return new self(
$this->message,
$newFilePath,
$this->traitFilePath !== null ? $this->file : $newFilePath,
$this->line,
$this->canBeIgnored,
$newFilePath,
null,
$this->traitFilePath,
$this->tip,
$this->nodeLine,
$this->nodeType,
$this->identifier,
$this->metadata,
$this->fixedErrorDiff,
$this->traitContexts,
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public function analyseFile(
$ignoredFile,
$line,
false,
$ignoredFile,
null,
$file,
$nodeCallback->getTraitFilePaths()[$ignoredFile] ?? null,
'Use @phpstan-ignore with an identifier and a comment (in parentheses).',
))->withIdentifier('ignore.allLineErrors');
unset($linesToIgnore[$ignoredFile][$line]);
Expand All @@ -174,8 +174,8 @@ public function analyseFile(
$ignoredFile,
$line,
false,
$ignoredFile,
null,
$file,
$nodeCallback->getTraitFilePaths()[$ignoredFile] ?? null,
'Explain why this ignore is necessary in parentheses after the identifier.',
))->withIdentifier('ignore.noComment');
unset($linesToIgnore[$ignoredFile][$line]);
Expand Down
12 changes: 12 additions & 0 deletions src/Analyser/FileAnalyserCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ final class FileAnalyserCallback
/** @var LinesToIgnore */
private array $unmatchedLineIgnores;

/** @var array<string, string> */
private array $traitFilePaths = [];

/**
* The registries answer per node class and cache that themselves; this keeps the answer for the
* classes met in this file, so that the walk does not ask them again for every node of a class.
Expand Down Expand Up @@ -119,6 +122,7 @@ public function __invoke(Node $node, Scope $scope): void

$traitFileName = $node->getTraitReflection()->getFileName();
if ($traitFileName !== null) {
$this->traitFilePaths[$fileDescription] = $traitFileName;
$this->processedFiles[] = $traitFileName;
}
}
Expand Down Expand Up @@ -314,6 +318,14 @@ public function getUnmatchedLineIgnores(): array
return $this->unmatchedLineIgnores;
}

/**
* @return array<string, string>
*/
public function getTraitFilePaths(): array
{
return $this->traitFilePaths;
}

/**
* @return list<Error>
*/
Expand Down
Loading