Skip to content

Commit 3e3691b

Browse files
authored
Prefix constants (#158)
1 parent b2e2dbe commit 3e3691b

10 files changed

+66
-12
lines changed

specs/const/global-scope-global-with-single-level-use-statement-and-alias.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
namespace Humbug;
6868
6969
use const Humbug\DUMMY_CONST as FOO;
70-
\FOO;
70+
\Humbug\FOO;
7171

7272
PHP
7373
],

specs/const/global-scope-global-with-single-level-use-statement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
namespace Humbug;
6868
6969
use const Humbug\DUMMY_CONST;
70-
\DUMMY_CONST;
70+
\Humbug\DUMMY_CONST;
7171

7272
PHP
7373
],

specs/const/global-scope-global.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
[
2424
'spec' => <<<'SPEC'
2525
Constant call in the global namespace:
26+
- prefix the constant
2627
- transforms the call into a FQ call
2728
SPEC
2829
,
@@ -35,15 +36,36 @@
3536
3637
namespace Humbug;
3738
38-
\DUMMY_CONST;
39+
\Humbug\DUMMY_CONST;
40+
41+
PHP
42+
],
43+
44+
[
45+
'spec' => <<<'SPEC'
46+
Internal constant call in the global namespace:
47+
- do not prefix the constant
48+
- transforms the call into a FQ call
49+
SPEC
50+
,
51+
'payload' => <<<'PHP'
52+
<?php
53+
54+
DIRECTORY_SEPARATOR;
55+
----
56+
<?php
57+
58+
namespace Humbug;
59+
60+
\DIRECTORY_SEPARATOR;
3961

4062
PHP
4163
],
4264

4365
[
4466
'spec' => <<<'SPEC'
4567
FQ constant call in the global namespace:
46-
- do nothing
68+
- prefix the constant
4769
SPEC
4870
,
4971
'payload' => <<<'PHP'
@@ -55,7 +77,7 @@
5577
5678
namespace Humbug;
5779
58-
\DUMMY_CONST;
80+
\Humbug\DUMMY_CONST;
5981

6082
PHP
6183
],

specs/const/namespace-global-with-single-level-use-statement-and-alias.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
Constant call imported with an aliased use statement:
5858
- prefix the namespace
5959
- prefix the use statement
60+
- prefix the constant call
6061
SPEC
6162
,
6263
'payload' => <<<'PHP'
@@ -73,7 +74,7 @@
7374
namespace Humbug\A;
7475
7576
use const Humbug\DUMMY_CONST as FOO;
76-
\FOO;
77+
\Humbug\FOO;
7778

7879
PHP
7980
],

specs/const/namespace-global-with-single-level-use-statement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
FQ constant call imported with a use statement:
5858
- prefix the namespace
5959
- prefix the use statement
60+
- prefix the constant call
6061
SPEC
6162
,
6263
'payload' => <<<'PHP'
@@ -73,7 +74,7 @@
7374
namespace Humbug\A;
7475
7576
use const Humbug\DUMMY_CONST;
76-
\DUMMY_CONST;
77+
\Humbug\DUMMY_CONST;
7778

7879
PHP
7980
],

specs/const/namespace-global.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'spec' => <<<'SPEC'
4848
FQ constant call in a namespace:
4949
- prefix the namespace
50+
- prefix the constant call
5051
SPEC
5152
,
5253
'payload' => <<<'PHP'
@@ -60,7 +61,7 @@
6061
6162
namespace Humbug\A;
6263
63-
\DUMMY_CONST;
64+
\Humbug\DUMMY_CONST;
6465

6566
PHP
6667
],

specs/use/use-const.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
[
4545
'spec' => <<<'SPEC'
4646
Constant use statement for an internal constant belonging to the global namespace:
47-
- prefix the use statement
47+
- do not prefix the use statement
4848
SPEC
4949
,
5050
'payload' => <<<'PHP'
@@ -57,7 +57,7 @@
5757
5858
namespace Humbug;
5959
60-
use const Humbug\DIRECTORY_SEPARATOR;
60+
use const DIRECTORY_SEPARATOR;
6161

6262
PHP
6363
],

src/NodeVisitor/NameStmtPrefixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ private function prefixName(Name $name): Node
128128
}
129129

130130
if ($parentNode instanceof ConstFetch
131-
&& 1 === count($resolvedName->parts)
132-
&& null === $resolvedValue->getUse()
131+
&& (
132+
$this->reflector->isConstantInternal($resolvedName->toString())
133+
// Constants have a fallback autoloading so we cannot prefix them when the name is ambiguous
134+
// See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
135+
|| false === ($resolvedName instanceof FullyQualified)
136+
)
133137
) {
134138
return $resolvedName;
135139
}

src/NodeVisitor/UseStmt/UseStmtPrefixer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ private function shouldPrefixUseStmt(UseUse $use): bool
7474
return false === $this->reflector->isFunctionInternal($use->name->getFirst());
7575
}
7676

77+
if (Use_::TYPE_CONSTANT === $useType) {
78+
return false === $this->reflector->isConstantInternal($use->name->getFirst());
79+
}
80+
7781
return Use_::TYPE_NORMAL !== $useType || false === $this->reflector->isClassInternal($use->name->getFirst());
7882
}
7983

src/Reflector.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919
use Roave\BetterReflection\Reflector\ClassReflector;
2020
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
2121
use Roave\BetterReflection\Reflector\FunctionReflector;
22+
use function array_key_exists;
23+
use function array_values;
24+
use function get_defined_constants;
25+
use function strtoupper;
2226

2327
final class Reflector
2428
{
2529
private $classReflector;
2630
private $functionReflector;
31+
private $constants;
2732

2833
public function __construct(ClassReflector $classReflector, FunctionReflector $functionReflector)
2934
{
@@ -51,4 +56,20 @@ public function isFunctionInternal(string $name): bool
5156
return false;
5257
}
5358
}
59+
60+
public function isConstantInternal(string $name): bool
61+
{
62+
if (null === $this->constants) {
63+
$constants = get_defined_constants(true);
64+
65+
unset($constants['user']);
66+
67+
$this->constants = array_merge(...array_values($constants));
68+
69+
unset($constants);
70+
}
71+
72+
// TODO: find a better solution
73+
return array_key_exists(strtoupper($name), $this->constants);
74+
}
5475
}

0 commit comments

Comments
 (0)