Completed
Push — develop ( bc21b0...0bdfd4 )
by Adam
24:26 queued 09:29
created

Classify   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B classify() 0 25 5
A classifyUrl() 0 12 2
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
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
22
    {
23
        if (filter_var($image, FILTER_VALIDATE_URL)) {
24
            $response = $this->classifyUrl($image, $params);
0 ignored issues
show
Bug introduced by
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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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