Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
11 | class PreQualityToolProcessor extends Processor implements ProcessorInterface |
||
12 | { |
||
13 | protected $simpleTools = ['composer', 'jsonlint', 'phplint', 'phpmd']; |
||
14 | |||
15 | /** |
||
16 | * @param array $configData |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | 10 | public function execute(array $configData) |
|
21 | { |
||
22 | 10 | if (true === $this->enableHook($configData)) { |
|
23 | 9 | $hookData = $this->configData[$this->hookName()]; |
|
24 | 9 | $this->configMessage($hookData); |
|
25 | |||
26 | 9 | $execute = $hookData['execute']; |
|
27 | 9 | $this->configSimpleTools($execute); |
|
28 | 9 | $this->configComplexTools($execute); |
|
29 | } |
||
30 | |||
31 | 6 | return $this->configData; |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param array $configData |
||
36 | * |
||
37 | * @return bool |
||
38 | */ |
||
39 | 10 | View Code Duplication | protected function enableHook(array $configData) |
|
|||
40 | { |
||
41 | 10 | if (!isset($configData[$this->hookName()])) { |
|
42 | 3 | $enable = $this->setQuestion(sprintf('Do you want enable %s hook?', $this->hookName()), '[Y/n]', 'Y'); |
|
43 | |||
44 | 3 | $enabled = 'Y' === strtoupper($enable) ? true : false; |
|
45 | |||
46 | 3 | $this->configData[$this->hookName()] = [ |
|
47 | 3 | 'enabled' => $enabled, |
|
48 | 'execute' => [], |
||
49 | ]; |
||
50 | |||
51 | 3 | return $enabled; |
|
52 | } |
||
53 | |||
54 | 7 | $this->configData = $configData; |
|
55 | |||
56 | 7 | return $configData[$this->hookName()]['enabled']; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array $execute |
||
61 | */ |
||
62 | 9 | protected function configSimpleTools(array $execute) |
|
63 | { |
||
64 | 9 | foreach ($this->simpleTools as $tool) { |
|
65 | 9 | if (!isset($execute[$tool])) { |
|
66 | 8 | $answer = $this->setQuestionTool($tool); |
|
67 | 9 | $this->configData[$this->hookName()]['execute'][$tool] = 'Y' === strtoupper($answer) ? true : false; |
|
68 | } |
||
69 | } |
||
70 | 9 | } |
|
71 | |||
72 | /** |
||
73 | * @param array $execute |
||
74 | */ |
||
75 | 9 | protected function configComplexTools(array $execute) |
|
81 | |||
82 | /** |
||
83 | * @param array $execute |
||
84 | */ |
||
85 | 9 | protected function configPhpCs(array $execute) |
|
91 | |||
92 | /** |
||
93 | * @param array $execute |
||
94 | */ |
||
95 | 9 | protected function configPhpCsFixer(array $execute) |
|
101 | |||
102 | 7 | protected function configPhpUnit(array $execute) |
|
108 | |||
109 | /** |
||
110 | * @param string $tool |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 8 | protected function setQuestionTool($tool) |
|
118 | |||
119 | /** |
||
120 | * @param array $hookData |
||
121 | */ |
||
122 | 9 | protected function configMessage(array $hookData) |
|
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | public function hookName() |
||
134 | } |
||
135 |
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.