Conditions | 11 |
Paths | 13 |
Total Lines | 53 |
Lines | 7 |
Ratio | 13.21 % |
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 |
||
161 | public function run() |
||
162 | { |
||
163 | View Code Duplication | if (!in_array($this->compiler, $this->compilers, true) |
|
|
|||
164 | && !is_callable($this->compiler) |
||
165 | ) { |
||
166 | $message = sprintf('Invalid ' . static::FORMAT_NAME . ' compiler %s!', $this->compiler); |
||
167 | |||
168 | return Result::error($this, $message); |
||
169 | } |
||
170 | |||
171 | foreach ($this->files as $in => $out) { |
||
172 | if (!file_exists($in)) { |
||
173 | $message = sprintf('File %s not found.', $in); |
||
174 | |||
175 | return Result::error($this, $message); |
||
176 | } |
||
177 | if (file_exists($out) && !is_writable($out)) { |
||
178 | return Result::error($this, 'Destination already exists and cannot be overwritten.'); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | foreach ($this->files as $in => $out) { |
||
183 | $css = $this->compile($in); |
||
184 | |||
185 | if ($css instanceof Result) { |
||
186 | return $css; |
||
187 | } elseif (false === $css) { |
||
188 | $message = sprintf( |
||
189 | ucfirst(static::FORMAT_NAME) . ' compilation failed for %s.', |
||
190 | $in |
||
191 | ); |
||
192 | |||
193 | return Result::error($this, $message); |
||
194 | } |
||
195 | |||
196 | $dst = $out . '.part'; |
||
197 | $write_result = file_put_contents($dst, $css); |
||
198 | |||
199 | if (false === $write_result) { |
||
200 | $message = sprintf('File write failed: %s', $out); |
||
201 | |||
202 | @unlink($dst); |
||
203 | return Result::error($this, $message); |
||
204 | } |
||
205 | |||
206 | // Cannot be cross-volume: should always succeed |
||
207 | @rename($dst, $out); |
||
208 | |||
209 | $this->printTaskSuccess('Wrote CSS to {filename}', ['filename' => $out]); |
||
210 | } |
||
211 | |||
212 | return Result::success($this, 'All ' . static::FORMAT_NAME . ' files compiled.'); |
||
213 | } |
||
214 | } |
||
215 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.