GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b3f20b...f5717e )
by James
03:07
created

BlockDirective::isMainContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of WebHelper Parser.
5
 *
6
 * (c) James <[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
12
namespace WebHelper\Parser\Directive;
13
14
use WebHelper\Parser\Server\ServerInterface;
15
16
/**
17
 * Describes a block directive instance or a context.
18
 *
19
 * a BlockDirective is an ordered list of other directives set in a context.
20
 *
21
 * @author James <[email protected]>
22
 */
23
class BlockDirective extends Directive implements DirectiveInterface
24
{
25
    /** @var array orderd list of sub directives */
26
    private $directives = [];
27
28
    /**
29
     * Adds a Directive at the end of the list.
30
     *
31
     * @param DirectiveInterface $directive a directive to add
32
     */
33 9
    public function add(DirectiveInterface $directive)
34
    {
35 9
        $this->directives[] = $directive;
36
37 9
        return $this;
38
    }
39
40
    /**
41
     * Confirms if the directive contains a specified directive.
42
     *
43
     * @param string $name the directive for which to check existence
44
     *
45
     * @return bool true if the sub-directive exists, false otherwise
46
     */
47 1
    public function hasDirective($name)
48
    {
49 1
        $inSubDirective = false;
50
51 1
        foreach ($this->directives as $directive) {
52 1
            if ($directive->getName() == $name) {
53 1
                return true;
54
            }
55
56 1
            $inSubDirective = $this->hasInnerDirective($name, $inSubDirective, $directive);
57 1
        }
58
59 1
        return $inSubDirective;
60
    }
61
62
    /**
63
     * Looks into sub directives to confirm if the actual contains a specified directive.
64
     *
65
     * @param string             $name           the directive for which to check existence
66
     * @param bool               $inSubDirective the actual state
67
     * @param DirectiveInterface $directive      the sub directive to look into
68
     *
69
     * @return bool true if the sub-directive exists, false otherwise
70
     */
71 1
    private function hasInnerDirective($name, $inSubDirective, DirectiveInterface $directive)
72
    {
73 1
        if (!$directive->isSimple()) {
74 1
            $inSubDirective = $inSubDirective || $directive->hasDirective($name);
75 1
        }
76
77 1
        return $inSubDirective;
78
    }
79
80
    /**
81
     * Confirms if the directive is simple.
82
     *
83
     * Block directive can have sub directives
84
     *
85
     * @return bool true if the directive is simple, false otherwise
86
     */
87 1
    public function isSimple()
88
    {
89 1
        return false;
90
    }
91
92
    /**
93
     * Dumps the directive respecting a server syntax.
94
     *
95
     * @param ServerInterface $server a server instance
96
     * @param int             $spaces the indentation spaces
97
     *
98
     * @return string the dumped directive
99
     */
100 1
    public function dump(ServerInterface $server, $spaces = 0)
101
    {
102 1
        $config = '';
103
104 1
        if (!$this->isMainContext()) {
105 1
            $value = $this->getValue() ? ' '.$this->getValue() : '';
106 1
            $config .= str_repeat(' ', $spaces).sprintf(
107 1
                $server->getDumperStartDirective(),
108 1
                $this->getName(),
109
                $value
110 1
            ).PHP_EOL;
111 1
        }
112
113 1
        foreach ($this->directives as $directive) {
114 1
            $config .= $directive->dump($server, $spaces + ($this->isMainContext() ? 0 : 4));
115 1
        }
116
117 1
        if (!$this->isMainContext()) {
118 1
            $config .= str_repeat(' ', $spaces).sprintf(
119 1
                $server->getDumperEndDirective(),
120 1
                $this->getName(),
121
                $value
0 ignored issues
show
Bug introduced by
The variable $value 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...
122 1
            ).PHP_EOL;
123 1
        }
124
125 1
        return $config;
126
    }
127
128
    /**
129
     * Confirms if a Block directive is a 'main' context.
130
     *
131
     * @return bool true if the name of the block directive is 'main', false otherwise
132
     */
133 1
    private function isMainContext()
134
    {
135 1
        return 'main' == $this->getName();
136
    }
137
}
138