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