/
AnalyseApplication.php
172 lines (151 loc) · 4.12 KB
/
AnalyseApplication.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use PhpParser\Node;
use PHPStan\Analyser\Analyser;
use PHPStan\Analyser\Scope;
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
use PHPStan\Type\MixedType;
class AnalyseApplication
{
/** @var \PHPStan\Analyser\Analyser */
private $analyser;
/** @var string */
private $memoryLimitFile;
public function __construct(
Analyser $analyser,
string $memoryLimitFile
)
{
$this->analyser = $analyser;
$this->memoryLimitFile = $memoryLimitFile;
}
/**
* @param string[] $files
* @param bool $onlyFiles
* @param \PHPStan\Command\Output $stdOutput
* @param \PHPStan\Command\Output $errorOutput
* @param \PHPStan\Command\ErrorFormatter\ErrorFormatter $errorFormatter
* @param bool $defaultLevelUsed
* @param bool $debug
* @param string|null $projectConfigFile
* @return int Error code.
*/
public function analyse(
array $files,
bool $onlyFiles,
Output $stdOutput,
Output $errorOutput,
ErrorFormatter $errorFormatter,
bool $defaultLevelUsed,
bool $debug,
?string $projectConfigFile
): int
{
$this->updateMemoryLimitFile();
$errors = [];
register_shutdown_function(function (): void {
$error = error_get_last();
if ($error === null) {
return;
}
if ($error['type'] !== E_ERROR) {
return;
}
if (strpos($error['message'], 'Allowed memory size') !== false) {
return;
}
@unlink($this->memoryLimitFile);
});
if (!$debug) {
$progressStarted = false;
$fileOrder = 0;
$preFileCallback = null;
$postFileCallback = function () use ($errorOutput, &$progressStarted, $files, &$fileOrder): void {
if (!$progressStarted) {
$errorOutput->getStyle()->progressStart(count($files));
$progressStarted = true;
}
$errorOutput->getStyle()->progressAdvance();
if ($fileOrder % 100 === 0) {
$this->updateMemoryLimitFile();
}
$fileOrder++;
};
} else {
$preFileCallback = static function (string $file) use ($stdOutput): void {
$stdOutput->writeLineFormatted($file);
};
$postFileCallback = null;
}
$hasInferrablePropertyTypesFromConstructor = false;
$errors = array_merge($errors, $this->analyser->analyse(
$files,
$onlyFiles,
$preFileCallback,
$postFileCallback,
$debug,
static function (Node $node, Scope $scope) use (&$hasInferrablePropertyTypesFromConstructor): void {
if ($hasInferrablePropertyTypesFromConstructor) {
return;
}
if (!$node instanceof Node\Stmt\PropertyProperty) {
return;
}
if (!$scope->isInClass()) {
return;
}
$classReflection = $scope->getClassReflection();
if (!$classReflection->hasConstructor() || $classReflection->getConstructor()->getDeclaringClass()->getName() !== $classReflection->getName()) {
return;
}
$propertyName = $node->name->toString();
if (!$classReflection->hasNativeProperty($propertyName)) {
return;
}
$propertyReflection = $classReflection->getNativeProperty($propertyName);
if (!$propertyReflection->isPrivate()) {
return;
}
$propertyType = $propertyReflection->getReadableType();
if (!$propertyType instanceof MixedType || $propertyType->isExplicitMixed()) {
return;
}
$hasInferrablePropertyTypesFromConstructor = true;
}
));
if (isset($progressStarted) && $progressStarted) {
$errorOutput->getStyle()->progressFinish();
}
$fileSpecificErrors = [];
$notFileSpecificErrors = [];
$warnings = [];
foreach ($errors as $error) {
if (is_string($error)) {
$notFileSpecificErrors[] = $error;
} else {
if ($error->isWarning()) {
$warnings[] = $error->getMessage();
continue;
}
$fileSpecificErrors[] = $error;
}
}
return $errorFormatter->formatErrors(
new AnalysisResult(
$fileSpecificErrors,
$notFileSpecificErrors,
$warnings,
$defaultLevelUsed,
$hasInferrablePropertyTypesFromConstructor,
$projectConfigFile
),
$stdOutput
);
}
private function updateMemoryLimitFile(): void
{
$bytes = memory_get_peak_usage(true);
$megabytes = ceil($bytes / 1024 / 1024);
file_put_contents($this->memoryLimitFile, sprintf('%d MB', $megabytes));
}
}