Skip to content

Commit 700f5b7

Browse files
committed
Refactor php-cs-fixer to check levels.
1 parent 36fe58a commit 700f5b7

File tree

8 files changed

+95
-99
lines changed

8 files changed

+95
-99
lines changed

src/PhpGitHooks/Application/Composer/PreCommitProcessor.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,18 @@ private function configPhpCsFixer($execute)
8383
}
8484

8585

86-
if (!isset($this->configData['pre-commit']['execute'][$tool]['level'])) {
87-
$answerLevel = strtolower($this->setQuestion(
88-
sprintf('Set a default Level for %s tool', strtoupper($tool)),
89-
'[PSR0|psr1|psr2|symfony]',
90-
'psr0'
91-
));
86+
if (!isset($this->configData['pre-commit']['execute'][$tool]['levels'])) {
87+
foreach (['psr0', 'psr1', 'psr2', 'symfony'] as $level) {
88+
$answerLevel = strtolower($this->setQuestion(
89+
sprintf('Enable %s level for %s tool', $level, strtoupper($tool)),
90+
'[Y/n]',
91+
'Y'
92+
));
9293

93-
$this->checkLevel($answerLevel);
94+
$answerLevel = 'Y' === strtoupper($answerLevel) ? true : false;
9495

95-
$this->configData['pre-commit']['execute'][$tool]['level'] = $answerLevel;
96+
$this->configData['pre-commit']['execute'][$tool]['levels'][$level] = $answerLevel;
97+
}
9698
}
9799
}
98100

@@ -106,18 +108,6 @@ private function setQuestionTool($tool)
106108
return $this->setQuestion(sprintf('Do you want enable %s tool?', strtoupper($tool)), '[Y/n]', 'Y');
107109
}
108110

109-
/**
110-
* @param string $answerLevel
111-
*/
112-
private function checkLevel($answerLevel)
113-
{
114-
$levels = ['psr0', 'psr1', 'psr2', 'symfony'];
115-
116-
if (false === in_array($answerLevel, $levels)) {
117-
throw new \InvalidArgumentException('Invalid php-cs-fixer level.');
118-
}
119-
}
120-
121111
/**
122112
* @param array $configData
123113
*

src/PhpGitHooks/Application/PhpCsFixer/FixCodeStyleCsFixerPreCommitExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function run(OutputInterface $output, array $files, $needle)
4545
if (true === $data['enabled']) {
4646
$this->phpCsFixerHandler->setOutput($output);
4747
$this->phpCsFixerHandler->setFiles($files);
48-
$this->phpCsFixerHandler->setLevel($data['level']);
48+
$this->phpCsFixerHandler->setLevels($data['levels']);
4949
$this->phpCsFixerHandler->setFilesToAnalyze($needle);
5050
$this->phpCsFixerHandler->run();
5151
}

src/PhpGitHooks/Infrastructure/PhpCsFixer/InMemoryPhpCsFixerHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public function setFilesToAnalyze($filesToAnalyze)
3636
}
3737

3838
/**
39-
* @param string $level
39+
* @param array $levels
4040
*/
41-
public function setLevel($level)
41+
public function setLevels(array $levels)
4242
{
43+
// TODO: Implement setLevels() method.
4344
}
4445
}

src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,65 @@ class PhpCsFixerHandler extends ToolHandler implements InteractiveToolInterface,
1414
private $files;
1515
/** @var string */
1616
private $filesToAnalyze;
17-
/** @var string */
18-
private $level;
17+
/** @var array */
18+
private $levels = [];
1919

2020
/**
2121
* @throws PhpCsFixerException
2222
*/
2323
public function run()
2424
{
25-
$this->outputHandler->setTitle('Checking '.strtoupper($this->level).' code style with PHP-CS-FIXER');
26-
$this->output->write($this->outputHandler->getTitle());
27-
28-
$errors = array();
29-
30-
foreach ($this->files as $file) {
31-
$srcFile = preg_match($this->filesToAnalyze, $file);
32-
33-
if (!$srcFile) {
34-
continue;
35-
}
36-
37-
$processBuilder = new ProcessBuilder(
38-
array(
39-
'php',
40-
'bin/php-cs-fixer',
41-
'--dry-run',
42-
'fix',
43-
$file,
44-
'--fixers='.$this->level,
45-
)
46-
);
47-
48-
$phpCsFixer = $processBuilder->getProcess();
49-
$phpCsFixer->run();
50-
51-
if (false === $phpCsFixer->isSuccessful()) {
52-
$errors[] = $phpCsFixer->getOutput();
25+
foreach ($this->levels as $level => $value) {
26+
if (true === $value) {
27+
$this->outputHandler->setTitle('Checking ' . strtoupper($level) . ' code style with PHP-CS-FIXER');
28+
$this->output->write($this->outputHandler->getTitle());
29+
30+
$errors = array();
31+
32+
foreach ($this->files as $file) {
33+
$srcFile = preg_match($this->filesToAnalyze, $file);
34+
35+
if (!$srcFile) {
36+
continue;
37+
}
38+
39+
$processBuilder = new ProcessBuilder(
40+
array(
41+
'php',
42+
'bin/php-cs-fixer',
43+
'--dry-run',
44+
'fix',
45+
$file,
46+
'--fixers=' . $level,
47+
)
48+
);
49+
50+
$phpCsFixer = $processBuilder->getProcess();
51+
$phpCsFixer->run();
52+
53+
if (false === $phpCsFixer->isSuccessful()) {
54+
$errors[] = $phpCsFixer->getOutput();
55+
}
56+
}
57+
58+
if ($errors) {
59+
$this->output->writeln(BadJobLogo::paint());
60+
throw new PhpCsFixerException(implode('', $errors));
61+
}
62+
63+
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
5364
}
5465
}
55-
56-
if ($errors) {
57-
$this->output->writeln(BadJobLogo::paint());
58-
throw new PhpCsFixerException(implode('', $errors));
59-
}
60-
61-
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
62-
}
63-
64-
/**
65-
throw new PhpCsFixerException(implode('', $errors));
66-
}
67-
68-
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
6966
}
7067

7168
/**
69+
* throw new PhpCsFixerException(implode('', $errors));
70+
* }
71+
*
72+
* $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
73+
* }
74+
*
75+
* /**
7276
* @param array $files
7377
*/
7478
public function setFiles(array $files)
@@ -85,10 +89,10 @@ public function setFilesToAnalyze($filesToAnalyze)
8589
}
8690

8791
/**
88-
* @param string $level
92+
* @param array $levels
8993
*/
90-
public function setLevel($level)
94+
public function setLevels(array $levels)
9195
{
92-
$this->level = $level;
96+
$this->levels = $levels;
9397
}
9498
}

src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandlerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface PhpCsFixerHandlerInterface
66
{
77
/**
8-
* @param string $level
8+
* @param array $levels
99
*/
10-
public function setLevel($level);
10+
public function setLevels(array $levels);
1111
}

src/PhpGitHooks/Tests/Application/Composer/ConfiguratorScriptTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function buildConfigStopInProdMode()
3737
*/
3838
public function buildConfigReturnsProcess()
3939
{
40-
$this->setExpectedException(\Exception::class);
4140
/** @var Mock $iO */
4241
$iO = Mockery::mock(IOInterface::class);
4342
$iO->shouldReceive('ask');

src/PhpGitHooks/Tests/Application/Composer/PreCommitProcessorTest.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function setUp()
2727
public function preCommitHookEnabled()
2828
{
2929
$this->IO->shouldReceive('ask')
30-
->times(8)
30+
->times(11)
3131
->andReturn('y', 'y', 'y', 'y', 'y', 'y', 'y', 'psr0');
3232
$configData = $this->preCommitProcessor->execute([]);
3333

@@ -53,8 +53,8 @@ public function preCommitHookDisabled()
5353
public function preCommitConfigNewSimpleToolWithoutConfigData()
5454
{
5555
$this->IO->shouldReceive('ask')
56-
->times(8)
57-
->andReturn('y', 'n', 'y', 'y', 'y', 'y', 'y', 'PSR1');
56+
->times(11)
57+
->andReturn('y', 'n', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y');
5858

5959
$configData = $this->preCommitProcessor->execute([]);
6060

@@ -73,8 +73,8 @@ public function preCommitConfigNewSimpleToolWithoutConfigData()
7373
public function preCommitConfigSimpleToolWithConfigData()
7474
{
7575
$this->IO->shouldReceive('ask')
76-
->times(4)
77-
->andReturn('y', 'y', 'y', 'symfony');
76+
->times(7)
77+
->andReturn('y');
7878

7979
$configData = $this->preCommitProcessor->execute(
8080
[
@@ -103,8 +103,8 @@ public function preCommitConfigSimpleToolWithConfigData()
103103
public function preCommitAddPhpCsFixer()
104104
{
105105
$this->IO->shouldReceive('ask')
106-
->times(2)
107-
->andReturn('y', 'psr2');
106+
->times(5)
107+
->andReturn('y');
108108

109109
$configData = $this->preCommitProcessor->execute(
110110
[
@@ -124,48 +124,52 @@ public function preCommitAddPhpCsFixer()
124124
$execute = $configData['pre-commit']['execute'];
125125

126126
$this->assertArrayHasKey('php-cs-fixer', $execute);
127-
$this->assertTrue($execute['php-cs-fixer']['enabled']);
128-
$this->assertEquals('psr2', $execute['php-cs-fixer']['level']);
127+
128+
$fixer = $execute['php-cs-fixer'];
129+
$this->assertTrue($fixer['enabled']);
130+
$this->assertArrayHasKey('levels', $fixer);
131+
132+
$levels = $fixer['levels'];
133+
$this->assertTrue($levels['psr0']);
134+
$this->assertTrue($levels['psr1']);
135+
$this->assertTrue($levels['psr2']);
136+
$this->assertTrue($levels['symfony']);
129137
}
130138

131139
/**
132140
* @test
133141
*/
134-
public function preCommitAddPhpCsFixerWithInvalidLevel()
142+
public function phpCsFixerConfigDataHasInvalidEntry()
135143
{
136-
$this->setExpectedException(\InvalidArgumentException::class);
137-
138-
$this->IO->shouldReceive('ask')
139-
->times(2)
140-
->andReturn('y', 'invalid_level');
144+
$this->setExpectedException(InvalidPhpCsFixerConfigDataException::class);
145+
$this->IO->shouldReceive('ask');
141146

142-
$configData = $this->preCommitProcessor->execute(
147+
$this->preCommitProcessor->execute(
143148
[
144149
'pre-commit' => [
145150
'execute' => [
146151
'phpunit' => true,
147152
'phpcs' => true,
148153
'phplint' => true,
149154
'phpmd' => true,
150-
'jsonlint' => true
155+
'jsonlint' => true,
156+
'php-cs-fixer' => true
151157
],
152158
'enabled' => true,
153159
],
154160
]
155161
);
156-
157-
$configData['pre-commit']['execute'];
158162
}
159163

160164
/**
161165
* @test
162166
*/
163-
public function phpCsFixerConfigDataHasInvalidEntry()
167+
public function phpCsFixerConfigDataHasInvalidEntryEnabled()
164168
{
165169
$this->setExpectedException(InvalidPhpCsFixerConfigDataException::class);
166170
$this->IO->shouldReceive('ask');
167171

168-
$configData = $this->preCommitProcessor->execute(
172+
$this->preCommitProcessor->execute(
169173
[
170174
'pre-commit' => [
171175
'execute' => [
@@ -174,13 +178,11 @@ public function phpCsFixerConfigDataHasInvalidEntry()
174178
'phplint' => true,
175179
'phpmd' => true,
176180
'jsonlint' => true,
177-
'php-cs-fixer' => true
181+
'php-cs-fixer' => []
178182
],
179183
'enabled' => true,
180184
],
181185
]
182186
);
183-
184-
$configData['pre-commit']['execute'];
185187
}
186188
}

src/PhpGitHooks/Tests/Application/PhpCsFixer/FixCodeStyleCsFixerPreCommitExecutorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp()
3737
*/
3838
public function idDisabled()
3939
{
40-
$this->preCommitConfig->setExtraOptions(['enabled' => false, 'level' => 'psr0']);
40+
$this->preCommitConfig->setExtraOptions(['enabled' => false, 'levels' => []]);
4141
$this->fixCodeStyleCsFixerPreCommitExecutor->run(
4242
$this->outputInterface,
4343
array(),
@@ -50,7 +50,7 @@ public function idDisabled()
5050
*/
5151
public function isEnabled()
5252
{
53-
$this->preCommitConfig->setExtraOptions(['enabled' => true, 'level' => 'psr0']);
53+
$this->preCommitConfig->setExtraOptions(['enabled' => true, 'levels' => ['psr0' => true]]);
5454

5555
$this->fixCodeStyleCsFixerPreCommitExecutor->run(
5656
$this->outputInterface,

0 commit comments

Comments
 (0)