Completed
Push — v2 ( 3135e6...5d3e3a )
by Joschi
04:42 queued 10s
created

ItemList   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
dl 0
loc 122
rs 10
c 0
b 0
f 0
ccs 4
cts 24
cp 0.1666
wmc 10
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A toObject() 0 4 1
A toJson() 0 4 1
A items() 0 4 1
A item() 0 4 1
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Ports\Item
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\Micrometa\Ports\Item;
38
39
/**
40
 * Item list
41
 *
42
 * @package Jkphl\Micrometa
43
 * @subpackage Jkphl\Micrometa\Ports
44
 */
45
class ItemList implements ItemListInterface
46
{
47
    /**
48
     * Items
49
     *
50
     * @var ItemInterface[]
51
     */
52
    protected $items;
53
    /**
54
     * Internal pointer
55
     *
56
     * @var null
57
     */
58
    protected $pointer = null;
59
60
    /**
61
     * ItemList constructor
62
     *
63
     * @param ItemInterface[] $items Items
64
     */
65 1
    public function __construct(array $items = [])
66
    {
67 1
        $this->items = $items;
68 1
        $this->pointer = 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 of type integer is incompatible with the declared type null of property $pointer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69 1
    }
70
71
    /**
72
     * Return the current item
73
     *
74
     * @return ItemInterface Item
75
     */
76
    public function current()
77
    {
78
        return $this->items[$this->pointer];
79
    }
80
81
    /**
82
     * Move forward to next element
83
     *
84
     * @return void
85
     */
86
    public function next()
87
    {
88
        ++$this->pointer;
89
    }
90
91
    /**
92
     * Return the position of the current element
93
     *
94
     * @return mixed Position of the current element
95
     */
96
    public function key()
97
    {
98
        return $this->pointer;
99
    }
100
101
    /**
102
     * Checks if current position is valid
103
     *
104
     * @return boolean The current position is valid
105
     */
106
    public function valid()
107
    {
108
        return isset($this->items[$this->pointer]);
109
    }
110
111
    /**
112
     * Rewind the item list to the first element
113
     *
114
     * @return void
115
     */
116
    public function rewind()
117
    {
118
        $this->pointer = 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 of type integer is incompatible with the declared type null of property $pointer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
    }
120
121
    /**
122
     * Return an object representation of the item list
123
     *
124
     * @return \stdClass Micro information items
125
     * @api
126
     */
127
    public function toObject()
128
    {
129
        return new \stdClass();
130
    }
131
132
    /**
133
     * Return a JSON representation of the item list
134
     *
135
     * @return string Micro information items
136
     * @api
137
     */
138
    public function toJson()
139
    {
140
        return json_encode(new \stdClass());
141
    }
142
143
    /**
144
     * Return all items, optionally of particular types
145
     *
146
     * @param array ...$types Item types
147
     * @return ItemListInterface Items matching the requested types
148
     * @api
149
     */
150
    public function items(...$types)
151
    {
152
        return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface Jkphl\Micrometa\Ports\It...temListInterface::items of type Jkphl\Micrometa\Ports\Item\ItemListInterface.

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...
153
    }
154
155
    /**
156
     * Return the first item, optionally of particular types
157
     *
158
     * @param array ...$types Item types
159
     * @return ItemInterface Item
160
     * @api
161
     */
162
    public function item(...$types)
163
    {
164
        return new Item();
165
    }
166
}
167