Skip to content

Commit 1968d4f

Browse files
committed
feat(config): make min-requirements configurable through file
1 parent 59cbc3b commit 1968d4f

File tree

10 files changed

+154
-6
lines changed

10 files changed

+154
-6
lines changed

src/Application/ConfigResolver.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ public static function guess(Composer $composer): string
103103
return self::DEFAULT_PRESET;
104104
}
105105

106+
/**
107+
* Merge requirements config from console input.
108+
*
109+
* @param array<string, string|array> $config
110+
* @param \Symfony\Component\Console\Input\InputInterface $input
111+
*
112+
* @return array<string, string|array>
113+
*/
114+
public static function mergeInputRequirements(array $config, InputInterface $input): array
115+
{
116+
$requirements = [
117+
'min-quality',
118+
'min-complexity',
119+
'min-architecture',
120+
'min-style',
121+
'disable-security-check',
122+
];
123+
foreach ($requirements as $requirement) {
124+
if ($input->hasParameterOption('--'.$requirement)) {
125+
$config['requirements'][$requirement] = $input->getOption($requirement);
126+
}
127+
}
128+
return $config;
129+
}
130+
106131
/**
107132
* @see https://www.php.net/manual/en/function.array-merge-recursive.php#96201
108133
*

src/Application/Console/Commands/AnalyseCommand.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use NunoMaduro\PhpInsights\Application\Console\Formatters\FormatResolver;
99
use NunoMaduro\PhpInsights\Application\Console\OutputDecorator;
1010
use NunoMaduro\PhpInsights\Application\Console\Style;
11+
use NunoMaduro\PhpInsights\Domain\Configuration;
1112
use Symfony\Component\Console\Input\InputInterface;
1213
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1314
use Symfony\Component\Console\Output\OutputInterface;
@@ -24,14 +25,21 @@ final class AnalyseCommand
2425
*/
2526
private $analyser;
2627

28+
/**
29+
* @var \NunoMaduro\PhpInsights\Domain\Configuration
30+
*/
31+
private $configuration;
32+
2733
/**
2834
* Creates a new instance of the Analyse Command.
2935
*
3036
* @param \NunoMaduro\PhpInsights\Application\Console\Analyser $analyser
37+
* @param \NunoMaduro\PhpInsights\Domain\Configuration $configuration
3138
*/
32-
public function __construct(Analyser $analyser)
39+
public function __construct(Analyser $analyser, Configuration $configuration)
3340
{
3441
$this->analyser = $analyser;
42+
$this->configuration = $configuration;
3543
}
3644

3745
/**
@@ -64,27 +72,27 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
6472
);
6573

6674
$hasError = false;
67-
if ($input->getOption('min-quality') > $results->getCodeQuality()) {
75+
if ($this->configuration->getMinQuality() > $results->getCodeQuality()) {
6876
$consoleStyle->error('The code quality score is too low');
6977
$hasError = true;
7078
}
7179

72-
if ($input->getOption('min-complexity') > $results->getComplexity()) {
80+
if ($this->configuration->getMinComplexity() > $results->getComplexity()) {
7381
$consoleStyle->error('The complexity score is too low');
7482
$hasError = true;
7583
}
7684

77-
if ($input->getOption('min-architecture') > $results->getStructure()) {
85+
if ($this->configuration->getMinArchitecture() > $results->getStructure()) {
7886
$consoleStyle->error('The architecture score is too low');
7987
$hasError = true;
8088
}
8189

82-
if ($input->getOption('min-style') > $results->getStyle()) {
90+
if ($this->configuration->getMinStyle() > $results->getStyle()) {
8391
$consoleStyle->error('The style score is too low');
8492
$hasError = true;
8593
}
8694

87-
if (! (bool) $input->getOption('disable-security-check') && $results->getTotalSecurityIssues() > 0) {
95+
if (! $this->configuration->getDisableSecurityCheck() && $results->getTotalSecurityIssues() > 0) {
8896
$hasError = true;
8997
}
9098

src/Application/Injectors/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function __invoke(): array
4141
$config = require $configPath;
4242
}
4343

44+
$config = ConfigResolver::mergeInputRequirements($config, $input);
4445
return ConfigResolver::resolve($config, DirectoryResolver::resolve($input));
4546
},
4647
];

src/Domain/Configuration.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ final class Configuration
6767
*/
6868
private $remove;
6969

70+
/**
71+
* List of requirements.
72+
*
73+
* @var array<string>
74+
*/
75+
private $requirements;
76+
7077
/**
7178
* List of custom configuration by insight.
7279
*
@@ -160,6 +167,46 @@ public function getPreset(): string
160167
return $this->preset;
161168
}
162169

170+
/**
171+
* @return float
172+
*/
173+
public function getMinQuality(): float
174+
{
175+
return (float) ($this->requirements['min-quality'] ?? 0);
176+
}
177+
178+
/**
179+
* @return float
180+
*/
181+
public function getMinComplexity(): float
182+
{
183+
return (float) ($this->requirements['min-complexity'] ?? 0);
184+
}
185+
186+
/**
187+
* @return float
188+
*/
189+
public function getMinArchitecture(): float
190+
{
191+
return (float) ($this->requirements['min-architecture'] ?? 0);
192+
}
193+
194+
/**
195+
* @return float
196+
*/
197+
public function getMinStyle(): float
198+
{
199+
return (float) ($this->requirements['min-style'] ?? 0);
200+
}
201+
202+
/**
203+
* @return bool
204+
*/
205+
public function getDisableSecurityCheck(): bool
206+
{
207+
return (bool) $this->requirements['disable-security-check'] ?? false;
208+
}
209+
163210
/**
164211
* @return FileLinkFormatterContract
165212
*/
@@ -179,6 +226,7 @@ private function resolveConfig(array $config): void
179226
'directory' => (string) getcwd(),
180227
'exclude' => [],
181228
'add' => [],
229+
'requirements' => [],
182230
'remove' => [],
183231
'config' => [],
184232
]);
@@ -201,6 +249,7 @@ private function resolveConfig(array $config): void
201249
$this->add = $config['add'];
202250
$this->remove = $config['remove'];
203251
$this->config = $config['config'];
252+
$this->requirements = $config['requirements'];
204253

205254
if (
206255
array_key_exists('ide', $config)

stubs/config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,12 @@
7070
// ],
7171
],
7272

73+
'requirements' => [
74+
// 'min-quality' => 0,
75+
// 'min-complexity' => 0,
76+
// 'min-architecture' => 0,
77+
// 'min-style' => 0,
78+
// 'disable-security-check' => false,
79+
],
80+
7381
];

stubs/drupal.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,12 @@
7070
// ],
7171
],
7272

73+
'requirements' => [
74+
// 'min-quality' => 0,
75+
// 'min-complexity' => 0,
76+
// 'min-architecture' => 0,
77+
// 'min-style' => 0,
78+
// 'disable-security-check' => false,
79+
],
80+
7381
];

stubs/laravel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,12 @@
9191
],
9292
],
9393

94+
'requirements' => [
95+
// 'min-quality' => 0,
96+
// 'min-complexity' => 0,
97+
// 'min-architecture' => 0,
98+
// 'min-style' => 0,
99+
// 'disable-security-check' => false,
100+
],
101+
94102
];

stubs/magento2.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@
6868
// ],
6969
],
7070

71+
'requirements' => [
72+
// 'min-quality' => 0,
73+
// 'min-complexity' => 0,
74+
// 'min-architecture' => 0,
75+
// 'min-style' => 0,
76+
// 'disable-security-check' => false,
77+
],
78+
7179
];

stubs/symfony.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@
6868
// ],
6969
],
7070

71+
'requirements' => [
72+
// 'min-quality' => 0,
73+
// 'min-complexity' => 0,
74+
// 'min-architecture' => 0,
75+
// 'min-style' => 0,
76+
// 'disable-security-check' => false,
77+
],
78+
7179
];

tests/Application/ConfigResolverTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use NunoMaduro\PhpInsights\Domain\LinkFormatter\NullFileLinkFormatter;
1313
use PHPUnit\Framework\TestCase;
1414
use SlevomatCodingStandard\Sniffs\Commenting\DocCommentSpacingSniff;
15+
use Symfony\Component\Console\Input\ArrayInput;
16+
use Symfony\Component\Console\Input\InputDefinition;
17+
use Symfony\Component\Console\Input\InputOption;
1518
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
1619

1720
final class ConfigResolverTest extends TestCase
@@ -176,6 +179,28 @@ public function testResolveWithIdePattern(): void
176179
self::assertNotInstanceOf(NullFileLinkFormatter::class, $config->getFileLinkFormatter());
177180
}
178181

182+
public function testMergeInputRequirements(): void
183+
{
184+
$input = new ArrayInput([
185+
'--not-whitelisted' => 1,
186+
'--min-complexity' => 1,
187+
],
188+
new InputDefinition([
189+
new InputOption('min-complexity'),
190+
new InputOption('disable-security-check'),
191+
new InputOption('not-whitelisted'),
192+
])
193+
);
194+
195+
$config = ConfigResolver::mergeInputRequirements([], $input);
196+
197+
self::assertEquals([
198+
'requirements' => [
199+
'min-complexity' => 1,
200+
]
201+
], $config);
202+
}
203+
179204
/**
180205
* @return array<string, array<string>>
181206
*/

0 commit comments

Comments
 (0)