coding-standard icon indicating copy to clipboard operation
coding-standard copied to clipboard

AlphabeticallySortedUses fixer removes code between use statements

Open mfb opened this issue 1 year ago • 3 comments

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.

mfb avatar Jul 17 '23 04:07 mfb

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.

jonathan1055 avatar Aug 23 '23 12:08 jonathan1055

I could investigate a fix for this, that would detect anything in between two use statements, and then set the $fixable to false

jonathan1055 avatar Aug 23 '23 12:08 jonathan1055

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.

davidrans avatar Mar 06 '24 18:03 davidrans