Skip to content

Commit

Permalink
Fix group use bug (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyuki authored Sep 16, 2020
1 parent 0ef8095 commit fd78875
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
22 changes: 22 additions & 0 deletions specs/use/use-class-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
use A\{B\C, D};
use \A\B\{C\D as ABCD, E};
B::class;
C::class;
D::class;
ABCD::class;
E::class;
----
<?php
Expand All @@ -42,6 +48,11 @@
use Humbug\A\D;
use Humbug\A\B\C\D as ABCD;
use Humbug\A\B\E;
\Humbug\A\B::class;
\Humbug\A\B\C::class;
\Humbug\A\D::class;
\Humbug\A\B\C\D::class;
\Humbug\A\B\E::class;

PHP
,
Expand Down Expand Up @@ -79,6 +90,12 @@
use A\{B\C, D};
use \A\B\{C\G, E};
B::class;
C::class;
D::class;
G::class;
E::class;
----
<?php
Expand All @@ -89,6 +106,11 @@
use Humbug\A\D;
use Humbug\A\B\C\G;
use Humbug\A\B\E;
\Humbug\A\B::class;
\Humbug\A\B\C::class;
\Humbug\A\D::class;
\Humbug\A\B\C\G::class;
\Humbug\A\B\E::class;

PHP
],
Expand Down
11 changes: 11 additions & 0 deletions specs/use/use-const-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
use const A\{B\C, D};
use const \A\B\{C\D as ABCD, E};
B;
C;
D;
ABCD;
E;
----
<?php
Expand All @@ -42,6 +48,11 @@
use const Humbug\A\D;
use const Humbug\A\B\C\D as ABCD;
use const Humbug\A\B\E;
\Humbug\A\B;
\Humbug\A\B\C;
\Humbug\A\D;
\Humbug\A\B\C\D;
\Humbug\A\B\E;

PHP
,
Expand Down
27 changes: 19 additions & 8 deletions specs/use/use-func-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,31 @@
<<<'PHP'
<?php
use A\{b};
use A\{B\c, d};
use \A\B\{C\g, e};
use function A\{b};
use function A\{B\c, d};
use function \A\B\{C\g, e};
b();
c();
d();
g();
e();
----
<?php
namespace Humbug;
use Humbug\A\b;
use Humbug\A\B\c;
use Humbug\A\d;
use Humbug\A\B\C\g;
use Humbug\A\B\e;
use function Humbug\A\b;
use function Humbug\A\B\c;
use function Humbug\A\d;
use function Humbug\A\B\C\g;
use function Humbug\A\B\e;
\Humbug\A\b();
\Humbug\A\B\c();
\Humbug\A\d();
\Humbug\A\B\C\g();
\Humbug\A\B\e();

PHP
,
Expand Down
7 changes: 7 additions & 0 deletions specs/use/use-mix-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
use A\B\{C\D, function b\c, const D};
D::class;
c();
D;
----
<?php
Expand All @@ -38,6 +42,9 @@
use Humbug\A\B\C\D;
use function Humbug\A\B\b\c;
use const Humbug\A\B\D;
\Humbug\A\B\C\D::class;
\Humbug\A\B\b\c();
\Humbug\A\B\D;

PHP
,
Expand Down
13 changes: 5 additions & 8 deletions src/PhpParser/NodeVisitor/UseStmt/UseStmtCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ public function add(?Name $namespaceName, Use_ $use): void
* use Y;
*
* will return the use statement for `Bar\Foo`.
*
* @param Name|null $namespaceName
* @param Name $node
*
* @return null|Name
*/
public function findStatementForNode(?Name $namespaceName, Name $node): ?Name
{
Expand Down Expand Up @@ -125,24 +120,26 @@ private function find(array $useStatements, bool $isFunctionName, bool $isConsta
continue;
}

$type = Use_::TYPE_UNKNOWN !== $use_->type ? $use_->type : $useStatement->type;

if ($name === $useStatement->getAlias()->toLowerString()) {
if ($isFunctionName) {
if (Use_::TYPE_FUNCTION === $use_->type) {
if (Use_::TYPE_FUNCTION === $type) {
return UseStmtManipulator::getOriginalName($useStatement);
}

continue;
}

if ($isConstantName) {
if (Use_::TYPE_CONSTANT === $use_->type) {
if (Use_::TYPE_CONSTANT === $type) {
return UseStmtManipulator::getOriginalName($useStatement);
}

continue;
}

if (Use_::TYPE_NORMAL === $use_->type) {
if (Use_::TYPE_NORMAL === $type) {
// Match the alias
return UseStmtManipulator::getOriginalName($useStatement);
}
Expand Down

0 comments on commit fd78875

Please sign in to comment.