Test Failed
Push — master ( 33bfdc...47f867 )
by Kirill
02:32
created

Definition   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getArguments() 0 4 1
A addArgument() 0 4 1
A hasArgument() 0 4 1
A getArgument() 0 4 1
A getName() 0 4 1
A getContext() 0 4 1
A isGeneric() 0 4 1
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\Type\TypeNameInterface;
14
15
/**
16
 * Class Definition
17
 */
18
class Definition implements DefinitionInterface
19
{
20
    /**
21
     * @var TypeNameInterface
22
     */
23
    private $name;
24
25
    /**
26
     * @var ContextInterface
27
     */
28
    private $context;
29
30
    /**
31
     * @var array
32
     */
33
    private $arguments = [];
34
35
    /**
36
     * Definition constructor.
37
     * @param ContextInterface $context
38
     * @param TypeNameInterface $name
39
     */
40
    public function __construct(ContextInterface $context, TypeNameInterface $name)
41
    {
42
        $this->context = $context;
43
        $this->name = $name->in($context->getName());
0 ignored issues
show
Documentation Bug introduced by
It seems like $name->in($context->getName()) of type object<self> is incompatible with the declared type object<Railt\SDL\IR\Type\TypeNameInterface> of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    /**
47
     * @return iterable|DefinitionArgumentInterface[]
48
     */
49
    public function getArguments(): iterable
50
    {
51
        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...tionArgumentInterface[].

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...
52
    }
53
54
    /**
55
     * @param string $name
56
     * @param TypeNameInterface $hint
57
     * @return DefinitionArgumentInterface
58
     */
59
    public function addArgument(string $name, TypeNameInterface $hint): DefinitionArgumentInterface
60
    {
61
        return $this->arguments[$name] = new Argument($name, $hint);
62
    }
63
64
    /**
65
     * @param string $name
66
     * @return bool
67
     */
68
    public function hasArgument(string $name): bool
69
    {
70
        return isset($this->arguments[$name]);
71
    }
72
73
    /**
74
     * @param string $name
75
     * @return DefinitionArgumentInterface
76
     */
77
    public function getArgument(string $name): DefinitionArgumentInterface
78
    {
79
        return $this->arguments[$name];
80
    }
81
82
    /**
83
     * @return TypeNameInterface
84
     */
85
    public function getName(): TypeNameInterface
86
    {
87
        return $this->name;
88
    }
89
90
    /**
91
     * @return ContextInterface
92
     */
93
    public function getContext(): ContextInterface
94
    {
95
        return $this->context;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function isGeneric(): bool
102
    {
103
        return \count($this->arguments) > 0;
104
    }
105
}
106