Skip to content

Commit 110271a

Browse files
authored
Merge pull request #66 from tighten/adc/fix-lint-staged
Fix issue with PHP CS Fixer when providing multiple files
2 parents 0830b7e + f3aec93 commit 110271a

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

app/Support/PhpCsFixer.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private function process(): int
4949
$resolver = new ConfigurationResolver(
5050
$this->getConfig(),
5151
[
52+
'config' => $this->getConfigFilePath(),
5253
'allow-risky' => 'yes',
5354
'diff' => $output->isVerbose(),
5455
'dry-run' => $this->dusterConfig->get('lint'),
@@ -63,7 +64,7 @@ private function process(): int
6364
);
6465

6566
$changes = (new Runner(
66-
$resolver->getFinder(),
67+
$this->getConfig()->getFinder(),
6768
$resolver->getFixers(),
6869
$resolver->getDiffer(),
6970
app()->get(EventDispatcher::class),
@@ -84,13 +85,29 @@ private function process(): int
8485

8586
private function getConfig(): ConfigInterface
8687
{
87-
$config = $this->getConfigFile();
88+
$config = $this->includeConfig();
8889

8990
if (! $config instanceof ConfigInterface) {
9091
throw new InvalidConfigurationException("The PHP CS Fixer config file does not return a 'PhpCsFixer\ConfigInterface' instance.");
9192
}
9293

93-
$finder = $config->getFinder();
94+
return $config->setFinder($this->updateFinder($config->getFinder()));
95+
}
96+
97+
/**
98+
* Update the finder with the paths and exclude from the config.
99+
* We are bypassing resolveFinder() in ConfigurationResolver
100+
* to allow for us to use the global duster config.
101+
*/
102+
private function updateFinder(Finder $finder): Finder
103+
{
104+
collect($this->dusterConfig->get('paths', []))->each(function ($path) use ($finder) {
105+
if (is_dir($path)) {
106+
$finder = $finder->in($path);
107+
} elseif (is_file($path)) {
108+
$finder = $finder->append([$path]);
109+
}
110+
});
94111

95112
collect($this->dusterConfig->get('exclude', []))->each(function ($path) use ($finder) {
96113
if (is_dir($path)) {
@@ -100,12 +117,17 @@ private function getConfig(): ConfigInterface
100117
}
101118
});
102119

103-
return $config->setFinder($finder);
120+
return $finder;
121+
}
122+
123+
private function includeConfig(): Config
124+
{
125+
return include $this->getConfigFilePath();
104126
}
105127

106-
private function getConfigFile(): Config
128+
private function getConfigFilePath(): string
107129
{
108-
return include (string) collect([
130+
return (string) collect([
109131
Project::path() . '/.php-cs-fixer.dist.php',
110132
Project::path() . '/.php-cs-fixer.php',
111133
base_path('standards/.php-cs-fixer.dist.php'),

builds/duster

3.6 KB
Binary file not shown.

config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
|
2727
*/
2828

29-
'version' => '0.5.5',
29+
'version' => '0.5.6',
3030

3131
/*
3232
|--------------------------------------------------------------------------

tests/Feature/DusterCommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,19 @@
128128
->not->toContain('Linting using PHP_CodeSniffer')
129129
->not->toContain('Linting using PHP CS Fixer');
130130
});
131+
132+
it('lints multiple provided files', function () {
133+
[$statusCode, $output] = run('duster', [
134+
'path' => [
135+
base_path('tests/Fixtures/MultipleFixableIssues/file.blade.php'),
136+
base_path('tests/Fixtures/MultipleFixableIssues/file.php'),
137+
],
138+
]);
139+
140+
expect($statusCode)->toBe(1)
141+
->and($output)
142+
->toContain('Linting using TLint')
143+
->toContain('Put a space between blade control structure names and the opening paren:`@if(` -> `@if (`')
144+
->toContain('Linting using Pint')
145+
->toContain('concat_space');
146+
});

tests/Pest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use App\Commands\DusterCommand;
44
use App\Kernel;
5+
use Illuminate\Support\Arr;
56
use Symfony\Component\Console\Input\ArrayInput;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\BufferedOutput;
@@ -65,7 +66,7 @@ function run($command, $arguments)
6566
'--lint' => true,
6667
], $arguments);
6768

68-
$arguments['path'] = [$arguments['path']];
69+
$arguments['path'] = Arr::wrap($arguments['path']);
6970

7071
$input = new ArrayInput($arguments, resolve(DusterCommand::class)->getDefinition());
7172
$output = new BufferedOutput(

0 commit comments

Comments
 (0)