coding-standard
coding-standard copied to clipboard
AlphabeticallySortedUses fixer removes code between use statements
If you enable the SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses rule and run phpcbf on code that has use
statements interspersed with other code (rather than all together at the top of a file as convention dictates), this fixer will remove code between the use
statements.
For example, this code:
$foo = 'bar';
use Foo\Baz;
$bar = 'foo';
use Foo\Bar;
$baz = 'bar';
use Bar\Foo;
when fixed becomes:
$foo = 'bar';
use Bar\Foo;
use Foo\Bar;
use Foo\Baz;
The variable assignment statements that were between use
statements have disappeared.
Yes I have found this too and it is very annoying. I know that that the general convention is to have all use
statements listed together at the top, but when debugging or prototyping a fix I often insert a new use statement right where I am working in a file, to keep it in immediate view. I don't know if it will be needed or not, so I don't scroll right to the top to add a line, then scroll back to where I am working. If I accidentally left one of these use statements there and ran phpcbf to tidy up the file it could delete hundreds of source code lines. Yes, I know I could recover from that, but it would be a pain to suddently find the file trashed, simply by running a utility which is designed to help developers, not make more work.
I could investigate a fix for this, that would detect anything in between two use statements, and then set the $fixable
to false
This fix resulted in a bunch of static function calls located at the top of files being deleted when running the rule across a large codebase.