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