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