1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Infrastructure\CodeSniffer; |
4
|
|
|
|
5
|
|
|
use IgnoreFiles\IgnoreFiles; |
6
|
|
|
use PhpGitHooks\Application\Message\MessageConfigData; |
7
|
|
|
use PhpGitHooks\Command\BadJobLogo; |
8
|
|
|
use PhpGitHooks\Command\OutputHandlerInterface; |
9
|
|
|
use PhpGitHooks\Infrastructure\Common\ToolHandler; |
10
|
|
|
use Symfony\Component\Process\Process; |
11
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CodeSnifferHandler. |
15
|
|
|
*/ |
16
|
|
|
class CodeSnifferHandler extends ToolHandler |
17
|
|
|
{ |
18
|
|
|
/** @var array */ |
19
|
|
|
private $files; |
20
|
|
|
/** @var string */ |
21
|
|
|
private $neddle; |
22
|
|
|
/** @var string */ |
23
|
|
|
private $standard = 'PSR2'; |
24
|
|
|
/** |
25
|
|
|
* @var IgnoreFiles |
26
|
|
|
*/ |
27
|
|
|
private $ignoreFiles; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* CodeSnifferHandler constructor. |
31
|
|
|
* |
32
|
|
|
* @param OutputHandlerInterface $outputHandler |
33
|
|
|
* @param IgnoreFiles $ignoreFiles |
34
|
|
|
*/ |
35
|
|
|
public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($outputHandler); |
38
|
|
|
$this->ignoreFiles = $ignoreFiles; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $messages |
43
|
|
|
* |
44
|
|
|
* @throws InvalidCodingStandardException |
45
|
|
|
*/ |
46
|
|
|
public function run(array $messages) |
47
|
|
|
{ |
48
|
|
|
$this->outputHandler->setTitle('Checking code style with PHPCS'); |
49
|
|
|
$this->output->write($this->outputHandler->getTitle()); |
50
|
|
|
|
51
|
|
|
foreach ($this->files as $file) { |
52
|
|
|
if (!preg_match($this->neddle, $file)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (false === $this->ignoreFiles->isIgnored($file)) { |
57
|
|
|
$processBuilder = new ProcessBuilder(array('php', 'bin/phpcs', '--standard='.$this->standard, $file)); |
58
|
|
|
/** @var Process $phpCs */ |
59
|
|
|
$phpCs = $processBuilder->getProcess(); |
60
|
|
|
$phpCs->run(); |
61
|
|
|
|
62
|
|
View Code Duplication |
if (false === $phpCs->isSuccessful()) { |
|
|
|
|
63
|
|
|
$this->outputHandler->setError($phpCs->getOutput()); |
64
|
|
|
$this->output->writeln($this->outputHandler->getError()); |
65
|
|
|
$this->output->writeln(BadJobLogo::paint($messages[MessageConfigData::KEY_ERROR_MESSAGE])); |
66
|
|
|
|
67
|
|
|
throw new InvalidCodingStandardException(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $neddle |
77
|
|
|
*/ |
78
|
|
|
public function setNeddle($neddle) |
79
|
|
|
{ |
80
|
|
|
$this->neddle = $neddle; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $files |
85
|
|
|
*/ |
86
|
|
|
public function setFiles($files) |
87
|
|
|
{ |
88
|
|
|
$this->files = $files; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $standard |
93
|
|
|
*/ |
94
|
|
|
public function setStandard($standard) |
95
|
|
|
{ |
96
|
|
|
$this->standard = $standard; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.