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

BaseObject   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 66
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInterfaces() 0 4 1
A hasInterface() 0 4 1
A getInterface() 0 4 1
A getNumberOfInterfaces() 0 4 1
A __sleep() 0 13 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\Dependent\Field\BaseFieldsContainer;
13
use Railt\SDL\Base\Invocations\Directive\BaseDirectivesContainer;
14
use Railt\SDL\Contracts\Definitions\InterfaceDefinition;
15
use Railt\SDL\Contracts\Definitions\ObjectDefinition;
16
use Railt\SDL\Contracts\Type;
17
18
/**
19
 * Class BaseObject
20
 */
21
abstract class BaseObject extends BaseTypeDefinition implements ObjectDefinition
22
{
23
    use BaseDirectivesContainer;
24
    use BaseFieldsContainer;
25
26
    /**
27
     * Object type name
28
     */
29
    protected const TYPE_NAME = Type::OBJECT;
30
31
    /**
32
     * @var array|InterfaceDefinition[]
33
     */
34
    protected $interfaces = [];
35
36
    /**
37
     * @return iterable|InterfaceDefinition[]
38
     */
39 2822
    public function getInterfaces(): iterable
40
    {
41 2822
        return \array_values($this->interfaces);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \array_values($this->interfaces); (array) is incompatible with the return type declared by the interface Railt\SDL\Contracts\Defi...finition::getInterfaces of type Railt\SDL\Contracts\Defi...s\InterfaceDefinition[].

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...
42
    }
43
44
    /**
45
     * @param string $name
46
     * @return bool
47
     */
48 26
    public function hasInterface(string $name): bool
49
    {
50 26
        return \array_key_exists($name, $this->interfaces);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return null|InterfaceDefinition
56
     */
57 30
    public function getInterface(string $name): ?InterfaceDefinition
58
    {
59 30
        return $this->interfaces[$name] ?? null;
60
    }
61
62
    /**
63
     * @return int
64
     */
65 24
    public function getNumberOfInterfaces(): int
66
    {
67 24
        return \count($this->interfaces);
68
    }
69
70
    /**
71
     * @return array
72
     */
73 739
    public function __sleep(): array
74
    {
75 739
        return \array_merge(parent::__sleep(), [
76
            // self class
77 739
            'interfaces',
78
79
            // trait HasFields
80
            'fields',
81
82
            // trait HasDirectives
83
            'directives',
84
        ]);
85
    }
86
}
87