Skip to content

Commit a44d574

Browse files
committed
Add a rector to replace known defined()s with bool
E.g. if you have `define('STATIC_DEPLOY_WP_ORG_MODE', true);` in constants.php, this will replace all instances of `defined('STATIC_DEPLOY_WP_ORG_MODE')` with `true`. This allows other optimizations like dead code elimination to work.
1 parent ee10b5a commit a44d574

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

rector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
require_once __DIR__ . '/util/rector/RemoveAlwaysFalseIfStatementRector.php';
44
require_once __DIR__ . '/util/rector/RemoveAlwaysTrueIfConditionRector2.php';
5+
require_once __DIR__ . '/util/rector/ReplaceKnownDefinedWithBooleanRector.php';
56

67
use Rector\Config\RectorConfig;
8+
use Rector\Constants\Rector\FuncCall\ReplaceKnownDefinedWithBooleanRector;
79
use Rector\DeadCode\Rector\For_\RemoveDeadContinueRector;
810
use Rector\DeadCode\Rector\If_\RemoveAlwaysFalseIfStatementRector;
911
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector2;
@@ -31,6 +33,7 @@
3133
[
3234
RemoveAlwaysFalseIfStatementRector::class,
3335
RemoveAlwaysTrueIfConditionRector2::class,
36+
ReplaceKnownDefinedWithBooleanRector::class,
3437
]
3538
)
3639
->withSets(
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Constants\Rector\FuncCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\FuncCall;
9+
use PhpParser\Node\Name;
10+
use PhpParser\Node\Scalar\String_;
11+
use PhpParser\Node\Expr\ConstFetch;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* Replace defined() calls with boolean values when the constant is defined in bootstrap
18+
*
19+
* @see \Rector\Tests\DeadCode\Rector\FuncCall\ReplaceDefinedWithBooleanRector\ReplaceDefinedWithBooleanRectorTest
20+
*/
21+
final class ReplaceKnownDefinedWithBooleanRector extends AbstractRector
22+
{
23+
public function getRuleDefinition(): RuleDefinition
24+
{
25+
return new RuleDefinition('Replace defined() calls with boolean true values when the constant is defined in bootstrap', [
26+
new CodeSample(
27+
<<<'CODE_SAMPLE'
28+
if (defined('BOOTSTRAPPED_CONSTANT')) {
29+
// do something
30+
}
31+
CODE_SAMPLE
32+
,
33+
<<<'CODE_SAMPLE'
34+
if (true) {
35+
// do something
36+
}
37+
CODE_SAMPLE
38+
)
39+
]);
40+
}
41+
42+
/**
43+
* @return array<class-string<Node>>
44+
*/
45+
public function getNodeTypes(): array
46+
{
47+
return [FuncCall::class];
48+
}
49+
50+
/**
51+
* @param FuncCall $node
52+
*/
53+
public function refactor(Node $node): ?Node
54+
{
55+
if (!$this->isName($node, 'defined')) {
56+
return null;
57+
}
58+
59+
if (count($node->args) !== 1) {
60+
return null;
61+
}
62+
63+
$firstArg = $node->args[0]->value;
64+
if (!$firstArg instanceof String_) {
65+
return null;
66+
}
67+
68+
if (defined($firstArg->value)) {
69+
return new ConstFetch(new Name('true'));
70+
}
71+
72+
return null;
73+
}
74+
}

0 commit comments

Comments
 (0)