Completed
Push — master ( 2cf8eb...09dcac )
by Дмитрий
04:07
created

JumpIfNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\ControlFlow\Node;
7
8
use PHPSA\ControlFlow\Block;
9
10
class JumpIfNode extends AbstractNode
11
{
12
    /**
13
     * @var AbstractNode
14
     */
15
    protected $cond;
16
17
    /**
18
     * @var Block
19
     */
20
    protected $if;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $if. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
21
22
    /**
23
     * @var Block|null
24
     */
25
    protected $else;
26
27 6
    public function __construct(AbstractNode $cond, Block $if)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $if. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
28
    {
29 6
        $this->if = $if;
30 6
        $this->cond = $cond;
31 6
    }
32
33
    /**
34
     * @param Block $if
35
     */
36
    public function setIf(Block $if)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $if. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
37
    {
38
        $this->if = $if;
39
    }
40
41
    /**
42
     * @param Block $else
43
     */
44 3
    public function setElse(Block $else)
45
    {
46 3
        $this->else = $else;
47 3
    }
48
49
    public function getSubVariables()
50
    {
51
        return [
52
            'cond' => $this->cond
53
        ];
54
    }
55
56
    public function getSubBlocks()
57
    {
58
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('if' => $th...'else' => $this->else); seems to be an array, but some of its elements' types (null) are incompatible with the return type of the parent method PHPSA\ControlFlow\Node\AbstractNode::getSubBlocks of type PHPSA\ControlFlow\Block[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
            'if' => $this->if,
60
            'else' => $this->else,
61
        ];
62
    }
63
}
64