Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpstan - use excludePaths instead of excludes_analyse #247

Merged
merged 3 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions src/Tools/Analyzer/Phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,14 @@ function ($relativeDir) {
)));
};

$defaultConfig = $this->config->path('phpstan.standard') ?: (getcwd() . '/phpstan.neon');
if (file_exists($defaultConfig)) {
$config = \Nette\Neon\Neon::decode(file_get_contents($defaultConfig));
$config['parameters'] += [
'excludes_analyse' => [],
];
} else {
$config = [
'parameters' => [
'autoload_directories' => $createAbsolutePaths($this->options->getAnalyzedDirs()),
'excludes_analyse' => [],
],
];
}
$defaultConfigFile = $this->config->path('phpstan.standard') ?: (getcwd() . '/phpstan.neon');
$existingConfig = file_exists($defaultConfigFile)
? \Nette\Neon\Neon::decode(file_get_contents($defaultConfigFile))
: [];

$config['parameters']['excludes_analyse'] = array_merge(
$config['parameters']['excludes_analyse'],
$config = self::buildConfig(
$existingConfig,
$createAbsolutePaths($this->options->getAnalyzedDirs()),
$createAbsolutePaths($this->options->ignore->phpstan())
);

Expand All @@ -69,4 +60,45 @@ private function getErrorFormatOption()
{
return $this->toolVersionIs('<', '0.10.3') ? 'errorFormat' : 'error-format';
}

public static function buildConfig($existingConfig, $autoloadDirectories, $ignoredPaths)
{
if ($existingConfig !== []) {
$config = $existingConfig + [
'parameters' => [],
];
} else {
$config = [
'parameters' => [
'autoload_directories' => $autoloadDirectories,
'excludePaths' => [
'analyseAndScan' => [],
],
],
];
}

if (isset($config['parameters']['excludePaths']['analyseAndScan'])) {
$config['parameters']['excludePaths']['analyseAndScan'] = array_merge(
$config['parameters']['excludePaths']['analyseAndScan'],
$ignoredPaths
);
} elseif (isset($config['parameters']['excludePaths'])) {
$config['parameters']['excludePaths'] = array_merge(
$config['parameters']['excludePaths'],
$ignoredPaths
);
} elseif (isset($config['parameters']['excludes_analyse'])) {
$config['parameters']['excludes_analyse'] = array_merge(
$config['parameters']['excludes_analyse'],
$ignoredPaths
);
} else {
$config['parameters']['excludePaths'] = [
'analyseAndScan' => $ignoredPaths,
];
}

return $config;
}
}
11 changes: 6 additions & 5 deletions tests/.ci/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
parameters:
reportUnmatchedIgnoredErrors: false
# exclude robo v0/v1 compatibility classes - it's not possible to analyze them with phsptan (only one robo version is installed)
excludes_analyse:
- %currentWorkingDirectory%/src/Task/ParallelExec.php
- %currentWorkingDirectory%/src/Task/NonParallelExecV0.php
- %currentWorkingDirectory%/src/Task/NonParallelExecV1.php
- %currentWorkingDirectory%/src/Task/RoboAdapter.php
excludePaths:
analyseAndScan:
- %currentWorkingDirectory%/src/Task/ParallelExec.php
- %currentWorkingDirectory%/src/Task/NonParallelExecV0.php
- %currentWorkingDirectory%/src/Task/NonParallelExecV1.php
- %currentWorkingDirectory%/src/Task/RoboAdapter.php
ignoreErrors:
# constants from phpqa
- message: '#Constant COMPOSER_(.+) not found.#'
Expand Down
106 changes: 106 additions & 0 deletions tests/Tools/Analyzer/PhpstanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Edge\QA\Tools\Analyzer;

use PHPUnit\Framework\TestCase;

class PhpstanTest extends TestCase
{

private $autoloadDirectories = ['src/'];
private $ignoredPaths = ['Test.php'];

/** @dataProvider provideConfig */
public function testBuildConfig($existingConfig, $expectedConfig)
{
$config = Phpstan::buildConfig($existingConfig, $this->autoloadDirectories, $this->ignoredPaths);
$this->assertEquals($expectedConfig, $config);
}

public function provideConfig()
{
return [
'No config' => [
'existingConfig' => [],
'expectedConfig' => [
'parameters' => [
'autoload_directories' => $this->autoloadDirectories,
'excludePaths' => [
'analyseAndScan' => $this->ignoredPaths,
],
],
],
],
'No parameters in config' => [
'existingConfig' => [
'includes' => $this->autoloadDirectories,
],
'expectedConfig' => [
'includes' => $this->autoloadDirectories,
'parameters' => [
'excludePaths' => [
'analyseAndScan' => $this->ignoredPaths,
],
],
],
],
'excludePaths shortcut' => [
'existingConfig' => [
'parameters' => [
'excludePaths' => [
'File.php',
],
],
],
'expectedConfig' => [
'parameters' => [
'excludePaths' => [
'File.php',
'Test.php',
],
],
],
],
'excludePaths + analyseAndScan' => [
'existingConfig' => [
'parameters' => [
'excludePaths' => [
'analyseAndScan' => [
'File.php',
],
],
],
],
'expectedConfig' => [
'parameters' => [
'excludePaths' => [
'analyseAndScan' => [
'File.php',
'Test.php',
],
],
],
],
],
'Deprecated excludes_analyse' => [
'existingConfig' => [
'parameters' => [
'reportUnmatchedIgnoredErrors' => false,
'excludes_analyse' => [
'File.php',
],
],
],
'expectedConfig' => [
'parameters' => [
'reportUnmatchedIgnoredErrors' => false,
'excludes_analyse' => [
'File.php',
'Test.php',
],
],
],
],
];
}
}