With PHP 7:
Classes, functions and constants being imported from the same namespace can now be grouped together in a single use statement.
Using the PSR2 Standard, the following happens (sample output):
FOUND 2 ERRORS AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------
1 | WARNING | [ ] A file should declare new symbols (classes,
| | functions, constants, etc.) and cause no other
| | side effects, or it should execute logic with
| | side effects, but should not do both. The first
| | symbol is defined on line 15 and the first side
| | effect is on line 13.
13 | ERROR | [x] There must be one USE keyword per declaration
13 | ERROR | [x] Closing brace must be on a line by itself
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
I can get rid of the one USE keyword error excluding PSR2.Namespaces.UseDeclaration. I can also work around the side effects warning excluding PSR1.Files.SideEffects, though not ideal.
However, I have no idea how work around the closing brace error, and I don't want to exclude that rule since I still want it to apply on the rest of the code.
Is there a clean way to work around this issue ?
The side effects and brace errors are caused by PHPCS assigning the curly braces to the function keyword in use function ..., which is incorrect. Fixing that should fix both of those.
Edit: There is already code for that. It's something else.
For the use declaration error, I don't think I can fix that. PSR2 is pretty clear about having one declaration per line, so they would need some errata to change that rule and allow for PHP7 syntax, if they want that to be allowed. But as you've said, you can ignore it in your ruleset.
The issues are actually caused by the curly braces being assigned to the use keyword (or a namespace keyword if you have one up top). Fixing that issue will fix the side effects and brace placement errors.
I've assigned the curly braces new tokens so there is no confusion with sniffs and the tokenizer. It also lets sniffs detect this syntax and apply coding standards to it more easily.
Excellent, thanks :pray:
Did this ever get done and released? I'm on 2.6.1 and php-cs is certainly unhappy with my grouped imports.
Did this ever get done and released? I'm on 2.6.1 and php-cs is certainly unhappy with my grouped imports.
If it is unhappy for reasons like There must be one USE keyword per declaration then that might be because you are using PSR2 and that declares one USE statement per line. I mentioned this in a previous comment as being something I don't think I can change.
If you are getting brace or side effect errors, then please let me know as these should be fixed and the unit tests are currently passing.
Apologies, I misunderstood your previous comments on what exactly was fixed. I'm not getting any undue errors due to parsing, PHP-CS is indeed applying PSR-2 correctly in this case.
Apologies, I misunderstood your previous comments on what exactly was fixed. I'm not getting any undue errors due to parsing, PHP-CS is indeed applying PSR-2 correctly in this case.
No problem. Thanks for getting back to me so quickly.
With respect to PSR-2, I wonder if there is still more to do here. Specifically around the fixer code. As of 2.6.2, the fixer still seems to corrupt PHP 7 grouped use statements.
I'm considering a patch that determines if the following token is a _grouped use curly_ and, if so, expands the grouped declaration to individual use declarations. Would like to know your thoughts before getting started…
I'm considering a patch that determines if the following token is a grouped use curly and, if so, expands the grouped declaration to individual use declarations. Would like to know your thoughts before getting started…
@jasonmccreary I wasn't aware there was an issue (but there clearly is) so I'd be very happy to look at any PR to fix this.
Cool. I'll submit something tomorrow as I have a need for it anyway and am happy to give back to this excellent tool.
I'll submit something tomorrow as I have a need for it anyway
Thanks a lot. If you get stuck, just open an issue and we can discuss options.
Changed title of incorrect issue - please ignore
Has this issue been fully resolved and as part of the update for code sniffer.
Thanks everyone
Has this issue been fully resolved and as part of the update for code sniffer.
This issue was resolved via a PR and released in version 2.6.0. The current stable version is 2.8.1, so it's been around for a few releases.
Hi everyone,
Example.php
<?php
namespace App\Demo;
use App\Model\{A, B};
class Example
{
}
I ran command phpcs --standard=PSR2 Example.php and getting following error There must be one USE keyword per declaration.
phpcs version is 3.0.1 (stable)
Please advice on this.
@nsaumini the problem is not in phpcs. There's no provision in PSR2 for grouped imports, hence phpcs correctly calling out on their usage.
@luispabon Noted thanks.
PSR are working on a new coding style extending PSR2 btw, hopefully it'll be accepted/published soonish:
https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md
That'd include a lot of new stuff that came in on PHP7.
There's only hope left, or we've got any information when it will be published :D ?
any info on this?
<rule ref="PSR2.Namespaces.UseDeclaration">
<exclude-pattern>*</exclude-pattern>
</rule>
in your ruleset.xml resolves a problem while we waiting for update :)
I am using that already, thanks @agriboed
Anyone solved this apart from the exclude pattern above? PHP 7 has been out for a little while now...
phpcs.xml. ruleset.xml <rule ref="PSR2.Namespaces.UseDeclaration">
<exclude-pattern>*</exclude-pattern>
</rule>
phpcs --standard=phpcs.xml --extensions=php appI'm using it for now, to no write directly the ruleset.xmlfile from global composer.
Most helpful comment
in your ruleset.xml resolves a problem while we waiting for update :)