-
Notifications
You must be signed in to change notification settings - Fork 3
/
CodeConverter.php
140 lines (116 loc) · 4.03 KB
/
CodeConverter.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace AdrianSuter\Autoload\Override;
use PhpParser\Lexer;
use PhpParser\Lexer\Emulative;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser;
use PhpParser\Parser\Php7;
use PhpParser\PrettyPrinter\Standard;
use function array_keys;
use function array_values;
use function md5;
use function str_replace;
use function uniqid;
/**
* @package AdrianSuter\Autoload\Override
*/
class CodeConverter
{
private const ATTR_RESOLVED_NAME = 'resolvedName';
/**
* @var Parser The PHP Parser.
*/
protected $parser;
/**
* @var Lexer The PHP Lexer.
*/
protected $lexer;
/**
* @var NodeTraverser The PHP Node Traverser.
*/
protected $traverser;
/**
* @var Standard The PHP Printer.
*/
protected $printer;
/**
* @var NodeFinder The PHP Node Finder.
*/
protected $nodeFinder;
/**
* @param Lexer|null $lexer The PHP Lexer.
* @param Parser|null $parser The PHP Parser.
* @param NodeTraverser|null $traverser The PHP Node Traverser - make sure that the traverser has a CloningVisitor
* and a NameResolver visitor.
* @param Standard|null $printer The PHP Printer.
* @param NodeFinder|null $nodeFinder The PHP Node Finder.
*/
public function __construct(
?Lexer $lexer = null,
?Parser $parser = null,
?NodeTraverser $traverser = null,
?Standard $printer = null,
?NodeFinder $nodeFinder = null
) {
$this->lexer = $lexer ?? new Emulative(
[
'usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'],
]
);
$this->parser = $parser ?? new Php7($this->lexer);
if ($traverser === null) {
$traverser = new NodeTraverser();
$traverser->addVisitor(new CloningVisitor());
$traverser->addVisitor(new NameResolver(null, ['replaceNodes' => false]));
}
$this->traverser = $traverser;
$this->printer = $printer ?? new Standard();
$this->nodeFinder = $nodeFinder ?? new NodeFinder();
}
/**
* Convert the given source code.
*
* @param string $code The source code.
* @param array $functionCallMappings The function call mappings.
*
* @return string
*/
public function convert(string $code, array $functionCallMappings): string
{
$oldStmts = $this->parser->parse($code);
$oldTokens = $this->lexer->getTokens();
$overridePlaceholders = [];
$newStmts = $this->traverser->traverse($oldStmts);
$funcCalls = $this->nodeFinder->findInstanceOf($newStmts, FuncCall::class);
foreach ($funcCalls as $funcCall) {
/** @var FuncCall $funcCall */
if (!$funcCall->name->hasAttribute(self::ATTR_RESOLVED_NAME)) {
continue;
}
/** @var FullyQualified $resolvedName */
$resolvedName = $funcCall->name->getAttribute(self::ATTR_RESOLVED_NAME);
$resolvedNameCode = $resolvedName->toCodeString();
if (isset($functionCallMappings[$resolvedNameCode])) {
$k = uniqid(md5($resolvedNameCode), true);
$overridePlaceholders[$k] = $functionCallMappings[$resolvedNameCode];
$funcCall->name = new FullyQualified($k);
}
}
$code = $this->printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
if (empty($overridePlaceholders)) {
return $code;
}
return str_replace(array_keys($overridePlaceholders), array_values($overridePlaceholders), $code);
}
}