These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PhpGitHooks\Module\PhpCs\Infrastructure\Tool; |
||
4 | |||
5 | use PhpGitHooks\Infrastructure\Tool\ToolPathFinder; |
||
6 | use PhpGitHooks\Module\PhpCs\Model\PhpCsToolProcessorInterface; |
||
7 | use Symfony\Component\Process\Process; |
||
8 | |||
9 | class PhpCsToolProcessor implements PhpCsToolProcessorInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var ToolPathFinder |
||
13 | */ |
||
14 | private $toolPathFinder; |
||
15 | |||
16 | /** |
||
17 | * PhpCsToolProcessor constructor. |
||
18 | * |
||
19 | * @param ToolPathFinder $toolPathFinder |
||
20 | */ |
||
21 | public function __construct(ToolPathFinder $toolPathFinder) |
||
22 | { |
||
23 | $this->toolPathFinder = $toolPathFinder; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @param string $file |
||
28 | * @param string $standard |
||
29 | * @param string $ignore |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function process($file, $standard, $ignore) |
||
34 | { |
||
35 | $process = $this->execute($file, $standard, $ignore); |
||
36 | |||
37 | return $this->setErrors($process); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $file |
||
42 | * @param string $standard |
||
43 | * @param string $ignore |
||
44 | * |
||
45 | * @return Process |
||
46 | */ |
||
47 | private function execute($file, $standard, $ignore) |
||
48 | { |
||
49 | $process = new Process( |
||
50 | [ |
||
51 | 'php', |
||
52 | $this->toolPathFinder->find('phpcs'), |
||
53 | '--standard='.$standard, |
||
54 | '--ignore='.$ignore, |
||
55 | $file, |
||
56 | ] |
||
57 | ); |
||
58 | |||
59 | $process->run(); |
||
60 | |||
61 | return $process; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param Process $process |
||
66 | * |
||
67 | * @return null|string |
||
68 | * |
||
69 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
70 | */ |
||
71 | View Code Duplication | private function setErrors(Process $process) |
|
0 ignored issues
–
show
|
|||
72 | { |
||
73 | if (false === $process->isSuccessful()) { |
||
74 | return $process->getErrorOutput() ?: $process->getOutput(); |
||
75 | } |
||
76 | |||
77 | return null; |
||
78 | } |
||
79 | } |
||
80 |
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.