-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringExprResolver.php
More file actions
160 lines (131 loc) · 4.57 KB
/
StringExprResolver.php
File metadata and controls
160 lines (131 loc) · 4.57 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Symplify\PhpConfigPrinter\ExprResolver;
use Nette\Utils\Strings;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Symplify\PhpConfigPrinter\NodeFactory\CommonNodeFactory;
use Symplify\PhpConfigPrinter\NodeFactory\ConstantNodeFactory;
use Symplify\PhpConfigPrinter\ValueObject\FunctionName;
final readonly class StringExprResolver
{
/**
* @see https://regex101.com/r/laf2wR/1
* @var string
*/
private const TWIG_HTML_XML_SUFFIX_REGEX = '#\.(twig|html|xml)$#';
public function __construct(
private ConstantNodeFactory $constantNodeFactory,
private CommonNodeFactory $commonNodeFactory
) {
}
public function resolve(
string $value,
bool $skipServiceReference,
bool $skipClassesToConstantReference,
bool $isRoutingImport = false
): Expr {
if ($value === '') {
return new String_($value);
}
if (str_starts_with($value, '%const(')) {
$const = substr($value, 7, -2);
return $this->constantNodeFactory->createConstant($const);
}
$classConstFetch = $this->constantNodeFactory->createClassConstantIfValue($value);
if ($classConstFetch instanceof ClassConstFetch) {
return $classConstFetch;
}
// do not print "\n" as empty space, but use string value instead
if (in_array($value, ["\r", "\n", "\r\n"], true)) {
return $this->keepNewline($value);
}
$value = ltrim($value, '\\');
if ($this->isClassType($value)) {
return $this->resolveClassType($skipClassesToConstantReference, $value);
}
if (str_starts_with($value, '@=')) {
return $this->resolveExpr($value, $skipServiceReference, $skipClassesToConstantReference);
}
// is service reference
if (str_starts_with($value, '@') && ! $this->isFilePath($value)) {
return $this->resolveServiceReferenceExpr(
$value,
$skipServiceReference,
FunctionName::SERVICE,
$isRoutingImport
);
}
return BuilderHelpers::normalizeValue($value);
}
private function resolveExpr(
string $value,
bool $skipServiceReference,
bool $skipClassesToConstantReference
): FuncCall {
$value = ltrim($value, '@=');
$expr = $this->resolve($value, $skipServiceReference, $skipClassesToConstantReference);
$args = [new Arg($expr)];
return new FuncCall(new FullyQualified(FunctionName::EXPR), $args);
}
private function keepNewline(string $value): String_
{
$string = new String_($value);
$string->setAttribute('kind', String_::KIND_DOUBLE_QUOTED);
return $string;
}
private function isFilePath(string $value): bool
{
return (bool) Strings::match($value, self::TWIG_HTML_XML_SUFFIX_REGEX);
}
private function resolveClassType(bool $skipClassesToConstantReference, string $value): String_ | ClassConstFetch
{
if ($skipClassesToConstantReference) {
return new String_($value);
}
return $this->commonNodeFactory->createClassReference($value);
}
private function isClassType(string $value): bool
{
if (! ctype_upper($value[0])) {
return false;
}
// to avoid autoload in case of missing code sniffer dependency
if (str_ends_with($value, 'Sniff')) {
return true;
}
if (str_ends_with($value, 'Fixer')) {
return true;
}
if (class_exists($value)) {
return true;
}
return interface_exists($value);
}
/**
* @param FunctionName::* $functionName
*/
private function resolveServiceReferenceExpr(
string $value,
bool $skipServiceReference,
string $functionName,
bool $isRoutingImport = false
): Expr {
$value = ltrim($value, '@');
$expr = $this->resolve($value, $skipServiceReference, false);
if ($skipServiceReference) {
// return the `@` back
if ($isRoutingImport && $expr instanceof String_) {
$expr->value = '@' . $expr->value;
}
return $expr;
}
$args = [new Arg($expr)];
return new FuncCall(new FullyQualified($functionName), $args);
}
}