-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathFileReconstructor.php
68 lines (56 loc) · 1.59 KB
/
FileReconstructor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php declare(strict_types=1);
namespace Rector\Testing\Application;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use Rector\NodeTraverser\StateHolder;
use Rector\Printer\CodeStyledPrinter;
use SplFileInfo;
final class FileReconstructor
{
/**
* @var Parser
*/
private $parser;
/**
* @var CodeStyledPrinter
*/
private $codeStyledPrinter;
/**
* @var Lexer
*/
private $lexer;
/**
* @var NodeTraverser
*/
private $nodeTraverser;
/**
* @var StateHolder
*/
private $stateHolder;
public function __construct(
Parser $parser,
CodeStyledPrinter $codeStyledPrinter,
Lexer $lexer,
NodeTraverser $nodeTraverser,
StateHolder $stateHolder
) {
$this->parser = $parser;
$this->codeStyledPrinter = $codeStyledPrinter;
$this->lexer = $lexer;
$this->nodeTraverser = $nodeTraverser;
$this->stateHolder = $stateHolder;
}
# ref: https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516
public function processFile(SplFileInfo $file): string
{
$fileContent = file_get_contents($file->getRealPath());
$oldStmts = $this->parser->parse($fileContent);
$oldTokens = $this->lexer->getTokens();
$newStmts = $this->nodeTraverser->traverse($oldStmts);
if (! $this->stateHolder->isAfterTraverseCalled()) {
[$newStmts, $oldStmts] = [$oldStmts, $newStmts];
}
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);
}
}