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

BaseUnion   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 58
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypes() 0 4 1
A hasType() 0 4 1
A getType() 0 4 1
A getNumberOfTypes() 0 4 1
A __sleep() 0 6 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\Definitions;
11
12
use Railt\SDL\Base\Invocations\Directive\BaseDirectivesContainer;
13
use Railt\SDL\Contracts\Definitions\Definition;
14
use Railt\SDL\Contracts\Definitions\UnionDefinition;
15
use Railt\SDL\Contracts\Type;
16
17
/**
18
 * Class BaseUnion
19
 */
20
abstract class BaseUnion extends BaseTypeDefinition implements UnionDefinition
21
{
22
    use BaseDirectivesContainer;
23
24
    /**
25
     * Type definition
26
     */
27
    protected const TYPE_NAME = Type::UNION;
28
29
    /**
30
     * @var array|Definition[]
31
     */
32
    protected $types = [];
33
34
    /**
35
     * @return iterable|Definition[]
36
     */
37
    public function getTypes(): iterable
38
    {
39
        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\Defi...ionDefinition::getTypes of type Railt\SDL\Contracts\Defi...efinitions\Definition[].

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...
40
    }
41
42
    /**
43
     * @param string $name
44
     * @return bool
45
     */
46 7
    public function hasType(string $name): bool
47
    {
48 7
        return \array_key_exists($name, $this->types);
49
    }
50
51
    /**
52
     * @param string $name
53
     * @return null|Definition
54
     */
55 6
    public function getType(string $name): ?Definition
56
    {
57 6
        return $this->types[$name] ?? null;
58
    }
59
60
    /**
61
     * @return int
62
     */
63 6
    public function getNumberOfTypes(): int
64
    {
65 6
        return \count($this->types);
66
    }
67
68
    /**
69
     * @return array
70
     */
71 6
    public function __sleep(): array
72
    {
73 6
        return \array_merge(parent::__sleep(), [
74 6
            'types',
75
        ]);
76
    }
77
}
78