Try Rector
Hire Team
Find Rule
NEW
Contact
Blog
Docs
Create custom Rector rule and see how it works:
Custom Rector rule
<?php declare(strict_types=1); use PhpParser\Node; use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Scalar\String_; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; final class CustomRuleRector extends AbstractRector { public function getNodeTypes(): array { return [String_::class]; } /** * @param String_ $node */ public function refactor(Node $node) { // change to number return new Int_(100); } public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('...', []); } }
PHP snippet to change
<?php $result = mt_rand(1, 2); if ($result === 'value') { return 'yes'; } return 'no';
Run Rector