Test Failed
Push — master ( 16c39a...9c7d46 )
by Kirill
07:07
created

Record::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
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\Record;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\Parser\Finder;
14
use Railt\SDL\Frontend\Type\TypeInterface;
15
use Railt\SDL\Frontend\Type\TypeNameInterface;
16
17
/**
18
 * Class Record
19
 */
20
class Record implements RecordInterface, \JsonSerializable
21
{
22
    /**
23
     * @var TypeNameInterface
24
     */
25
    private $name;
26
27
    /**
28
     * @var RuleInterface
29
     */
30
    private $ast;
31
32
    /**
33
     * @var array|ArgumentInterface[]
34
     */
35
    private $arguments = [];
36
    /**
37
     * @var TypeInterface
38
     */
39
    private $type;
40
41
    /**
42
     * Record constructor.
43
     * @param TypeNameInterface $name
44
     * @param TypeInterface $type
45
     * @param RuleInterface $ast
46
     */
47
    public function __construct(TypeNameInterface $name, TypeInterface $type, RuleInterface $ast)
48
    {
49
        $this->name = $name;
50
        $this->ast = $ast;
51
        $this->type = $type;
52
    }
53
54
    /**
55
     * @return TypeInterface
56
     */
57
    public function getType(): TypeInterface
58
    {
59
        return $this->type;
60
    }
61
62
    /**
63
     * @return iterable|array
64
     */
65
    public function getArguments(): iterable
66
    {
67
        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\Recor...Interface::getArguments of type Railt\SDL\Frontend\Recor...ord\ArgumentInterface[].

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...
68
    }
69
70
    /**
71
     * @param ArgumentInterface $argument
72
     * @return RecordInterface
73
     */
74
    public function addArgument(ArgumentInterface $argument): RecordInterface
75
    {
76
        $this->arguments[$argument->getName()] = $argument;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return TypeNameInterface
83
     */
84
    public function getName(): TypeNameInterface
85
    {
86
        return $this->name;
87
    }
88
89
    /**
90
     * @param string $query
91
     * @return Finder
92
     */
93
    public function find(string $query): Finder
94
    {
95
        return $this->ast->find($query);
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function jsonSerialize(): array
102
    {
103
        return [
104
            'name'      => $this->name,
105
            'type'      => $this->type,
106
            'arguments' => $this->arguments,
107
        ];
108
    }
109
}
110