Skip to content

Commit bb4f3d7

Browse files
committed
Analyse issue-bot samples as a whole project with their project type
Like the playground runner: the sample is analysed in a directory of its own, so that rules on CollectedDataNode, like the ones of the closed project type, do not skip the errors they skip when only files are analysed, and the projectType option of the sample is passed on. The core rule reports unused traits then, the playground one would report them again. Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
1 parent 89cdfaa commit bb4f3d7

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎issue-bot/playground.neon‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ rules:
33
- PHPStan\Rules\Playground\FunctionNeverRule
44
- PHPStan\Rules\Playground\LiteralArrayKeyCastRule
55
- PHPStan\Rules\Playground\MethodNeverRule
6-
- PHPStan\Rules\Playground\NotAnalysedTraitRule
76
- PHPStan\Rules\Playground\NoPhpCodeRule
87
- PHPStan\Rules\Playground\PhpdocCommentRule

‎issue-bot/src/Console/RunCommand.php‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ private function analyseHash(LoopInterface $loop, OutputInterface $output, int $
197197
'checkTooWideThrowTypesInProtectedAndPublicMethods' => $options['checkTooWideTypesInProtectedAndPublicMethods'] ?? false,
198198
'reportUnsafeArrayStringKeyCasting' => $options['reportUnsafeArrayStringKeyCasting'] ?? null,
199199
];
200+
// like the playground runner: only when it differs from PHPStan's default
201+
if (($options['projectType'] ?? 'open') === 'closed') {
202+
$parameters['projectType'] = 'closed';
203+
}
200204
$parameters['exceptions'] = [
201205
'implicitThrows' => $options['implicitThrows'] ?? true,
202206
'reportUncheckedExceptionDeadCatch' => $options['reportUncheckedExceptionDeadCatch'] ?? true,
@@ -215,7 +219,11 @@ private function analyseHash(LoopInterface $loop, OutputInterface $output, int $
215219

216220
$hash = $result->getHash();
217221
$neonPath = sprintf($this->tmpDir . '/%s.neon', $hash);
218-
$codePath = sprintf($this->tmpDir . '/%s.php', $hash);
222+
// the sample is analysed in a directory of its own, like a whole project, as the playground runner does:
223+
// rules on CollectedDataNode skip some errors when only files are analysed
224+
$codeDir = sprintf($this->tmpDir . '/%s', $hash);
225+
@mkdir($codeDir, 0777, true);
226+
$codePath = sprintf('%s/%s.php', $codeDir, $hash);
219227
file_put_contents($neonPath, $neon);
220228
file_put_contents($codePath, $result->getCode());
221229

@@ -227,7 +235,7 @@ private function analyseHash(LoopInterface $loop, OutputInterface $output, int $
227235
'--no-progress',
228236
'-c',
229237
escapeshellarg($neonPath),
230-
escapeshellarg($codePath),
238+
escapeshellarg($codeDir),
231239
];
232240

233241
$output->writeln(sprintf('Starting analysis of %s', $hash));
@@ -256,10 +264,11 @@ private function analyseHash(LoopInterface $loop, OutputInterface $output, int $
256264
*/
257265
public static function extractErrors(array $json, string $hash): array
258266
{
267+
$replacedPaths = [sprintf('/%s/%s.php', $hash, $hash), sprintf('/%s.php', $hash)];
259268
$errors = [];
260269
foreach ($json['files'] ?? [] as $file) {
261270
foreach ($file['messages'] as $message) {
262-
$messageText = str_replace(sprintf('/%s.php', $hash), '/tmp.php', $message['message']);
271+
$messageText = str_replace($replacedPaths, '/tmp.php', $message['message']);
263272
if (strpos($messageText, 'Internal error') !== false) {
264273
throw new Exception(sprintf('While analysing %s: %s', $hash, $messageText));
265274
}
@@ -273,7 +282,7 @@ public static function extractErrors(array $json, string $hash): array
273282
// like it produced zero errors and any expected output silently disappears
274283
// from the diff. Record them so they are visible.
275284
foreach ($json['errors'] ?? [] as $genericError) {
276-
$messageText = str_replace(sprintf('/%s.php', $hash), '/tmp.php', (string) $genericError);
285+
$messageText = str_replace($replacedPaths, '/tmp.php', (string) $genericError);
277286
$errors[] = new PlaygroundError(-1, $messageText, null);
278287
}
279288

‎issue-bot/tests/Console/RunCommandTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ public function testTopLevelErrorHashPathIsReplaced(): void
9898
self::assertStringNotContainsString('/abc.php', $errors[0]->getMessage());
9999
}
100100

101+
public function testHashPathInDirectoryOfItsOwnIsReplaced(): void
102+
{
103+
$json = [
104+
'files' => [
105+
'/some/dir/abc/abc.php' => [
106+
'errors' => 1,
107+
'messages' => [
108+
['message' => 'Class Foo declared in /some/dir/abc/abc.php is unused.', 'line' => 3],
109+
],
110+
],
111+
],
112+
'errors' => [
113+
'Child process error while analysing /some/dir/abc/abc.php in worker',
114+
],
115+
];
116+
117+
$errors = RunCommand::extractErrors($json, 'abc');
118+
119+
self::assertCount(2, $errors);
120+
self::assertSame('Class Foo declared in /some/dir/tmp.php is unused.', $errors[0]->getMessage());
121+
self::assertSame('Child process error while analysing /some/dir/tmp.php in worker', $errors[1]->getMessage());
122+
}
123+
101124
public function testResultsFileShapeIsUnchanged(): void
102125
{
103126
// The results file written by RunCommand is serialize(['phpVersion' => int,

0 commit comments

Comments
 (0)