-
Notifications
You must be signed in to change notification settings - Fork 506
Introduce IgnoreErrorExtension #3783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hi, this is exactly what I'm looking for right now as I'm working on Magento 2 projects when I'm bit of forced into following error:
I do not want to ignore all missingType.parameter errors in I'd use this proposed feature or I can use configuration with something like this:
And for the requirement to have strongly typed first argument and ignore error based on that - I can then create custom rule to check this condition. Maybe you can do that too in your case. |
@LoogleCZ Yes, with this extension you would be able to filter errors based on their class, namespace, if they use an attribute, if this extend from some base class, etc... Curious what Ondrej thinks about it 😊 |
Yeah, this would be a nice addition. But there's a lot of details that we need to get right (because ignoring errors is one of the most popular and most widely used PHPStan feature!). Some notes I have after thinking about this:
|
Sure
If we would first want to process the ignoreErrors, then the current place where I call the extension does not make sense. How would we be able to get the Node at the ignoreErrors stage? I think that information is not part of the Error, right? Is it even possible to put the Node in the Error, and restore that from the result cache?
Sure, we can first call the RuleErrorTransformer and then pass the Error to the filter.
Will take care of this later once we iron out 2 + 3 😉 |
Yeah, if you need to access |
So to summarize:
Thank you! |
702d577
to
f7021e9
Compare
I pushed the requested changes, please have a look when you have time. When you think it looks OK, I will continue with adding tests and tweaking things. |
I totally forgot about this! It got lost in my 100+ unread-emails-inbox. But it occured to me it might be a solution to a problem someone's faced so here I am again to give some feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise 👍
f7021e9
to
ae24523
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good!
Now for the tests. I'd be completely fine with just E2E tests in this manner:
phpstan-src/.github/workflows/e2e-tests.yml
Lines 216 to 245 in ee7eb11
- script: | | |
cd e2e/bad-exclude-paths | |
cp -r tmp-node-modules node_modules | |
OUTPUT=$(../../bin/phpstan analyse -c ignoreNonexistentExcludePath.neon) | |
echo "$OUTPUT" | |
- script: | | |
cd e2e/bad-exclude-paths | |
OUTPUT=$(../../bin/phpstan analyse -c ignoreReportUnmatchedFalse.neon) | |
echo "$OUTPUT" | |
- script: | | |
cd e2e/bug-11826 | |
composer install | |
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan") | |
echo "$OUTPUT" | |
../bashunit -a contains 'Child process error (exit code 255): PHP Fatal error' "$OUTPUT" | |
../bashunit -a contains 'Result is incomplete because of severe errors.' "$OUTPUT" | |
- script: | | |
cd e2e/bug-11857 | |
composer install | |
../../bin/phpstan | |
- script: | | |
cd e2e/result-cache-meta-extension | |
composer install | |
../../bin/phpstan -vvv | |
../../bin/phpstan -vvv --fail-without-result-cache | |
echo 'modified-hash' > hash.txt | |
OUTPUT=$(../bashunit -a exit_code "2" "../../bin/phpstan -vvv --fail-without-result-cache") | |
echo "$OUTPUT" | |
../bashunit -a matches "Note: Using configuration file .+phpstan.neon." "$OUTPUT" | |
../bashunit -a contains 'Result cache not used because the metadata do not match: metaExtensions' "$OUTPUT" |
This comment was marked as resolved.
This comment was marked as resolved.
This pull request has been marked as ready for review. |
@ondrejmirtes It's ready for review 🎉 |
if ($error->canBeIgnored()) { | ||
foreach ($this->ignoreErrorExtensionProvider->getExtensions() as $ignoreErrorExtension) { | ||
if ($ignoreErrorExtension->shouldIgnore($error, $node, $scope)) { | ||
continue 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if I comment this change, all tests are passing. Please add a collector + CollectedDataNode rule with an error to the E2E test, and ignore it with an extension, so we can verify this code does what it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I added the tests. While doing it, I wonder, what the value of this would be. Since the Node points to the CollectedDataNode, that contains all data.
I think in real world scenario's, this is not interesting, but maybe I don't see it yet.
5f13e90
to
3efd52a
Compare
This allows for more flexibility in ignoring errors. Some use cases: * Ignore `missingType.iterableValue` on controller actions: Rule: when the method is public and it has `#[Route]` attribute or the class has `#[AsController]` attribute. * Ignore `should return int but returns int|null` on `getId` for entities. Rule: class needs to have `#[Entity]` attribute. * Ignore `never returns null so it can be removed from the return type` Rule: method needs to have `#[GraphQL\Field]` attribute. * Enforce missingCheckedExceptionInThrows partially, only for specific classes.
3efd52a
to
7f37ecc
Compare
@ondrejmirtes It's ready again. The failures are unrelated to this PR AFAIK. |
Thank you! Please contribut the documentation now. Should be very similar to phpstan/phpstan@3fa1a7c |
🫡 Thanks for the merge, the docs are added here: |
With the release of 2.1.7 I immediately added my own IgnoreErrorExtension and it didn't work as expected. After a lot of trial and error, I noticed it was related to some cache. Even though I did run So that makes me wonder, is there anything we should do in the caching system? When such extension is added, it should invalidate the cache, so that all errors will be passed through the method. @ondrejmirtes I don't know too much about how this works, but if you have a hint, I can implement / fix it. Maybe it's similar to #3765 |
There's no other cache besides the one deleted by clear-result-cache. Whole analysis runs again and FileAnalyser is called for all analysed files. First you should look into logic in your new extension, maybe there's a bug in it. If that doesn't help, please reproduce the issue in a public repository so I can take a look. |
Nevermind. Turns out I forgot to pass |
I don't really approve or endose using .php as main config file format. Although it works, you should prefer .neon which is auto-discovered and this mistake wouldn't happen to you :) .php is officially supported for generating and reading baselines (for performance reasons). Aside from that, I also sometimes recommend it to conditionally include other .neon files, like here: https://github.com/phpstan/phpstan-src/blob/2.1.x/build/ignore-by-php-version.neon.php |
I'm aware, and you also explained this somewhere else where you said you want to eventually / maybe work on a PHP class configuration format. For what it's worth: For us, the benefits of using PHP outweigh for yet another config format (PHPStan is the only one using Neon in our project). <?php declare(strict_types=1);
use Symfony\Component\Finder\Finder;
$files = Finder::create()->files()->name('*.php')->sortByName()->in(__DIR__ . '/src-dev/PHPStan/config');
return [
'includes' => [
__DIR__ . '/phpstan-baseline.php',
...array_values(array_map(fn ($file) => $file->getRealPath(), iterator_to_array($files))),
],
// ... The above works so neat. Simply add a new file to the config directory, and PHPStan picks it up. |
btw: I just realized another use-case for this extension type. we are now able to easily filter errors for 3rd party rules. we no longer need to copy/paste these rules or use patch files to make them work for us, as we need it in our context |
@staabm I don't get it. You can filter errors by identifiers already in the config. |
with this error extension we can filter them more fine-grained though. |
Yeah, sure :) |
This allows for more flexibility in ignoring errors.
Some use cases:
missingType.iterableValue
on controller actions:Rule: when the method is public and it has
#[Route]
attribute or theclass has
#[AsController]
attribute.should return int but returns int|null
ongetId
for entities.Rule: class needs to have
#[Entity]
attribute.never returns null so it can be removed from the return type
Rule: method needs to have
#[GraphQL\Field]
attribute.This idea came from phpstan/phpstan#11134
Note
@ondrejmirtes Before I continue with adding tests and improving things, I would first like to know if agree with this functionality.