xref: /dokuwiki/_test/rector/DokuWikiRenamePrintToEcho.php (revision 33b331541831ff69d22f79b2ad12d36f4fe3dd39)
126dfc232SAndreas Gohr<?php
226dfc232SAndreas Gohr
326dfc232SAndreas Gohrnamespace dokuwiki\test\rector;
426dfc232SAndreas Gohr
526dfc232SAndreas Gohruse PhpParser\Node;
626dfc232SAndreas Gohruse PhpParser\Node\Expr\Print_;
726dfc232SAndreas Gohruse PhpParser\Node\Stmt\Echo_;
826dfc232SAndreas Gohruse PhpParser\Node\Stmt\Expression;
9*33b33154SAndreas Gohruse Rector\Rector\AbstractRector;
1026dfc232SAndreas Gohruse Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1126dfc232SAndreas Gohruse Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1226dfc232SAndreas Gohr
1326dfc232SAndreas Gohr/**
1426dfc232SAndreas Gohr * Replace print calls with echo
1526dfc232SAndreas Gohr */
1626dfc232SAndreas Gohrclass DokuWikiRenamePrintToEcho extends AbstractRector
1726dfc232SAndreas Gohr{
1826dfc232SAndreas Gohr
1926dfc232SAndreas Gohr    /** @inheritdoc */
2026dfc232SAndreas Gohr    public function getRuleDefinition(): RuleDefinition
2126dfc232SAndreas Gohr    {
2226dfc232SAndreas Gohr        return new RuleDefinition('Replace print calls with echo', [
2326dfc232SAndreas Gohr            new CodeSample(
2426dfc232SAndreas Gohr                <<<'CODE_SAMPLE'
2526dfc232SAndreas Gohrprint 'Hello World';
2626dfc232SAndreas GohrCODE_SAMPLE,
2726dfc232SAndreas Gohr                <<<'CODE_SAMPLE'
2826dfc232SAndreas Gohrecho 'Hello World';
2926dfc232SAndreas GohrCODE_SAMPLE
3026dfc232SAndreas Gohr            ),
3126dfc232SAndreas Gohr        ]);
3226dfc232SAndreas Gohr    }
3326dfc232SAndreas Gohr
3426dfc232SAndreas Gohr    /** @inheritdoc */
3526dfc232SAndreas Gohr    public function getNodeTypes(): array
3626dfc232SAndreas Gohr    {
3726dfc232SAndreas Gohr        return [Expression::class];
3826dfc232SAndreas Gohr    }
3926dfc232SAndreas Gohr
4026dfc232SAndreas Gohr    /** @inheritdoc */
4126dfc232SAndreas Gohr    public function refactor(Node $node)
4226dfc232SAndreas Gohr    {
4326dfc232SAndreas Gohr        if (!$node->expr instanceof Print_) {
4426dfc232SAndreas Gohr            return null;
4526dfc232SAndreas Gohr        }
4626dfc232SAndreas Gohr        return new Echo_([$node->expr->expr], $node->getAttributes());
4726dfc232SAndreas Gohr    }
4826dfc232SAndreas Gohr}
49