Completed
Push — master ( b2a65d...63a169 )
by Kirill
02:18
created

Invocation::addArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend\Definition;
11
12
use Railt\SDL\Frontend\Context\ContextInterface;
13
use Railt\SDL\IR\SymbolTable\PrimitiveInterface;
14
use Railt\SDL\IR\Type\TypeNameInterface;
15
16
/**
17
 * Class InvocationPrimitive
18
 */
19
class Invocation implements InvocationInterface
20
{
21
    /**
22
     * @var TypeNameInterface
23
     */
24
    private $name;
25
26
    /**
27
     * @var array
28
     */
29
    private $arguments = [];
30
31
    /**
32
     * @var ContextInterface
33
     */
34
    private $from;
35
36
    /**
37
     * InvocationPrimitive constructor.
38
     * @param TypeNameInterface $name
39
     * @param ContextInterface $from
40
     */
41
    public function __construct(TypeNameInterface $name, ContextInterface $from)
42
    {
43
        $this->name = $name;
44
        $this->from = $from;
45
    }
46
47
    /**
48
     * @return ContextInterface
49
     */
50
    public function getContext(): ContextInterface
51
    {
52
        return $this->from;
53
    }
54
55
    /**
56
     * @return iterable|InvocationInterface[]
57
     */
58
    public function getArguments(): iterable
59
    {
60
        return $this->arguments;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->arguments; (array) is incompatible with the return type declared by the interface Railt\SDL\Frontend\Defin...Interface::getArguments of type Railt\SDL\Frontend\Defin...n\InvocationInterface[].

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 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('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...
61
    }
62
63
    /**
64
     * @param string $name
65
     * @param Invocation|PrimitiveInterface $value
66
     * @return InvocationInterface
67
     */
68
    public function addArgument(string $name, $value): InvocationInterface
69
    {
70
        $this->arguments[$name] = $value;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return TypeNameInterface
77
     */
78
    public function getName(): TypeNameInterface
79
    {
80
        return $this->name;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function __toString(): string
87
    {
88
        if ($this->hasArguments()) {
89
            $arguments = [];
90
91
            foreach ($this->arguments as $name => $value) {
92
                $arguments[] = $name . ': ' . $value;
93
            }
94
95
            return \sprintf('%s<%s>', $this->name->getFullyQualifiedName(), \implode(', ', $arguments));
96
        }
97
98
        return $this->name->getFullyQualifiedName();
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    public function hasArguments(): bool
105
    {
106
        return \count($this->arguments) > 0;
107
    }
108
}
109