| Conditions | 14 |
| Paths | 10 |
| Total Lines | 59 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function runActualTask($params = []): ?string |
||
| 26 | { |
||
| 27 | $errors = []; |
||
| 28 | foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
||
|
|
|||
| 29 | $searchPath = $this->mu()->findMyCodeDir($moduleDir); |
||
| 30 | if (file_exists($searchPath)) { |
||
| 31 | $this->mu()->colourPrint( |
||
| 32 | 'Searching in ' . $searchPath . ' for files with more than one class.', |
||
| 33 | 'blue' |
||
| 34 | ); |
||
| 35 | $fileFinder = new FindFiles(); |
||
| 36 | $flatArray = $fileFinder |
||
| 37 | ->setSearchPath($searchPath) |
||
| 38 | ->setExtensions(['php']) |
||
| 39 | ->getFlatFileArray(); |
||
| 40 | if (is_array($flatArray) && count($flatArray)) { |
||
| 41 | foreach ($flatArray as $path) { |
||
| 42 | // $className = basename($path, '.php'); |
||
| 43 | $classNames = []; |
||
| 44 | $content = file_get_contents($path); |
||
| 45 | $tokens = token_get_all($content); |
||
| 46 | for ($index = 0; isset($tokens[$index]); $index++) { |
||
| 47 | if (! isset($tokens[$index][0])) { |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | if ($tokens[$index][0] === T_CLASS && |
||
| 51 | $tokens[$index + 1][0] === T_WHITESPACE && |
||
| 52 | $tokens[$index + 2][0] === T_STRING |
||
| 53 | ) { |
||
| 54 | $index += 2; // Skip class keyword and whitespace |
||
| 55 | $classNames[] = $tokens[$index][1]; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | if (count($classNames) > 1) { |
||
| 59 | $errors[] = $path . ': ' . implode(', ', $classNames); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } else { |
||
| 63 | $this->mu()->colourPrint( |
||
| 64 | 'Could not find any files in ' . $searchPath, |
||
| 65 | 'red' |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | } elseif ($searchPath) { |
||
| 69 | $this->mu()->colourPrint( |
||
| 70 | 'Could not find the following path: "' . $searchPath, |
||
| 71 | 'blue' |
||
| 72 | ); |
||
| 73 | } else { |
||
| 74 | $this->mu()->colourPrint( |
||
| 75 | 'empty search path', |
||
| 76 | 'blue' |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | if (count($errors)) { |
||
| 81 | return 'Found files with multiple classes: ' . implode("\n\n ---\n\n", $errors); |
||
| 82 | } |
||
| 83 | return null; |
||
| 84 | } |
||
| 91 |