forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeonAdapter.php
More file actions
157 lines (139 loc) · 4.18 KB
/
Copy pathNeonAdapter.php
File metadata and controls
157 lines (139 loc) · 4.18 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
<?php declare(strict_types = 1);
namespace PHPStan\DependencyInjection;
use Nette\DI\Config\Adapter;
use Nette\DI\Config\Helpers;
use Nette\DI\Definitions\Statement;
use Nette\DI\InvalidConfigurationException;
use Nette\Neon\Entity;
use Nette\Neon\Exception;
use Nette\Neon\Neon;
use Override;
use PHPStan\DependencyInjection\Neon\OptionalPath;
use PHPStan\File\FileHelper;
use PHPStan\File\FileReader;
use function array_values;
use function count;
use function dirname;
use function implode;
use function in_array;
use function is_array;
use function is_int;
use function is_string;
use function ltrim;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function substr;
final class NeonAdapter implements Adapter
{
public const CACHE_KEY = 'v32-deferred-autowired-parameters';
private const PREVENT_MERGING_SUFFIX = '!';
/** @var FileHelper[] */
private array $fileHelpers = [];
/**
* @param list<string> $expandRelativePaths
*/
public function __construct(private array $expandRelativePaths)
{
}
/**
* @return mixed[]
*/
#[Override]
public function load(string $file): array
{
$contents = FileReader::read($file);
try {
return $this->process((array) Neon::decode($contents), '', $file);
} catch (Exception $e) {
throw new Exception(sprintf('Error while loading %s: %s', $file, $e->getMessage()));
}
}
/**
* @param mixed[] $arr
* @return mixed[]
*/
public function process(array $arr, string $fileKey, string $file): array
{
$res = [];
foreach ($arr as $key => $val) {
if (is_string($key) && substr($key, -1) === self::PREVENT_MERGING_SUFFIX) {
if (!is_array($val) && $val !== null) {
throw new InvalidConfigurationException(sprintf('Replacing operator is available only for arrays, item \'%s\' is not array.', $key));
}
$key = substr($key, 0, -1) ?: '';
$val[Helpers::PREVENT_MERGING] = true;
}
$keyToResolve = $fileKey;
if (is_int($key)) {
$keyToResolve .= '[]';
} else {
$keyToResolve .= '[' . $key . ']';
}
if (is_array($val)) {
if (!is_int($key)) {
$fileKeyToPass = $fileKey . '[' . $key . ']';
} else {
$fileKeyToPass = $fileKey . '[]';
}
$val = $this->process($val, $fileKeyToPass, $file);
} elseif ($val instanceof Entity) {
if (!is_int($key)) {
$fileKeyToPass = $fileKey . '(' . $key . ')';
} else {
$fileKeyToPass = $fileKey . '()';
}
if ($val->value === Neon::CHAIN) {
$tmp = null;
foreach ($this->process($val->attributes, $fileKeyToPass, $file) as $st) {
$tmp = new Statement(
$tmp === null ? $st->getEntity() : [$tmp, ltrim(implode('::', (array) $st->getEntity()), ':')],
$st->arguments,
);
}
$val = $tmp;
} else {
if (
in_array($keyToResolve, [
'[parameters][excludePaths][]',
'[parameters][excludePaths][analyse][]',
'[parameters][excludePaths][analyseAndScan][]',
], true)
&& count($val->attributes) === 1
&& $val->attributes[0] === '?'
&& is_string($val->value)
&& !str_contains($val->value, '%')
&& !str_starts_with($val->value, '*')
) {
$fileHelper = $this->createFileHelperByFile($file);
$val = new OptionalPath($fileHelper->normalizePath($fileHelper->absolutizePath($val->value)));
} else {
$tmp = $this->process([$val->value], $fileKeyToPass, $file);
$val = new Statement($tmp[0], $this->process($val->attributes, $fileKeyToPass, $file));
}
}
}
if (in_array($keyToResolve, $this->expandRelativePaths, true) && is_string($val) && !str_contains($val, '%') && !str_starts_with($val, '*')) {
$fileHelper = $this->createFileHelperByFile($file);
$val = $fileHelper->normalizePath($fileHelper->absolutizePath($val));
}
if (
$keyToResolve === '[parameters][excludePaths]'
&& $val !== null
&& array_values($val) === $val
) {
$val = ['analyseAndScan' => $val, 'analyse' => []];
}
$res[$key] = $val;
}
return $res;
}
private function createFileHelperByFile(string $file): FileHelper
{
$dir = dirname($file);
if (!isset($this->fileHelpers[$dir])) {
$this->fileHelpers[$dir] = new FileHelper($dir);
}
return $this->fileHelpers[$dir];
}
}