use phpstan paths

This commit is contained in:
Tomas Votruba 2018-12-28 17:01:16 +01:00
parent 0b6860ef16
commit ceb946cc8b
5 changed files with 88 additions and 2 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@ rector-symfony.yml
# often customized locally - example on Github is just fine
create-rector.yml
phpstan-dependencies.json
phpstan-paths.txt

View File

@ -31,7 +31,7 @@ script:
- |
if [[ $STATIC_ANALYSIS == true ]]; then
composer check-cs
composer phpstan
composer phpstan-ci
fi
# Rector demo run

View File

@ -100,7 +100,11 @@
"vendor/bin/ecs check bin packages src tests --fix",
"bin/clean_trailing_spaces.sh"
],
"phpstan": "vendor/bin/phpstan analyse packages src tests",
"phpstan-ci": "vendor/bin/phpstan analyse packages src tests",
"phpstan": [
"php utils/phpstan/generate-paths.php",
"vendor/bin/phpstan analyse --paths-file phpstan-paths.txt"
],
"docs": "bin/rector generate-docs > docs/AllRectorsOverview.md"
},
"scripts-descriptions": {

View File

@ -9,6 +9,7 @@ parameters:
level: 7
excludes_analyse:
- "utils/phpstan/generate-paths.php"
# test files
- '*packages/NodeTypeResolver/tests/Source/AnotherClass.php'
- '*tests/Rector/MethodCall/MethodNameReplacerRector/**/SomeClass.php'

View File

@ -0,0 +1,79 @@
<?php
// experimental, phpstan 0.10.7
// @todo refactor to classes later, if proven working
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
require __DIR__ . '/../../vendor/autoload.php';
$phpstanDependencies = __DIR__ . '/../../phpstan-dependencies.json';
$symfonyStyle = (new SymfonyStyleFactory())->create();
// prepare dependencies.json
if (! file_exists($phpstanDependencies)) {
$process = Process::fromShellCommandline('vendor/bin/phpstan dump-deps src packages > phpstan-dependencies.json');
$symfonyStyle->note('Dumping dependencies (will take ~10 s)');
$process->run();
}
$fileDependenciesJson = Json::decode(FileSystem::read($phpstanDependencies), Json::FORCE_ARRAY);
$changedFiles = resolveChangedFiles();
$filesToCheck = [];
foreach ($changedFiles as $changedFile) {
$changedFile = getcwd() . '/' . $changedFile;
if (isset($fileDependenciesJson[$changedFile])) {
$filesToCheck = array_merge($filesToCheck, $fileDependenciesJson[$changedFile], [$changedFile]);
}
}
$newFiles = resolveNewFiles();
$filesToCheck = array_merge($filesToCheck, $newFiles);
$filesToCheckString = implode(PHP_EOL, $filesToCheck);
FileSystem::write('phpstan-paths.txt', $filesToCheckString);
$symfonyStyle->success(sprintf('%d paths generated to "phpstan-paths.txt"', count($filesToCheck)));
/**
* @return string[]
*/
function resolveChangedFiles(): array
{
$process = Process::fromShellCommandline('git status -s');
$process->run();
$gitStatusOutput = $process->getOutput();
$files = [];
foreach (Strings::matchAll($gitStatusOutput, '# ([\w\/]+\.php)#m') as $match) {
$files[] = $match[1];
}
return $files;
}
/**
* @return string[]
*/
function resolveNewFiles(): array
{
$process = Process::fromShellCommandline('git status -s');
$process->run();
$gitStatusOutput = $process->getOutput();
$files = [];
foreach (Strings::matchAll($gitStatusOutput, '#\?\? ([\w\/]+)#m') as $match) {
$files[] = getcwd() . '/' . $match[1];
}
return $files;
}