Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

BaseDocument::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 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\Base;
11
12
use Railt\Io\Readable;
13
use Railt\SDL\Base\Definitions\BaseDefinition;
14
use Railt\SDL\Base\Invocations\Directive\BaseDirectivesContainer;
15
use Railt\SDL\Contracts\Definitions\Definition;
16
use Railt\SDL\Contracts\Definitions\SchemaDefinition;
17
use Railt\SDL\Contracts\Definitions\TypeDefinition;
18
use Railt\SDL\Contracts\Document;
19
use Railt\SDL\Contracts\Type;
20
21
/**
22
 * Class BaseDocument
23
 */
24
abstract class BaseDocument extends BaseDefinition implements Document
25
{
26
    use BaseDirectivesContainer;
27
28
    /**
29
     * Document type name
30
     */
31
    protected const TYPE_NAME = Type::DOCUMENT;
32
33
    /**
34
     * @var SchemaDefinition|null
35
     */
36
    private $schema;
37
38
    /**
39
     * @var array|TypeDefinition[]
40
     */
41
    protected $types = [];
42
43
    /**
44
     * @var array|Definition[]
45
     */
46
    protected $definitions = [];
47
48
    /**
49
     * @var Readable
50
     */
51
    protected $file;
52
53
    /**
54
     * @return null|SchemaDefinition
55
     */
56 48
    public function getSchema(): ?SchemaDefinition
57
    {
58 48
        if ($this->schema === null) {
59 48
            foreach ($this->types as $type) {
60 48
                if ($type instanceof SchemaDefinition) {
61 48
                    return $this->schema = $type;
62
                }
63
            }
64
        }
65
66
        return $this->schema;
67
    }
68
69
    /**
70
     * @return iterable
71
     */
72 7189
    public function getTypeDefinitions(): iterable
73
    {
74 7189
        return \array_values($this->types);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \array_values($this->types); (array) is incompatible with the return type declared by the interface Railt\SDL\Contracts\Document::getTypeDefinitions of type Railt\SDL\Contracts\iter...itions\TypeDefinition[].

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...
75
    }
76
77
    /**
78
     * @param string $name
79
     * @return null|TypeDefinition
80
     */
81 307
    public function getTypeDefinition(string $name): ?TypeDefinition
82
    {
83 307
        return $this->types[$name] ?? null;
84
    }
85
86
    /**
87
     * @param string $name
88
     * @return bool
89
     */
90 18
    public function hasTypeDefinition(string $name): bool
91
    {
92 18
        return \array_key_exists($name, $this->types);
93
    }
94
95
    /**
96
     * @return int
97
     */
98 18
    public function getNumberOfTypeDefinitions(): int
99
    {
100 18
        return \count($this->types);
101
    }
102
103
    /**
104
     * @return iterable
105
     */
106 7165
    public function getDefinitions(): iterable
107
    {
108 7165
        return \array_values(\array_merge($this->types, $this->definitions));
109
    }
110
111
    /**
112
     * @return Readable
113
     */
114 3651
    public function getFile(): Readable
115
    {
116 3651
        return $this->file;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 3651
    final public function getFileName(): string
123
    {
124 3651
        return $this->file->getPathname();
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getContents(): string
131
    {
132
        return $this->file->getContents();
133
    }
134
135
    /**
136
     * @return array
137
     */
138 1257
    public function __sleep(): array
139
    {
140 1257
        return \array_merge(parent::__sleep(), [
141
            // File
142 1257
            'file',
143
144
            // instanceof TypeDefinition
145
            'types',
146
147
            // instanceof Definition
148
            'definitions',
149
150
            // Directives
151
            'directives',
152
        ]);
153
    }
154
155
    /**
156
     * @return string
157
     */
158 66
    public function getTypeName(): string
159
    {
160 66
        return self::TYPE_NAME;
161
    }
162
}
163