Is there a single rule for Use of undefined constant
in array keys
#7015
-
In a very old codebase (pre PHP5.3) there are lots of examples of array keys being accessed without quotes: $foo = array('x' => 'y');
$bar = $foo[x] Which triggers a warning:
I would like a rule that quotes all undefined constants in array keys. eg: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You may can create custom rector rule for it: <?php
declare(strict_types=1);
namespace Utils\Rector\ConstFetch;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class QuoteConstFetchRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Quote constant not exists', [
new CodeSample(
<<<'CODE_SAMPLE'
$foo = array('x' => 'y');
$bar = $foo[x]
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$foo = array('x' => 'y');
$bar = $foo['x']
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\ConstFetch::class];
}
/**
* @param Node\Expr\ConstFetch $node
*/
public function refactor(Node $node): ?Node
{
$constantName = $this->getName($node);
if ($constantName === null) {
return null;
}
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($this->reflectionProvider->hasConstant(new Node\Name($constantName), $scope)) {
return null;
}
return new Node\Scalar\String_($constantName);
}
} Then, update composer autoload to add the path of new rector service: "autoload": {
"psr-4": {
"Utils\\": "utils/"
}
}, run composer dump-autoload -o composer dump-autoload -o Then register to your rector.php <?php
declare(strict_types=1);
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Utils\Rector\ConstFetch\QuoteConstFetchRector;
return static function (ContainerConfigurator $containerConfigurator): void {
// other config here
$services = $containerConfigurator->services();
$services->set(QuoteConstFetchRector::class);
}; Then run it: vendor/bin/rector process test2.php --clear-cache
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================
1) test2.php:0
---------- begin diff ----------
@@ @@
<?php
$foo = array('x' => 'y');
-$bar = $foo[x];
+$bar = $foo['x'];
----------- end diff -----------
Applied rules:
* QuoteConstFetchRector
[OK] 1 file has been changed by Rector
For note, constant not exists may depends on phpstan autoload it, if you have a constant from other file (eg on include file), you may need to config bootstrap files by update your rector.php to include the bootstrap files that define constants: <?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Utils\Rector\ConstFetch\QuoteConstFetchRector;
return static function (ContainerConfigurator $containerConfigurator): void {
// other config here
$services = $containerConfigurator->services();
$services->set(QuoteConstFetchRector::class);
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::BOOTSTRAP_FILES, [
__DIR__ . '/constants.php',
__DIR__ . '/project/special/autoload.php',
]);
}; |
Beta Was this translation helpful? Give feedback.
-
Thanks @samsonasik I'll take a look and tweak where needed. |
Beta Was this translation helpful? Give feedback.
You may can create custom rector rule for it: