Completed
Pull Request — master (#24)
by
unknown
05:53
created

CodeTable::urlBase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Conf;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\Model\LazyResource;
7
8
/**
9
 * A single CodeTable resource.
10
 */
11
class CodeTable extends LazyResource
12
{
13
    /** @var string */
14
    public $code;
15
16
    /**
17
     * CodeTable constructor.
18
     *
19
     * @param Client $client
20
     * @param string $code
21
     */
22
    public function __construct(Client $client, $code)
23
    {
24
        parent::__construct($client);
25
        $this->code = $code;
26
    }
27
28
    /**
29
    * Return a single row referring to the code table.
30
    *
31
    * @param $code - The code in the Table we want to pull.
32
    *
33
    * @return object - the row in the code table.
34
    */
35
    public function getRowByCode($code)
36
    {
37
        $found = array();
38
        $codeTable = json_decode($this->client->get($this->urlBase()));
39
40
        foreach ($codeTable->row as $row) {
41
            if ($row->code == $code) {
42
                array_push($found,$row);
43
                $count++;
0 ignored issues
show
Bug introduced by
The variable $count 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...
44
            }
45
        }
46
47
        return($found);
48
    }
49
50
    /**
51
     * Check if we have the full representation of our data object.
52
     *
53
     * @param \stdClass $data
54
     *
55
     * @return bool
56
     */
57
    protected function isInitialized($data)
58
    {
59
        return isset($data->name);
60
    }
61
62
    /**
63
     * Generate the base URL for this resource.
64
     *
65
     * @return string
66
     */
67
    protected function urlBase()
68
    {
69
        return "/conf/code-tables/{$this->code}";
70
    }
71
72
}
73