Conditions | 11 |
Paths | 34 |
Total Lines | 45 |
Code Lines | 25 |
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 |
||
35 | public function picture($dir = null, $width = 640, $height = 480, $fullPath = true, $randomize = true, $word = null) |
||
36 | { |
||
37 | $dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible |
||
38 | // Validate directory path |
||
39 | if (!is_dir($dir) || !is_writable($dir)) { |
||
40 | throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir)); |
||
41 | } |
||
42 | |||
43 | // Generate a random filename. Use the server address so that a file |
||
44 | // generated at the same time on a different server won't have a collision. |
||
45 | $name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)); |
||
46 | $filename = $name .'.jpg'; |
||
47 | $filepath = $dir . DIRECTORY_SEPARATOR . $filename; |
||
48 | |||
49 | $url = $this->pictureUrl($width, $height, $randomize, $word); |
||
50 | |||
51 | // save file |
||
52 | if (function_exists('curl_exec')) { |
||
53 | // use cURL |
||
54 | $fp = fopen($filepath, 'w'); |
||
55 | $ch = curl_init($url); |
||
56 | curl_setopt($ch, CURLOPT_FILE, $fp); |
||
57 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
58 | $success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200; |
||
59 | |||
60 | if ($success) { |
||
61 | fclose($fp); |
||
62 | } else { |
||
63 | unlink($filepath); |
||
64 | } |
||
65 | |||
66 | curl_close($ch); |
||
67 | } elseif (ini_get('allow_url_fopen')) { |
||
68 | // use remote fopen() via copy() |
||
69 | $success = copy($url, $filepath); |
||
70 | } else { |
||
71 | return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()'); |
||
72 | } |
||
73 | |||
74 | if (!$success) { |
||
75 | // could not contact the distant URL or HTTP error - fail silently. |
||
76 | return null; |
||
77 | } |
||
78 | |||
79 | return $fullPath ? $filepath : $filename; |
||
80 | } |
||
82 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.