-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Undefined index in FunctionDeclarationSniff #3917
Comments
@GuySartorelli Scanning code with parse errors is likely to always lead to problems. Most sniffs try their best to bow out silently when a parse error is encountered, but there are so many different type of parse errors/typos possible, it is hard to account for all situations and PHPCS shouldn't have to in the first place. I'd recommend running a PHP linter over your code, like PHP Parallel Lint before passing it off to PHPCS. Alternatively you can include the Please be aware that the results of most sniffs will not be reliable when your code has parse errors. I'm going to suggest closing this issue as this is not an issue with PHPCS, but an issue with your code containing parse errors. |
It seems to me that at the very least the way that PHPCS and PHPCBF display the error should be the same. But it also seems reasonably to me that there would be a mechanism for sniffs to say "this is the line in the file I was up to" so that the error output can indicate which line in the file caused the error. Even if not doing the above, it would be useful if there was a message that said something basic like "This may be the result of parsing errors in your PHP code - please check your PHP code, e.g. with a linter like PHP Parellel Lint" |
PHPCS catches the error and displays the caught error on line 1 of the report for the file and will stop scanning the file. As the report for PHPCBF doesn't display individual errors, the error can't be displayed in the report and is just thrown. There is already an issue open to improve that - see #2871 and #3642.
The error handler which catches the error does not have access to that information. |
Describe the bug
There's sort of two bugs in one here. One is that the error message is displayed differently when running
phpcs
than it is when runningphpcbf
. The other is that the error happens at all.When I run
phpcs
over the below code, it gives a relatively nice error message, and when I runphpcbf
over it gives a nasty looking one - but in both cases the error message doesn't tell me what I need to change about my PHP code to fix it.Code sample
Custom ruleset
N/A
To reproduce
Steps to reproduce the behavior:
test.php
with the code sample above...phpcs --standard=psr12 test.php
phpcbf --standard=psr12 test.php
Expected behavior
Instead of throwing a
Undefined array key -1
error, the output should indicate what is wrong with my PHP code so that I can fix it. In this case, it's missing either:abstract
and a semicolonAlternatively, it should at least give me some hints to help me find the problem myself (e.g. tell me which line in the PHP file triggered the error - it currently incorrectly reports line 1) - with clean output in both
phpcs
andphpcbf
Versions (please complete the following information)
Additional context
I found this while working on linting developer documentation using a wrapper I'm developing which passes in markdown php code blocks to be linted, which is why the example is a bit weird - but the instructions above all work with just regular old sniffer.
Please confirm:
master
branch of PHP_CodeSniffer.The text was updated successfully, but these errors were encountered: