Completed
Push — master ( 22fd50...798002 )
by Neomerx
03:53
created

ContextStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 96
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStates() 0 4 1
A getProperties() 0 4 1
A getCurrentBlockId() 0 4 1
A setCurrentBlockId() 0 6 1
A clear() 0 7 1
A getProperty() 0 4 1
A getState() 0 4 1
A setState() 0 6 1
1
<?php namespace Limoncello\Validation\Execution;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\Execution\BlockPropertiesInterface;
20
use Limoncello\Validation\Contracts\Execution\BlockStateInterface;
21
use Limoncello\Validation\Contracts\Execution\ContextStorageInterface;
22
23
/**
24
 * @package Limoncello\Validation
25
 */
26
class ContextStorage implements ContextStorageInterface, BlockStateInterface, BlockPropertiesInterface
27
{
28
    /**
29
     * @var array
30
     */
31
    private $states = [];
32
33
    /**
34
     * @var int
35
     */
36
    private $currentBlockId = 0;
37
38
    /**
39
     * @var
40
     */
41
    private $blocks;
42
43
    /**
44
     * @param $blocks
45
     */
46
    public function __construct(array $blocks)
47
    {
48
        $this->blocks = $blocks;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function getStates(): BlockStateInterface
55
    {
56
        return $this;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getProperties(): BlockPropertiesInterface
63
    {
64
        return $this;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function getCurrentBlockId(): int
71
    {
72
        return $this->currentBlockId;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function setCurrentBlockId(int $index): ContextStorageInterface
79
    {
80
        $this->currentBlockId = $index;
81
82
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Limoncello\Validation\Execution\ContextStorage) is incompatible with the return type declared by the interface Limoncello\Validation\Co...face::setCurrentBlockId of type self.

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...
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function clear(): ContextStorageInterface
89
    {
90
        $this->states = [];
91
        $this->setCurrentBlockId(0);
92
93
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Limoncello\Validation\Execution\ContextStorage) is incompatible with the return type declared by the interface Limoncello\Validation\Co...StorageInterface::clear of type self.

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...
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function getProperty(int $key, $default = null)
100
    {
101
        return $this->blocks[$this->getCurrentBlockId()][BlockSerializer::PROPERTIES][$key] ?? $default;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function getState(int $key, $default = null)
108
    {
109
        return $this->states[$this->getCurrentBlockId()][$key] ?? $default;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function setState(int $key, $value): BlockStateInterface
116
    {
117
        $this->states[$this->getCurrentBlockId()][$key] = $value;
118
119
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Limoncello\Validation\Execution\ContextStorage) is incompatible with the return type declared by the interface Limoncello\Validation\Co...tateInterface::setState of type self.

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...
120
    }
121
}
122