Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix constants #158

Merged
merged 2 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
namespace Humbug;

use const Humbug\DUMMY_CONST as FOO;
\FOO;
\Humbug\FOO;

PHP
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
namespace Humbug;

use const Humbug\DUMMY_CONST;
\DUMMY_CONST;
\Humbug\DUMMY_CONST;

PHP
],
Expand Down
28 changes: 25 additions & 3 deletions specs/const/global-scope-global.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
[
'spec' => <<<'SPEC'
Constant call in the global namespace:
- prefix the constant
- transforms the call into a FQ call
SPEC
,
Expand All @@ -35,15 +36,36 @@

namespace Humbug;

\DUMMY_CONST;
\Humbug\DUMMY_CONST;

PHP
],

[
'spec' => <<<'SPEC'
Internal constant call in the global namespace:
- do not prefix the constant
- transforms the call into a FQ call
SPEC
,
'payload' => <<<'PHP'
<?php

DIRECTORY_SEPARATOR;
----
<?php

namespace Humbug;

\DIRECTORY_SEPARATOR;

PHP
],

[
'spec' => <<<'SPEC'
FQ constant call in the global namespace:
- do nothing
- prefix the constant
SPEC
,
'payload' => <<<'PHP'
Expand All @@ -55,7 +77,7 @@

namespace Humbug;

\DUMMY_CONST;
\Humbug\DUMMY_CONST;

PHP
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
Constant call imported with an aliased use statement:
- prefix the namespace
- prefix the use statement
- prefix the constant call
SPEC
,
'payload' => <<<'PHP'
Expand All @@ -73,7 +74,7 @@
namespace Humbug\A;

use const Humbug\DUMMY_CONST as FOO;
\FOO;
\Humbug\FOO;

PHP
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
FQ constant call imported with a use statement:
- prefix the namespace
- prefix the use statement
- prefix the constant call
SPEC
,
'payload' => <<<'PHP'
Expand All @@ -73,7 +74,7 @@
namespace Humbug\A;

use const Humbug\DUMMY_CONST;
\DUMMY_CONST;
\Humbug\DUMMY_CONST;

PHP
],
Expand Down
3 changes: 2 additions & 1 deletion specs/const/namespace-global.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'spec' => <<<'SPEC'
FQ constant call in a namespace:
- prefix the namespace
- prefix the constant call
SPEC
,
'payload' => <<<'PHP'
Expand All @@ -60,7 +61,7 @@

namespace Humbug\A;

\DUMMY_CONST;
\Humbug\DUMMY_CONST;

PHP
],
Expand Down
4 changes: 2 additions & 2 deletions specs/use/use-const.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
[
'spec' => <<<'SPEC'
Constant use statement for an internal constant belonging to the global namespace:
- prefix the use statement
- do not prefix the use statement
SPEC
,
'payload' => <<<'PHP'
Expand All @@ -57,7 +57,7 @@

namespace Humbug;

use const Humbug\DIRECTORY_SEPARATOR;
use const DIRECTORY_SEPARATOR;

PHP
],
Expand Down
8 changes: 6 additions & 2 deletions src/NodeVisitor/NameStmtPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ private function prefixName(Name $name): Node
}

if ($parentNode instanceof ConstFetch
&& 1 === count($resolvedName->parts)
&& null === $resolvedValue->getUse()
&& (
$this->reflector->isConstantInternal($resolvedName->toString())
// Constants have a fallback autoloading so we cannot prefix them when the name is ambiguous
// See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
|| false === ($resolvedName instanceof FullyQualified)
)
) {
return $resolvedName;
}
Expand Down
4 changes: 4 additions & 0 deletions src/NodeVisitor/UseStmt/UseStmtPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ private function shouldPrefixUseStmt(UseUse $use): bool
return false === $this->reflector->isFunctionInternal($use->name->getFirst());
}

if (Use_::TYPE_CONSTANT === $useType) {
return false === $this->reflector->isConstantInternal($use->name->getFirst());
}

return Use_::TYPE_NORMAL !== $useType || false === $this->reflector->isClassInternal($use->name->getFirst());
}

Expand Down
21 changes: 21 additions & 0 deletions src/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\FunctionReflector;
use function array_key_exists;
use function array_values;
use function get_defined_constants;
use function strtoupper;

final class Reflector
{
private $classReflector;
private $functionReflector;
private $constants;

public function __construct(ClassReflector $classReflector, FunctionReflector $functionReflector)
{
Expand Down Expand Up @@ -51,4 +56,20 @@ public function isFunctionInternal(string $name): bool
return false;
}
}

public function isConstantInternal(string $name): bool
{
if (null === $this->constants) {
$constants = get_defined_constants(true);

unset($constants['user']);

$this->constants = array_merge(...array_values($constants));

unset($constants);
}

// TODO: find a better solution
return array_key_exists(strtoupper($name), $this->constants);
}
}