| Conditions | 14 |
| Paths | 257 |
| Total Lines | 79 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 159 | public function main() |
||
| 160 | { |
||
| 161 | if ($this->file === null && empty($this->filesets)) { |
||
| 162 | throw new BuildException("You must specify a file or fileset(s) for the <ReplaceRegexp> task."); |
||
| 163 | } |
||
| 164 | |||
| 165 | // compile a list of all files to modify, both file attrib and fileset elements |
||
| 166 | // can be used. |
||
| 167 | $files = []; |
||
| 168 | |||
| 169 | if ($this->file !== null) { |
||
| 170 | $files[] = $this->file; |
||
| 171 | } |
||
| 172 | |||
| 173 | if (!empty($this->filesets)) { |
||
| 174 | $filenames = []; |
||
| 175 | foreach ($this->filesets as $fs) { |
||
| 176 | try { |
||
| 177 | $ds = $fs->getDirectoryScanner($this->project); |
||
| 178 | $filenames = $ds->getIncludedFiles(); // get included filenames |
||
| 179 | $dir = $fs->getDir($this->project); |
||
| 180 | foreach ($filenames as $fname) { |
||
| 181 | $files[] = new File($dir, $fname); |
||
| 182 | } |
||
| 183 | } catch (BuildException $be) { |
||
| 184 | $this->log($be->getMessage(), Project::MSG_WARN); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->log("Applying Regexp processing to " . count($files) . " files."); |
||
| 190 | |||
| 191 | // These "slots" allow filters to retrieve information about the currently-being-process files |
||
| 192 | $slot = $this->getRegisterSlot("currentFile"); |
||
| 193 | $basenameSlot = $this->getRegisterSlot("currentFile.basename"); |
||
| 194 | |||
| 195 | $filter = new FilterChain($this->project); |
||
| 196 | |||
| 197 | $r = new ReplaceRegexp(); |
||
| 198 | $r->setRegexps([$this->regexp]); |
||
| 199 | |||
| 200 | $filter->addReplaceRegexp($r); |
||
| 201 | $filters = [$filter]; |
||
| 202 | |||
| 203 | foreach ($files as $file) { |
||
| 204 | // set the register slots |
||
| 205 | |||
| 206 | $slot->setValue($file->getPath()); |
||
| 207 | $basenameSlot->setValue($file->getName()); |
||
| 208 | |||
| 209 | // 1) read contents of file, pulling through any filters |
||
| 210 | $in = null; |
||
| 211 | $out = null; |
||
| 212 | $contents = ""; |
||
| 213 | |||
| 214 | try { |
||
| 215 | $in = FileUtils::getChainedReader(new FileReader($file), $filters, $this->project); |
||
| 216 | while (-1 !== ($buffer = $in->read())) { |
||
| 217 | $contents .= $buffer; |
||
| 218 | } |
||
| 219 | $in->close(); |
||
| 220 | } catch (Exception $e) { |
||
| 221 | if ($in) { |
||
| 222 | $in->close(); |
||
| 223 | } |
||
| 224 | $this->log("Error reading file: " . $e->getMessage(), Project::MSG_WARN); |
||
| 225 | } |
||
| 226 | |||
| 227 | try { |
||
| 228 | // now create a FileWriter w/ the same file, and write to the file |
||
| 229 | $out = new FileWriter($file); |
||
| 230 | $out->write($contents); |
||
| 231 | $out->close(); |
||
| 232 | $this->log("Applying regexp processing to " . $file->getPath(), Project::MSG_VERBOSE); |
||
| 233 | } catch (Exception $e) { |
||
| 234 | if ($out) { |
||
| 235 | $out->close(); |
||
| 236 | } |
||
| 237 | $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN); |
||
| 238 | } |
||
| 242 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.