Skip to content

Commit dc02c13

Browse files
authored
[Feature] Diff output configurable (#482)
1 parent 80d7f28 commit dc02c13

File tree

10 files changed

+69
-13
lines changed

10 files changed

+69
-13
lines changed

.github/workflows/frameworks.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
continue-on-error: ${{ env.allow_failure == 'true' }}
6363
- name: Launch PHPInsights
6464
working-directory: ./project
65-
run: php vendor/bin/phpinsights -n --ansi
65+
run: php vendor/bin/phpinsights -n --disable-security-check --ansi
6666
continue-on-error: ${{ env.allow_failure == 'true' }}
6767
- name: Launch PHPInsights Fixer
6868
working-directory: ./project
@@ -131,9 +131,9 @@ jobs:
131131
continue-on-error: ${{ env.allow_failure == 'true' }}
132132
- name: Launch PHPInsights
133133
working-directory: ./project
134-
run: php artisan insights --ansi
134+
run: php artisan insights -n --disable-security-check --ansi
135135
continue-on-error: ${{ env.allow_failure == 'true' }}
136136
- name: Launch PHPInsights Fixer
137137
working-directory: ./project
138-
run: php artisan insights --ansi --fix
138+
run: php artisan insights -n --ansi --fix --disable-security-check
139139
continue-on-error: ${{ env.allow_failure == 'true' }}

docs/get-started.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,18 @@ composer bin phpinsights require nunomaduro/phpinsights
178178

179179
Between 2 analysis, issues are cached.
180180
PHPInsights is smart enough to invalidate cache when it detect changes in your code, but you may completly flush cache before analysis by adding `--flush-cache` flag.
181+
182+
## Configure diff <Badge text="^2.0"/>
183+
184+
Some insights display a diff output.
185+
If you want more context on the diff, configure it in the `phpinsights.php` file:
186+
187+
```php
188+
<?php
189+
190+
return [
191+
// ...
192+
'diff_context' => 3,
193+
// ...
194+
];
195+
```

src/Application/Adapters/Laravel/Commands/InsightsCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use NunoMaduro\PhpInsights\Application\Console\Definitions\AnalyseDefinition;
1111
use NunoMaduro\PhpInsights\Domain\Configuration;
1212
use NunoMaduro\PhpInsights\Domain\Container;
13+
use NunoMaduro\PhpInsights\Domain\Exceptions\InvalidConfiguration;
1314
use NunoMaduro\PhpInsights\Domain\Kernel;
1415
use NunoMaduro\PhpInsights\Domain\Reflection;
1516
use RuntimeException;
@@ -47,7 +48,17 @@ public function handle(): int
4748
* @noRector Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector
4849
*/
4950
$configuration['fix'] = $this->hasOption('fix') && (bool) $this->option('fix') === true;
50-
$configuration = ConfigResolver::resolve($configuration, $this->input);
51+
try {
52+
$configuration = ConfigResolver::resolve($configuration, $this->input);
53+
} catch (InvalidConfiguration $exception) {
54+
$this->output->writeln([
55+
'',
56+
' <bg=red;options=bold> Invalid configuration </>',
57+
' <fg=red>•</> <options=bold>' . $exception->getMessage() . '</>',
58+
'',
59+
]);
60+
return 1;
61+
}
5162

5263
$container = Container::make();
5364
if (! $container instanceof \League\Container\Container) {

src/Application/Console/Formatters/Console.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,8 @@ private function parseDetailMessage(Details $detail): string
627627
$detailString = '';
628628

629629
foreach (explode(PHP_EOL, $detail->getMessage()) as $line) {
630+
$detailString .= PHP_EOL;
631+
630632
if (mb_strpos($line, '-') === 0) {
631633
$hasColor = true;
632634
$detailString .= '<fg=red>';
@@ -637,7 +639,7 @@ private function parseDetailMessage(Details $detail): string
637639
$detailString .= '<fg=green>';
638640
}
639641

640-
$detailString .= OutputFormatter::escape($line) . PHP_EOL;
642+
$detailString .= OutputFormatter::escape($line);
641643

642644
if ($hasColor) {
643645
$hasColor = false;

src/Application/Injectors/Configuration.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use NunoMaduro\PhpInsights\Application\Console\Definitions\DefinitionBinder;
1010
use NunoMaduro\PhpInsights\Domain\Configuration as DomainConfiguration;
1111
use NunoMaduro\PhpInsights\Domain\Container;
12+
use NunoMaduro\PhpInsights\Domain\Exceptions\InvalidConfiguration;
1213
use Psr\SimpleCache\CacheInterface;
1314
use Symfony\Component\Console\Input\ArgvInput;
15+
use Symfony\Component\Console\Output\ConsoleOutput;
1416

1517
/**
1618
* @internal
@@ -52,7 +54,18 @@ public function __invoke(): array
5254

5355
$config['fix'] = $fixOption || $input->getFirstArgument() === 'fix';
5456

55-
return ConfigResolver::resolve($config, $input);
57+
try {
58+
return ConfigResolver::resolve($config, $input);
59+
} catch (InvalidConfiguration $exception) {
60+
(new ConsoleOutput())->getErrorOutput()
61+
->writeln([
62+
'',
63+
' <bg=red;options=bold> Invalid configuration </>',
64+
' <fg=red>•</> <options=bold>' . $exception->getMessage() . '</>',
65+
'',
66+
]);
67+
die(1);
68+
}
5669
},
5770
];
5871
}

src/Domain/Configuration.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ final class Configuration
106106

107107
private int $threads;
108108

109+
private int $diffContext;
110+
109111
/**
110112
* Configuration constructor.
111113
*
@@ -237,6 +239,11 @@ public function getNumberOfThreads(): int
237239
return $this->threads;
238240
}
239241

242+
public function getDiffContext(): int
243+
{
244+
return $this->diffContext;
245+
}
246+
240247
/**
241248
* @param array<string, string|int|array|null> $config
242249
*/
@@ -253,6 +260,7 @@ private function resolveConfig(array $config): void
253260
'remove' => [],
254261
'config' => [],
255262
'fix' => false,
263+
'diff_context' => 1,
256264
]);
257265

258266
$resolver->setDefined('ide');
@@ -266,9 +274,15 @@ private function resolveConfig(array $config): void
266274
$resolver->setAllowedValues('config', $this->validateConfigInsights());
267275
$resolver->setAllowedValues('requirements', $this->validateRequirements());
268276
$resolver->setAllowedTypes('threads', ['null', 'int']);
277+
$resolver->setAllowedTypes('diff_context', 'int');
278+
$resolver->setAllowedValues('diff_context', static fn ($value) => $value >= 0);
269279
$resolver->setAllowedValues('threads', static fn ($value) => $value === null || $value >= 1);
270280

271-
$config = $resolver->resolve($config);
281+
try {
282+
$config = $resolver->resolve($config);
283+
} catch (\Throwable $throwable) {
284+
throw new InvalidConfiguration($throwable->getMessage(), $throwable->getCode(), $throwable);
285+
}
272286

273287
$this->preset = $config['preset'];
274288

@@ -286,6 +300,7 @@ private function resolveConfig(array $config): void
286300
$this->config = $config['config'];
287301
$this->requirements = $config['requirements'];
288302
$this->fix = $config['fix'];
303+
$this->diffContext = $config['diff_context'];
289304

290305
if (array_key_exists('ide', $config)
291306
&& is_string($config['ide'])

src/Domain/Differ.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ final class Differ implements DifferInterface
1414

1515
public function __construct()
1616
{
17+
$diffContext = Container::make()->get(Configuration::class)->getDiffContext();
18+
1719
$outputBuilder = new StrictUnifiedDiffOutputBuilder([
1820
'collapseRanges' => true,
1921
'commonLineThreshold' => 1,
20-
'contextLines' => 0,
22+
'contextLines' => $diffContext,
2123
'fromFile' => '',
2224
'toFile' => '',
2325
]);

src/Domain/Insights/FixerDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function addDetails(Details $details): void
133133

134134
public function addDiff(string $file, string $diff): void
135135
{
136-
$diff = substr($diff, 8);
136+
$diff = trim(substr($diff, 8));
137137

138138
$this->errors[] = Details::make()->setFile($file)->setDiff($diff)->setMessage($diff);
139139
}

tests/Application/ConfigResolverTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Console\Input\InputArgument;
1818
use Symfony\Component\Console\Input\InputDefinition;
1919
use Symfony\Component\Console\Input\InputOption;
20-
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
2120
use Tests\Fakes\FakeInput;
2221

2322
final class ConfigResolverTest extends TestCase
@@ -27,7 +26,6 @@ final class ConfigResolverTest extends TestCase
2726
public function setUp(): void
2827
{
2928
parent::setUp();
30-
3129
$this->baseFixturePath = dirname(__DIR__) . DIRECTORY_SEPARATOR .
3230
'Fixtures' . DIRECTORY_SEPARATOR . 'ConfigResolver' . DIRECTORY_SEPARATOR;
3331
}
@@ -124,7 +122,7 @@ public function testResolvedConfigIsCorrectlyMerged(): void
124122

125123
public function testUnknownPresetThrowException(): void
126124
{
127-
$this->expectException(InvalidOptionsException::class);
125+
$this->expectException(InvalidConfiguration::class);
128126

129127
$config = ['preset' => 'UnknownPreset'];
130128

tests/Domain/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testDefineNullForThreads(): void
119119
*/
120120
public function testExceptionOnInvalidSetThread($invalid): void
121121
{
122-
$this->expectException(\Symfony\Component\OptionsResolver\Exception\InvalidOptionsException::class);
122+
$this->expectException(InvalidConfiguration::class);
123123
$this->expectExceptionMessage('The option "threads" with value');
124124
new Configuration(['threads' => $invalid]);
125125
}

0 commit comments

Comments
 (0)