xref: /dokuwiki/_test/rector/DokuWikiRenamePrintToEcho.php (revision 26dfc2323f8f70cb69aac4c8c51bf7997809f2ca)
1*26dfc232SAndreas Gohr<?php
2*26dfc232SAndreas Gohr
3*26dfc232SAndreas Gohrnamespace dokuwiki\test\rector;
4*26dfc232SAndreas Gohr
5*26dfc232SAndreas Gohruse PhpParser\Node;
6*26dfc232SAndreas Gohruse PhpParser\Node\Expr\FuncCall;
7*26dfc232SAndreas Gohruse PhpParser\Node\Expr\Print_;
8*26dfc232SAndreas Gohruse PhpParser\Node\Stmt\Echo_;
9*26dfc232SAndreas Gohruse PhpParser\Node\Stmt\Expression;
10*26dfc232SAndreas Gohruse Rector\Core\Rector\AbstractRector;
11*26dfc232SAndreas Gohruse Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12*26dfc232SAndreas Gohruse Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13*26dfc232SAndreas Gohr
14*26dfc232SAndreas Gohr/**
15*26dfc232SAndreas Gohr * Replace print calls with echo
16*26dfc232SAndreas Gohr */
17*26dfc232SAndreas Gohrclass DokuWikiRenamePrintToEcho extends AbstractRector
18*26dfc232SAndreas Gohr{
19*26dfc232SAndreas Gohr
20*26dfc232SAndreas Gohr    /** @inheritdoc */
21*26dfc232SAndreas Gohr    public function getRuleDefinition(): RuleDefinition
22*26dfc232SAndreas Gohr    {
23*26dfc232SAndreas Gohr        return new RuleDefinition('Replace print calls with echo', [
24*26dfc232SAndreas Gohr            new CodeSample(
25*26dfc232SAndreas Gohr                <<<'CODE_SAMPLE'
26*26dfc232SAndreas Gohrprint 'Hello World';
27*26dfc232SAndreas GohrCODE_SAMPLE,
28*26dfc232SAndreas Gohr                <<<'CODE_SAMPLE'
29*26dfc232SAndreas Gohrecho 'Hello World';
30*26dfc232SAndreas GohrCODE_SAMPLE
31*26dfc232SAndreas Gohr            ),
32*26dfc232SAndreas Gohr        ]);
33*26dfc232SAndreas Gohr    }
34*26dfc232SAndreas Gohr
35*26dfc232SAndreas Gohr    /** @inheritdoc */
36*26dfc232SAndreas Gohr    public function getNodeTypes(): array
37*26dfc232SAndreas Gohr    {
38*26dfc232SAndreas Gohr        return [Expression::class];
39*26dfc232SAndreas Gohr    }
40*26dfc232SAndreas Gohr
41*26dfc232SAndreas Gohr    /** @inheritdoc */
42*26dfc232SAndreas Gohr    public function refactor(Node $node)
43*26dfc232SAndreas Gohr    {
44*26dfc232SAndreas Gohr        if (!$node->expr instanceof Print_) {
45*26dfc232SAndreas Gohr            return null;
46*26dfc232SAndreas Gohr        }
47*26dfc232SAndreas Gohr        return new Echo_([$node->expr->expr], $node->getAttributes());
48*26dfc232SAndreas Gohr    }
49*26dfc232SAndreas Gohr}
50