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:
Complex classes like MagentoHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MagentoHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class MagentoHelper extends AbstractHelper |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $_magentoRootFolder = null; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $_magentoMajorVersion = \N98\Magento\Application::MAGENTO_MAJOR_VERSION_1; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $_magentoEnterprise = false; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $_magerunStopFileFound = false; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $_magerunStopFileFolder = null; |
||
46 | |||
47 | /** |
||
48 | * @var InputInterface |
||
49 | */ |
||
50 | protected $input; |
||
51 | |||
52 | /** |
||
53 | * @var OutputInterface |
||
54 | */ |
||
55 | protected $output; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $baseConfig = array(); |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $_customConfigFilename = 'n98-magerun2.yaml'; |
||
66 | |||
67 | /** |
||
68 | * Returns the canonical name of this helper. |
||
69 | * |
||
70 | * @return string The canonical name |
||
71 | * |
||
72 | * @api |
||
73 | */ |
||
74 | public function getName() |
||
78 | |||
79 | /** |
||
80 | * @param InputInterface $input |
||
1 ignored issue
–
show
|
|||
81 | * @param OutputInterface $output |
||
82 | */ |
||
83 | public function __construct(InputInterface $input = null, OutputInterface $output = null) |
||
84 | { |
||
85 | if (null === $input) { |
||
86 | $input = new ArgvInput(); |
||
87 | } |
||
88 | |||
89 | if (null === $output) { |
||
90 | $output = new ConsoleOutput(); |
||
91 | } |
||
92 | |||
93 | $this->input = $input; |
||
94 | $this->output = $output; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Start Magento detection |
||
99 | * |
||
100 | * @param string $folder |
||
101 | * @param array $subFolders [optional] sub-folders to check |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function detect($folder, array $subFolders = array()) |
||
105 | { |
||
106 | $folders = $this->splitPathFolders($folder); |
||
107 | $folders = $this->checkMagerunFile($folders); |
||
108 | $folders = $this->checkModman($folders); |
||
109 | $folders = array_merge($folders, $subFolders); |
||
110 | |||
111 | foreach (array_reverse($folders) as $searchFolder) { |
||
112 | if (!is_dir($searchFolder) || !is_readable($searchFolder)) { |
||
113 | continue; |
||
114 | } |
||
115 | |||
116 | $found = $this->_search($searchFolder); |
||
117 | if ($found) { |
||
118 | return true; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return false; |
||
123 | } |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getRootFolder() |
||
133 | |||
134 | public function getEdition() |
||
138 | |||
139 | /** |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function isEnterpriseEdition() |
||
146 | |||
147 | /** |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getMajorVersion() |
||
154 | |||
155 | /** |
||
156 | * @return boolean |
||
157 | */ |
||
158 | public function isMagerunStopFileFound() |
||
159 | { |
||
160 | return $this->_magerunStopFileFound; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getMagerunStopFileFolder() |
||
167 | { |
||
168 | return $this->_magerunStopFileFolder; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string $folder |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | protected function splitPathFolders($folder) |
||
177 | { |
||
178 | $folders = array(); |
||
179 | |||
180 | $folderParts = explode('/', $folder); |
||
181 | foreach ($folderParts as $key => $part) { |
||
182 | $explodedFolder = implode('/', array_slice($folderParts, 0, $key + 1)); |
||
183 | if ($explodedFolder !== '') { |
||
184 | $folders[] = $explodedFolder; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | return $folders; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Check for modman file and .basedir |
||
193 | * |
||
194 | * @param array $folders |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function checkModman(array $folders) |
||
199 | { |
||
200 | foreach (array_reverse($folders) as $searchFolder) { |
||
201 | View Code Duplication | if (!is_readable($searchFolder)) { |
|
202 | if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) { |
||
203 | $this->output->writeln( |
||
204 | '<debug>Folder <info>' . $searchFolder . '</info> is not readable. Skip.</debug>' |
||
205 | ); |
||
206 | } |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | $finder = Finder::create(); |
||
211 | $finder |
||
212 | ->files() |
||
213 | ->ignoreUnreadableDirs(true) |
||
214 | ->depth(0) |
||
215 | ->followLinks() |
||
216 | ->ignoreDotFiles(false) |
||
217 | ->name('.basedir') |
||
218 | ->in($searchFolder); |
||
219 | |||
220 | $count = $finder->count(); |
||
221 | if ($count > 0) { |
||
222 | $baseFolderContent = trim(file_get_contents($searchFolder . '/.basedir')); |
||
223 | if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) { |
||
224 | $this->output->writeln( |
||
225 | '<debug>Found modman .basedir file with content <info>' . $baseFolderContent . '</info></debug>' |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | if (!empty($baseFolderContent)) { |
||
230 | array_push($folders, $searchFolder . '/../' . $baseFolderContent); |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | |||
235 | return $folders; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Check for magerun stop-file |
||
240 | * |
||
241 | * @param array $folders |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | protected function checkMagerunFile(array $folders) |
||
288 | |||
289 | /** |
||
290 | * @param string $searchFolder |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | protected function _search($searchFolder) |
||
345 | |||
346 | /** |
||
347 | * @return array |
||
348 | * @throws RuntimeException |
||
349 | */ |
||
350 | public function getBaseConfig() |
||
351 | { |
||
352 | if (!$this->baseConfig) { |
||
353 | $this->initBaseConfig(); |
||
354 | } |
||
355 | |||
356 | return $this->baseConfig; |
||
357 | } |
||
358 | |||
359 | private function initBaseConfig() |
||
374 | |||
375 | /** |
||
376 | * private getter for application that has magento detected |
||
377 | * |
||
378 | * @return Application|\Symfony\Component\Console\Application |
||
379 | */ |
||
380 | private function getApplication() |
||
392 | |||
393 | private function addBaseConfig($root, $configFileName) |
||
408 | } |
||
409 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.