Conditions | 4 |
Paths | 4 |
Total Lines | 59 |
Code Lines | 19 |
Lines | 59 |
Ratio | 100 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
48 | public function upload($file) |
||
49 | { |
||
50 | if (false === file_exists($file)) |
||
51 | { |
||
52 | throw new \UnexpectedValueException('File not found for upload'); |
||
53 | } |
||
54 | |||
55 | /* |
||
56 | * @todo Crowdin does not accept Guzzle's POST requests. |
||
57 | * She just tells me: |
||
58 | * <code>4</code> |
||
59 | * <message>No files specified in request</message> |
||
60 | * |
||
61 | * :( |
||
62 | |||
63 | return $this->getHttpClient() |
||
64 | ->post($this->getBasePath('upload-tm'), ['form_params' => ['file' => $file]]); |
||
65 | |||
66 | HELP WANTED !!! |
||
67 | |||
68 | */ |
||
69 | |||
70 | $post_params = array(); |
||
71 | $request_url = 'https://api.crowdin.com/api/' . $this->getBasePath('upload-tm'); |
||
72 | |||
73 | if (function_exists('curl_file_create')) |
||
74 | { |
||
75 | $post_params['file'] = curl_file_create($file); |
||
76 | } |
||
77 | else |
||
78 | { |
||
79 | /* |
||
80 | * This does NOT seem to work with Mrs. Crowdin... |
||
81 | * ... comes from their docs |
||
82 | * $post_params['file'] = '@/home/crowdin/test.tmx'; |
||
83 | */ |
||
84 | throw new \RuntimeException('This version of cURL does not seem to work (with Crowdin...)'); |
||
85 | } |
||
86 | |||
87 | $ch = curl_init(); |
||
88 | |||
89 | curl_setopt($ch, CURLOPT_URL, $request_url); |
||
90 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
91 | curl_setopt($ch, CURLOPT_POST, true); |
||
92 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); |
||
93 | |||
94 | $result = curl_exec($ch); |
||
95 | |||
96 | curl_close($ch); |
||
97 | |||
98 | if (false === $result) |
||
99 | { |
||
100 | throw new \UnexpectedValueException('File upload failed'); |
||
101 | |||
102 | // @todo Some more errors pls.... |
||
103 | } |
||
104 | |||
105 | return 'File has been uploaded (?)'; |
||
106 | } |
||
107 | } |
||
108 |
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.