Skip to content

Commit

Permalink
feat: Allow shortening symbols from multi-use statements (only classe…
Browse files Browse the repository at this point in the history
…s for now) (#7816)
  • Loading branch information
Wirone committed Feb 9, 2024
1 parent 8210c16 commit 0bb1e4b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Fixer/Import/FullyQualifiedStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$uses = [];
$lastUse = null;

foreach ($namespaceUsesAnalyzer->getDeclarationsInNamespace($tokens, $namespace) as $use) {
foreach ($namespaceUsesAnalyzer->getDeclarationsInNamespace($tokens, $namespace, true) as $use) {
if (!$use->isClass()) {
continue;
}
Expand Down
126 changes: 126 additions & 0 deletions tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,132 @@ public function getValue()
null,
['import_symbols' => true],
];

// TODO: Ensure shortening for imported functions and constants
yield 'Shorten symbol from comma-separated multi-use statement' => [
<<<'EOD'
<?php
namespace Foo;
use Bar\Service1, Bar\Service2;
use function Bar\func1, Bar\func2;
use const Bar\CONST1, Bar\CONST2;
\Bar\func1(new Service1(\Bar\CONST1));
\Bar\func2(new Service2(\Bar\CONST2));
EOD,
<<<'EOD'
<?php
namespace Foo;
use Bar\Service1, Bar\Service2;
use function Bar\func1, Bar\func2;
use const Bar\CONST1, Bar\CONST2;
\Bar\func1(new \Bar\Service1(\Bar\CONST1));
\Bar\func2(new \Bar\Service2(\Bar\CONST2));
EOD,
];

// TODO: Ensure shortening for imported functions and constants
yield 'Shorten symbol from multi-line, comma-separated multi-use statement, with some noise here and there' => [
<<<'EOD'
<?php
namespace Foo;
use Bar\Service1, /* MAKE SOME NOOOOOISE! */
/* MAKE SOME NOOOOOISE! */ Bar\Service2;
use function Bar\func1, // MAKE SOME NOOOOOISE!
/** MAKE SOME NOOOOOISE! */ Bar\func2;
use const /* MAKE SOME NOOOOOISE! */ Bar\CONST1,
Bar\CONST2; # MAKE SOME NOOOOOISE!
\Bar\func1(new Service1(\Bar\CONST1));
\Bar\func2(new Service2(\Bar\CONST2));
EOD,
<<<'EOD'
<?php
namespace Foo;
use Bar\Service1, /* MAKE SOME NOOOOOISE! */
/* MAKE SOME NOOOOOISE! */ Bar\Service2;
use function Bar\func1, // MAKE SOME NOOOOOISE!
/** MAKE SOME NOOOOOISE! */ Bar\func2;
use const /* MAKE SOME NOOOOOISE! */ Bar\CONST1,
Bar\CONST2; # MAKE SOME NOOOOOISE!
\Bar\func1(new \Bar\Service1(\Bar\CONST1));
\Bar\func2(new \Bar\Service2(\Bar\CONST2));
EOD,
];

// TODO: Ensure shortening for imported functions and constants
yield 'Shorten symbol from grouped multi-use statement' => [
<<<'EOD'
<?php
namespace Foo;
use Bar\{Service1, Service2};
use function Bar\{func1, func2};
use const Bar\{CONST1, CONST2};
\Bar\func1(new Service1(\Bar\CONST1));
\Bar\func2(new Service2(\Bar\CONST2));
EOD,
<<<'EOD'
<?php
namespace Foo;
use Bar\{Service1, Service2};
use function Bar\{func1, func2};
use const Bar\{CONST1, CONST2};
\Bar\func1(new \Bar\Service1(\Bar\CONST1));
\Bar\func2(new \Bar\Service2(\Bar\CONST2));
EOD,
];

// TODO: Ensure shortening for imported functions and constants
yield 'Shorten symbol from multi-line grouped multi-use statement with some noise here and there' => [
<<<'EOD'
<?php
namespace Foo;
use Bar\{
/* MAKE SOME NOOOOOISE! */ Service1,
Service2 /* MAKE SOME NOOOOOISE! */
};
use function Bar\{
func1, // MAKE SOME NOOOOOISE!
/** MAKE SOME NOOOOOISE! */ func2
};
use const Bar\{
/* MAKE SOME NOOOOOISE! */ CONST1,
CONST2 # MAKE SOME NOOOOOISE!
};
\Bar\func1(new Service1(\Bar\CONST1));
\Bar\func2(new Service2(\Bar\CONST2));
EOD,
<<<'EOD'
<?php
namespace Foo;
use Bar\{
/* MAKE SOME NOOOOOISE! */ Service1,
Service2 /* MAKE SOME NOOOOOISE! */
};
use function Bar\{
func1, // MAKE SOME NOOOOOISE!
/** MAKE SOME NOOOOOISE! */ func2
};
use const Bar\{
/* MAKE SOME NOOOOOISE! */ CONST1,
CONST2 # MAKE SOME NOOOOOISE!
};
\Bar\func1(new \Bar\Service1(\Bar\CONST1));
\Bar\func2(new \Bar\Service2(\Bar\CONST2));
EOD,
];
}

/**
Expand Down

0 comments on commit 0bb1e4b

Please sign in to comment.