Completed
Push — master ( 411d94...8fae9a )
by John
02:59
created

GeneratorSerialiser::addCallSerialise()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 5
nop 1
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient\ClassGenerator\Generator;
14
15
use Graze\UnicontrollerClient\ClassGenerator\Generator\AbstractGenerator;
16
use Graze\UnicontrollerClient\ClassGenerator\Generator\GeneratorInterface;
17
use Graze\UnicontrollerClient\ClassGenerator\Definition\DefinitionProperty;
18
19
class GeneratorSerialiser extends AbstractGenerator implements GeneratorInterface
20
{
21
    const OUTPUT_PATH = 'src/Serialiser/Serialiser/Serialiser%s.php';
22
23
    /**
24
     * @var array
25
     */
26
    private $callsSerialise = [];
27
28
    /**
29
     * @param string $name
30
     * @return string
31
     */
32
    public function generateClass($name)
33
    {
34
        return sprintf(
35
            $this->getTemplate('Serialiser/SerialiserClass'),
36
            $name,
37
            implode("\n", $this->callsSerialise)
38
        );
39
    }
40
41
    /**
42
     * @param DefinitionProperty $property
43
     */
44
    public function addCallSerialise(DefinitionProperty $property)
45
    {
46
        switch ($property->getType()) {
47
            case DefinitionProperty::PROPERTY_TYPE_ARRAY:
48
                $call = sprintf(
49
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseArray'),
50
                    $property->getName(),
51
                    $property->getArrayElementName()
52
                );
53
                break;
54
55
            case DefinitionProperty::PROPERTY_TYPE_STRING:
56
                $call = sprintf(
57
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseString'),
58
                    $property->getName()
59
                );
60
                break;
61
62
            case DefinitionProperty::PROPERTY_TYPE_INT:
63
                $call = sprintf(
64
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseInt'),
65
                    $property->getName()
66
                );
67
                break;
68
69
            case DefinitionProperty::PROPERTY_TYPE_BINARY_DATA:
70
                $call = sprintf(
71
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseBinaryData'),
72
                    $property->getName()
73
                );
74
                break;
75
        }
76
77
        $this->callsSerialise[] = $call;
0 ignored issues
show
Bug introduced by
The variable $call 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...
78
    }
79
80
    /**
81
     * @param string $name
82
     *
83
     * @return [type]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(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...
84
     */
85
    public function getOutputPath($name)
86
    {
87
        return sprintf(self::OUTPUT_PATH, $name);
88
    }
89
}
90