Completed
Push — master ( 7f2c67...65ee7b )
by Joschi
03:47
created

PropertyList::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * rdfa-lite
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain\Property
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\RdfaLiteMicrodata\Domain\Property;
38
39
use Jkphl\RdfaLiteMicrodata\Domain\Exceptions\ErrorException;
40
use Jkphl\RdfaLiteMicrodata\Domain\Exceptions\OutOfBoundsException;
41
use Jkphl\RdfaLiteMicrodata\Domain\Iri\IriInterface;
42
43
/**
44
 * Property list
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
48
 */
49
class PropertyList implements PropertyListInterface
50
{
51
    /**
52
     * Property values
53
     *
54
     * @var array[]
55
     */
56
    protected $values = [];
57
    /**
58
     * Property names
59
     *
60
     * @var IriInterface[]
61
     */
62
    protected $names = [];
63
    /**
64
     * Name cursor mapping
65
     *
66
     * @var int[]
67
     */
68
    protected $nameToCursor = [];
69
    /**
70
     * Internal cursor
71
     *
72
     * @var int
73
     */
74
    protected $cursor = 0;
75
76
    /**
77
     * Return the current property values
78
     *
79
     * @return array Property values
80
     */
81 8
    public function current()
82
    {
83 8
        return $this->values[$this->cursor];
84
    }
85
86
    /**
87
     * Move forward to next element
88
     */
89 8
    public function next()
90
    {
91 8
        ++$this->cursor;
92 8
    }
93
94
    /**
95
     * Return the current IRI key
96
     *
97
     * @return IriInterface IRI key
98
     */
99 1
    public function key()
100
    {
101 1
        return $this->names[$this->cursor];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->names[$this->cursor]; (Jkphl\RdfaLiteMicrodata\Domain\Iri\IriInterface) is incompatible with the return type declared by the interface Iterator::key of type integer|double|string|boolean.

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...
102
    }
103
104
    /**
105
     * Checks if current position is valid
106
     *
107
     * @return boolean The current position is valid
108
     */
109 8
    public function valid()
110
    {
111 8
        return isset($this->values[$this->cursor]);
112
    }
113
114
    /**
115
     * Rewind the Iterator to the first element
116
     */
117 8
    public function rewind()
118
    {
119 8
        $this->cursor = 0;
120 8
    }
121
122
    /**
123
     * Unset a property
124
     *
125
     * @param IriInterface|string $iri IRI
126
     * @throws ErrorException
127
     */
128 1
    public function offsetUnset($iri)
129
    {
130 1
        throw new ErrorException(
131 1
            sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri),
132 1
            ErrorException::CANNOT_UNSET_PROPERTY,
133 1
            E_WARNING
134
        );
135
    }
136
137
    /**
138
     * Return the number of properties
139
     *
140
     * @return int Number of properties
141
     */
142 5
    public function count()
143
    {
144 5
        return count($this->nameToCursor);
145
    }
146
147
    /**
148
     * Return an array form
149
     *
150
     * @return array Array form
151
     */
152 1
    public function toArray()
153
    {
154 1
        $values = $this->values;
155 1
        return array_map(
156 1
            function ($cursor) use ($values) {
157 1
                return $values[$cursor];
158 1
            },
159 1
            $this->nameToCursor
160
        );
161
    }
162
163
    /**
164
     * Add a property
165
     *
166
     * @param PropertyInterface $property Property
167
     */
168 11
    public function add(PropertyInterface $property)
169
    {
170 11
        $iri = $property->toIri();
171
172
        // Create the property values list if necessary
173 11
        if (!$this->offsetExists($iri)) {
174 11
            $this->offsetSet($iri, [$property]);
175 11
            return;
176
        }
177
178 2
        $this->offsetGet($iri)[] = $property;
179 2
    }
180
181
    /**
182
     * Return whether a property exists
183
     *
184
     * @param IriInterface|string $iri IRI
185
     * @return boolean Property exists
186
     */
187 11
    public function offsetExists($iri)
188
    {
189 11
        return array_key_exists(strval($iri), $this->nameToCursor);
190
    }
191
192
    /**
193
     * Set a particular property
194
     *
195
     * @param IriInterface|string $iri IRI
196
     * @param array $value Property values
197
     */
198 11
    public function offsetSet($iri, $value)
199
    {
200 11
        $iriStr = strval($iri);
201 11
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ?
202 11
            $this->nameToCursor[$iriStr] : ($this->nameToCursor[$iriStr] = count($this->nameToCursor));
203 11
        $this->names[$cursor] = $iri;
204 11
        $this->values[$cursor] = $value;
205 11
    }
206
207
    /**
208
     * Get a particular property
209
     *
210
     * @param IriInterface|string $iri IRI
211
     * @return array Property values
212
     */
213 5
    public function &offsetGet($iri)
214
    {
215 5
        $iriStr = strval($iri);
216
217
        // If the property name is unknown
218 5
        if (!isset($this->nameToCursor[$iriStr])) {
219 1
            throw new OutOfBoundsException(
220 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $iriStr),
221 1
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
222
            );
223
        }
224
225 4
        $cursor = $this->nameToCursor[strval($iri)];
226 4
        return $this->values[$cursor];
227
    }
228
}
229