| Conditions | 18 |
| Paths | 172 |
| Total Lines | 82 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 67 | private function cleanup($path) |
||
| 68 | { |
||
| 69 | if (!in_array(substr($path, strlen($this->basedir) + 1), $this->excludeDirs, true)) { |
||
| 70 | # process files in $path |
||
| 71 | |||
| 72 | $files = array_merge( |
||
| 73 | glob($path . '/*.php'), |
||
| 74 | glob($path . '/*.tpl') |
||
| 75 | ); |
||
| 76 | |||
| 77 | foreach ($files as $filePath) { |
||
| 78 | $file_modified = false; |
||
| 79 | $lines = file($filePath); |
||
| 80 | $displayFilePath = substr($filePath, strlen($this->basedir) + 1); |
||
| 81 | |||
| 82 | # detect illegal characters at start of PHP or XML file |
||
| 83 | |||
| 84 | if (count($lines) && preg_match('/^(.+?)\<\?/', $lines[0], $matches)) { |
||
| 85 | $this->warn( |
||
| 86 | 'invalid character(s) "' . $matches[1] . '" at start of ' . $displayFilePath |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | # Remove trailing whitespaces, strip CRs, expand tabs, make |
||
| 91 | # sure that all - including the last - line end on "\n", |
||
| 92 | # and detect short open tags. Only-whitespace lines are |
||
| 93 | # allowed by PSR-2 and will not be trimmed. |
||
| 94 | |||
| 95 | $n = 1; |
||
| 96 | foreach ($lines as &$line) { |
||
| 97 | if (!preg_match("/^ *(\\*|\/\/|#) *\n$/", $line) |
||
| 98 | && (trim($line, " \n") !== '' || substr($line, -1) !== "\n") |
||
| 99 | ) { |
||
| 100 | $oldLine = $line; |
||
| 101 | $line = rtrim($line); # trims " \t\n\r\0\x0B" |
||
| 102 | $line = $this->expandTabs($line); |
||
| 103 | $line .= "\n"; |
||
| 104 | |||
| 105 | if ($line != $oldLine) { |
||
| 106 | $file_modified = true; |
||
| 107 | ++$this->linesModified; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | if (preg_match('/\<\?\s/', $line)) { # relies on \n at EOL |
||
| 111 | $this->warn('short open tag in line ' . $n . ' of ' . $displayFilePath); |
||
| 112 | } |
||
| 113 | ++$n; |
||
| 114 | } |
||
| 115 | unset($line); |
||
| 116 | |||
| 117 | # remove PHP close tags and empty lines from end of file |
||
| 118 | |||
| 119 | $l = count($lines) - 1; |
||
| 120 | while ($l > 0) { |
||
| 121 | $trimmed_line = trim($lines[$l]); |
||
| 122 | if ($trimmed_line === '?>' || $trimmed_line === '') { |
||
| 123 | unset($lines[$l]); |
||
| 124 | $file_modified = true; |
||
| 125 | ++$this->linesModified; |
||
| 126 | } else { |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | --$l; |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($file_modified) { |
||
| 133 | echo 'cleaned ' . substr($filePath, 2) . "\n"; |
||
| 134 | file_put_contents($filePath, implode('', $lines)); |
||
| 135 | ++$this->filesModified; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | # process subdirectories in $path |
||
| 140 | |||
| 141 | $dirs = glob($path . '/*', GLOB_ONLYDIR); |
||
| 142 | foreach ($dirs as $dir) { |
||
| 143 | if ($dir !== '.' && $dir !== '..') { |
||
| 144 | $this->cleanup($dir); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 174 |