These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace IBM\Watson\VisualRecognition\Api; |
||
4 | |||
5 | use IBM\Watson\Common\Api\AbstractApi; |
||
6 | use IBM\Watson\VisualRecognition\Model\ClassifiedImages; |
||
7 | |||
8 | class Classify extends AbstractApi |
||
9 | { |
||
10 | /** |
||
11 | * Classify image |
||
12 | * |
||
13 | * @param string|resource $image |
||
14 | * @param array $params |
||
15 | * |
||
16 | * @return \IBM\Watson\VisualRecognition\Model\ClassifiedImages |
||
17 | * |
||
18 | * @throws \Http\Client\Exception |
||
19 | * @throws \Exception |
||
20 | */ |
||
21 | public function classify($image, $params = []) |
||
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
22 | { |
||
23 | if (filter_var($image, FILTER_VALIDATE_URL)) { |
||
24 | $response = $this->classifyUrl($image, $params); |
||
0 ignored issues
–
show
It seems like
$image defined by parameter $image on line 21 can also be of type resource ; however, IBM\Watson\VisualRecogni...Classify::classifyUrl() does only seem to accept string , maybe add an additional type check?
This check looks at variables that have been passed in as parameters and are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble.
Loading history...
|
|||
25 | } |
||
26 | |||
27 | if (file_exists($image)) { |
||
28 | $image = fopen($image, 'r'); |
||
29 | } |
||
30 | |||
31 | if (is_resource($image)) { |
||
32 | $params[] = [ |
||
33 | 'name' => 'images_file', |
||
34 | 'content' => $image |
||
35 | ]; |
||
36 | |||
37 | $response = $this->postRaw('/v3/classify', $params); |
||
38 | } |
||
39 | |||
40 | if ($response->getStatusCode() !== 200) { |
||
41 | $this->handleErrors($response); |
||
0 ignored issues
–
show
The variable
$response does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
42 | } |
||
43 | |||
44 | return $this->hydrator->hydrate($response, ClassifiedImages::class); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Classify image by url |
||
49 | * |
||
50 | * @param string $url |
||
51 | * @param array $params |
||
52 | * |
||
53 | * @return \Psr\Http\Message\ResponseInterface |
||
54 | * |
||
55 | * @throws \Http\Client\Exception |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | public function classifyUrl($url, $params = []) |
||
59 | { |
||
60 | $params['url'] = $url; |
||
61 | |||
62 | $response = $this->get('/v3/classify', $params); |
||
63 | |||
64 | if ($response->getStatusCode() !== 200) { |
||
65 | $this->handleErrors($response); |
||
66 | } |
||
67 | |||
68 | return $response; |
||
69 | } |
||
70 | } |
||
71 |