Conditions | 9 |
Paths | 28 |
Total Lines | 60 |
Code Lines | 37 |
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 |
||
46 | public function sendData($data) |
||
47 | { |
||
48 | if (!in_array(substr($data['images_file'], -4), static::ALLOWED_FILE_TYPES)) { |
||
49 | throw new \InvalidArgumentException( |
||
50 | 'Image file needs to be one of the following types: ' . implode(', ', static::ALLOWED_FILE_TYPES) |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | $params = [ |
||
55 | [ |
||
56 | 'name' => 'images_file', |
||
57 | 'contents' => fopen($data['images_file'], 'r'), |
||
58 | 'filename' => $data['images_file'] |
||
59 | ] |
||
60 | ]; |
||
61 | |||
62 | if (isset($data['classifier_ids'])) { |
||
63 | if (is_array($data['classifier_ids'])) { |
||
64 | $classifierIdParams = []; |
||
65 | foreach ($data['classifier_ids'] as $id) { |
||
66 | $classifierIdParams[] = ['classifier_id' => $id]; |
||
67 | } |
||
68 | |||
69 | $params[] = [ |
||
70 | 'name' => 'classifier_ids', |
||
71 | 'contents' => json_encode(['classifiers' => $classifierIdParams]) |
||
72 | ]; |
||
73 | } else { |
||
74 | throw new \InvalidArgumentException('Classifier IDs must be array'); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | $multipartStream = new MultipartStream($params); |
||
79 | |||
80 | $request = new Request( |
||
81 | 'POST', |
||
82 | $this->getApiUrl(static::CLASSIFY_PATH) . '?' . http_build_query(['version' => $data['version']]), |
||
83 | ['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])], |
||
84 | $multipartStream |
||
85 | ); |
||
86 | |||
87 | try { |
||
88 | $response = $this->httpClient->send($request); |
||
89 | |||
90 | if ($response->getStatusCode() != 200) { |
||
91 | $error = $response->getStatusCode() . " Response Received: " . $response->getBody()->getContents(); |
||
92 | throw new \Exception($error, $response->getStatusCode()); |
||
93 | } |
||
94 | } catch (ClientException $e) { |
||
95 | if ($e->getCode() == 401) { |
||
96 | throw new AuthException('Invalid credentials provided'); |
||
97 | } elseif ($e->getCode() == 415) { |
||
98 | throw new \InvalidArgumentException('Unsupported image type'); |
||
99 | } else { |
||
100 | throw $e; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $this->response = new ClassifyResponse($this, $response->getBody()); |
||
105 | } |
||
106 | } |
||
107 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.