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

BaseEnum   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 63
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isCompatible() 0 4 2
A getValues() 0 4 1
A hasValue() 0 4 1
A getValue() 0 4 1
A __sleep() 0 10 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\Enum\ValueDefinition;
14
use Railt\SDL\Contracts\Definitions\EnumDefinition;
15
use Railt\SDL\Contracts\Type;
16
17
/**
18
 * Class BaseEnum
19
 */
20
abstract class BaseEnum extends BaseTypeDefinition implements EnumDefinition
21
{
22
    use BaseDirectivesContainer;
23
24
    /**
25
     * Enum type name
26
     */
27
    protected const TYPE_NAME = Type::ENUM;
28
29
    /**
30
     * @var array|ValueDefinition[]
31
     */
32
    protected $values = [];
33
34
    /**
35
     * @param mixed|string $value
36
     * @return bool
37
     */
38 168
    public function isCompatible($value): bool
39
    {
40 168
        return \is_string($value) && $this->hasValue($value);
41
    }
42
43
    /**
44
     * @return iterable|ValueDefinition[]
45
     */
46 5438
    public function getValues(): iterable
47
    {
48 5438
        return \array_values($this->values);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \array_values($this->values); (array) is incompatible with the return type declared by the interface Railt\SDL\Contracts\Defi...umDefinition::getValues of type Railt\SDL\Contracts\Defi...\Enum\ValueDefinition[].

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...
49
    }
50
51
    /**
52
     * @param string $name
53
     * @return bool
54
     */
55 126
    public function hasValue(string $name): bool
56
    {
57 126
        return \array_key_exists($name, $this->values);
58
    }
59
60
    /**
61
     * @param string $name
62
     * @return null|ValueDefinition
63
     */
64 6
    public function getValue(string $name): ?ValueDefinition
65
    {
66 6
        return $this->values[$name] ?? null;
67
    }
68
69
    /**
70
     * @return array
71
     */
72 1004
    public function __sleep(): array
73
    {
74 1004
        return \array_merge(parent::__sleep(), [
75
            // self class
76 1004
            'values',
77
78
            // trait HasDirectives
79
            'directives',
80
        ]);
81
    }
82
}
83