Passed
Branch release/1.5.0 (86328d)
by vincent
02:29
created

Jobs::genreItemGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Tmdb package.
5
 *
6
 * (c) Vincent Faliès <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Vincent Faliès <[email protected]>
12
 * @copyright Copyright (c) 2017
13
 */
14
15
namespace vfalies\tmdb\Catalogs;
16
17
use vfalies\tmdb\Interfaces\TmdbInterface;
18
use vfalies\tmdb\lib\Guzzle\Client as HttpClient;
19
use vfalies\tmdb\Exceptions\TmdbException;
20
21
/**
22
 * Class to get jobs list with department
23
 * @package Tmdb
24
 * @author Vincent Faliès <[email protected]>
25
 * @copyright Copyright (c) 2017
26
 */
27
class Jobs
28
{
29
30
    /**
31
     * Tmdb object
32
     * @var TmdbInterface
33
     */
34
    protected $tmdb = null;
35
36
    /**
37
     * Constructor
38
     * @param \vfalies\tmdb\Interfaces\TmdbInterface; $tmdb
0 ignored issues
show
Documentation introduced by
The doc-type \vfalies\tmdb\Interfaces\TmdbInterface; could not be parsed: Expected "|" or "end of type", but got ";" at position 38. (view supported doc-types)

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.

Loading history...
39
     */
40 2
    public function __construct(TmdbInterface $tmdb)
41
    {
42 2
        $this->tmdb = $tmdb;
43 2
    }
44
45
    /**
46
     * Get job list
47
     * @param array $options
48
     * @return \Generator|\stdClass
49
     * @throws \vfalies\tmdb\Catalogs\TmdbException
50
     */
51 2
    public function getList(array $options = array())
52
    {
53
        try
54
        {
55 2
            $params   = $this->tmdb->checkOptions($options);
56 2
            $response = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), 'job/list', null, $params);
57
58 1
            $jobs = [];
0 ignored issues
show
Unused Code introduced by
$jobs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59 1
            if (isset($response->jobs))
60
            {
61 1
                $results = [];
62 1
                foreach ($response->jobs as $j)
63
                {
64 1
                    $result             = new \stdClass();
65 1
                    $result->department = $j->department;
66 1
                    $result->jobs       = $j->job_list;
67
68 1
                    $results[] = $result;
69
                }
70
            }
71
        }
72 1
        catch (TmdbException $ex)
73
        {
74 1
            throw $ex;
75
        }
76 1
        return $this->genreItemGenerator($results);
0 ignored issues
show
Bug introduced by
The variable $results 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...
77
    }
78
79
    /**
80
     * Genre Item generator method
81
     * @param array $results
82
     */
83 1
    private function genreItemGenerator(array $results)
84
    {
85 1
        foreach ($results as $result) {
86 1
            yield $result;
87
        }
88 1
    }
89
}
90