Skip to content

Commit 84c3743

Browse files
committed
feat: dirty files check
1 parent 83f1509 commit 84c3743

9 files changed

+61
-1
lines changed

src/Command/AnalyseCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ protected function configure(): void
109109
new InputOption('watch', mode: InputOption::VALUE_NONE, description: 'Launch PHPStan Pro'),
110110
new InputOption('pro', mode: InputOption::VALUE_NONE, description: 'Launch PHPStan Pro'),
111111
new InputOption('fail-without-result-cache', mode: InputOption::VALUE_NONE, description: 'Return non-zero exit code when result cache is not used'),
112+
new InputOption('dirty', mode: InputOption::VALUE_NONE, description: 'Only analyze files that have uncommitted changes'),
112113
]);
113114
}
114115

@@ -147,6 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
147148
$pro = (bool) $input->getOption('watch') || (bool) $input->getOption('pro');
148149
$fix = (bool) $input->getOption('fix');
149150
$failWithoutResultCache = (bool) $input->getOption('fail-without-result-cache');
151+
$dirty = (bool) $input->getOption('dirty');
150152

151153
/** @var string|false|null $generateBaselineFile */
152154
$generateBaselineFile = $input->getOption('generate-baseline');
@@ -190,6 +192,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
190192
$tmpFile,
191193
$insteadOfFile,
192194
true,
195+
$dirty,
193196
);
194197
} catch (InceptionNotSuccessfulException $e) {
195198
return 1;

src/Command/ClearResultCacheCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8888
null,
8989
null,
9090
true,
91+
false,
9192
);
9293
} catch (InceptionNotSuccessfulException) {
9394
return 1;

src/Command/CommandHelper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static function begin(
9595
?string $singleReflectionFile,
9696
?string $singleReflectionInsteadOfFile,
9797
bool $cleanupContainerCache,
98+
bool $dirty,
9899
): InceptionResult
99100
{
100101
$stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output)));
@@ -239,7 +240,13 @@ public static function begin(
239240
$defaultLevelUsed = true;
240241
}
241242

242-
$paths = array_map(static fn (string $path): string => $currentWorkingDirectoryFileHelper->normalizePath($currentWorkingDirectoryFileHelper->absolutizePath($path)), $paths);
243+
if ($dirty) {
244+
$dirtyPathsHelper = new DirtyFilesHelper();
245+
$paths = array_map(static fn (string $path): string => $currentWorkingDirectoryFileHelper
246+
->normalizePath($currentWorkingDirectoryFileHelper->absolutizePath($path)), $dirtyPathsHelper->getGitDirtyFiles());
247+
} else {
248+
$paths = array_map(static fn (string $path): string => $currentWorkingDirectoryFileHelper->normalizePath($currentWorkingDirectoryFileHelper->absolutizePath($path)), $paths);
249+
}
243250

244251
$analysedPathsFromConfig = [];
245252
$containerFactory = new ContainerFactory($currentWorkingDirectory);

src/Command/DiagnoseCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8787
null,
8888
null,
8989
false,
90+
false,
9091
);
9192
} catch (InceptionNotSuccessfulException) {
9293
return 1;

src/Command/DirtyFilesHelper.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Command;
4+
5+
final class DirtyFilesHelper
6+
{
7+
private function getGitRepoRoot(): ?string
8+
{
9+
exec('git rev-parse --show-toplevel 2>&1', $output, $returnVar);
10+
if ($returnVar !== 0) {
11+
return null;
12+
}
13+
14+
return trim($output[0]);
15+
}
16+
17+
/**
18+
* @return string[]
19+
*/
20+
public function getGitDirtyFiles(): array
21+
{
22+
$repoPath = $this->getGitRepoRoot();
23+
if ($repoPath === null) {
24+
return [];
25+
}
26+
27+
$cmd = 'cd ' . escapeshellarg($repoPath) . ' && git status --porcelain';
28+
29+
exec($cmd, $output, $returnVar);
30+
if ($returnVar !== 0) {
31+
return [];
32+
}
33+
34+
$dirtyFiles = [];
35+
36+
foreach ($output as $line) {
37+
$filePath = substr($line, 3);
38+
$dirtyFiles[] = $filePath;
39+
}
40+
41+
return $dirtyFiles;
42+
}
43+
}

src/Command/DumpParametersCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
null,
9090
null,
9191
false,
92+
false,
9293
);
9394
} catch (InceptionNotSuccessfulException) {
9495
return 1;

src/Command/FixerWorkerCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114114
null,
115115
null,
116116
false,
117+
false,
117118
);
118119
} catch (InceptionNotSuccessfulException) {
119120
return 1;

src/Command/WorkerCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
118118
$tmpFile,
119119
$insteadOfFile,
120120
false,
121+
false,
121122
);
122123
} catch (InceptionNotSuccessfulException $e) {
123124
return 1;

tests/PHPStan/Command/CommandHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function testBegin(
128128
null,
129129
null,
130130
false,
131+
false,
131132
);
132133
if ($expectException) {
133134
$this->fail();
@@ -309,6 +310,7 @@ public function testResolveParameters(
309310
null,
310311
null,
311312
false,
313+
false,
312314
);
313315
$parameters = $result->getContainer()->getParameters();
314316
foreach ($expectedParameters as $name => $expectedValue) {

0 commit comments

Comments
 (0)