Completed
Push — v2 ( 58f91a...3b3087 )
by Joschi
05:06
created

Item::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Ports
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
use Jkphl\Micrometa\Application\Item\ItemInterface as ApplicationItemInterface;
40
use Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException as DomainOutOfBoundsException;
41
use Jkphl\Micrometa\Infrastructure\Factory\ItemFactory;
42
use Jkphl\Micrometa\Infrastructure\Factory\ProfiledNamesFactory;
43
use Jkphl\Micrometa\Infrastructure\Parser\ProfiledNamesList;
44
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException;
45
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException;
46
47
/**
48
 * Micro information item
49
 *
50
 * @package Jkphl\Micrometa
51
 * @subpackage Jkphl\Micrometa\Ports
52
 */
53
class Item extends AbstractItemList implements ItemInterface
54
{
55
    /**
56
     * Application item
57
     *
58
     * @var ApplicationItemInterface
59
     */
60
    protected $item;
61
62
    /**
63
     * Item constructor
64
     *
65
     * @param ApplicationItemInterface $item Application item
66
     */
67 6
    public function __construct(ApplicationItemInterface $item)
68
    {
69 6
        $this->item = $item;
70 6
        parent::__construct(ItemFactory::createFromParserResult($this->item->getChildren()));
71 6
    }
72
73
    /**
74
     * Get the first value of an item property
75
     *
76
     * @param string $name Item property name
77
     * @return string First value of an item property
78
     * @api
79
     */
80 1
    public function __get($name)
81
    {
82 1
        return $this->getProperty($name, null, 0);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getProperty($name, null, 0); (array|string|Jkphl\Micro...orts\Item\ItemInterface) is incompatible with the return type declared by the interface Jkphl\Micrometa\Ports\Item\ItemInterface::__get of type string.

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
     * Get a single property (value)
87
     *
88
     * @param string $name Property name
89
     * @param string $profile Property profile
90
     * @param int $index Property value index
91
     * @return array|string|ItemInterface Property value(s)
92
     * @throws OutOfBoundsException If the property name is unknown
93
     * @throws OutOfBoundsException If the property value index is out of bounds
94
     * @api
95
     */
96 4
    public function getProperty($name, $profile = null, $index = null)
97
    {
98
        try {
99 4
            $propertyValues = $this->item->getProperty($name, $profile);
100 4
        } catch (DomainOutOfBoundsException $e) {
101 4
            throw new OutOfBoundsException($e->getMessage(), $e->getCode());
102
        }
103
104
        // If all property values should be returned
105 4
        if ($index === null) {
106 4
            return $propertyValues;
107
        }
108
109
        // If the property value index is out of bounds
110 3
        if (!isset($propertyValues[$index])) {
111
            throw new OutOfBoundsException(
112
                sprintf(OutOfBoundsException::INVALID_PROPERTY_VALUE_INDEX_STR, $index),
113
                OutOfBoundsException::INVALID_PROPERTY_VALUE_INDEX
114
            );
115
        }
116
117 3
        return $propertyValues[$index];
118
    }
119
120
    /**
121
     * Return whether the item is of a particular type (or contained in a list of types)
122
     *
123
     * The item type(s) can be specified in a variety of ways, @see ProfiledNamesFactory::createFromArguments()
124
     *
125
     * @param string $name Name
126
     * @param string|null $profile Profile
127
     * @return boolean Item type is contained in the list of types
128
     * @api
129
     */
130 1
    public function isOfType($name, $profile = null)
131
    {
132
        /** @var ProfiledNamesList $types */
133 1
        $types = ProfiledNamesFactory::createFromArguments(func_get_args());
134
135
        // Run through all item types
136
        /** @var \stdClass $itemType */
137 1
        foreach ($this->item->getType() as $itemType) {
138
            // Run through all query types
139
            /** @var \stdClass $queryType */
140 1
            foreach ($types as $queryType) {
141 1
                if (($queryType->name == $itemType->name) &&
142 1
                    (($queryType->profile === null) ? true : ($queryType->profile == $itemType->profile))
143
                ) {
144 1
                    return true;
145
                }
146
            }
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * Get all values of the first available property in a stack
154
     *
155
     * The property stack can be specified in a variety of ways, @see ProfiledNamesFactory::createFromArguments()
156
     *
157
     * @param string $name Name
158
     * @param string $profile Profile
159
     * @return array Property values
160
     * @throws InvalidArgumentException If no property name was given
161
     * @throws OutOfBoundsException If none of the requested properties is known
162
     * @api
163
     */
164 1
    public function getFirstProperty($name, $profile = null)
165
    {
166
        /** @var ProfiledNamesList $properties */
167 1
        $properties = ProfiledNamesFactory::createFromArguments(func_get_args());
168
169
        // Prepare a default exception
170 1
        $e = new OutOfBoundsException(
171 1
            OutOfBoundsException::NO_MATCHING_PROPERTIES_STR,
172 1
            OutOfBoundsException::NO_MATCHING_PROPERTIES
173
        );
174
175
        // Run through all properties
176 1
        foreach ($properties as $property) {
177
            try {
178 1
                return $this->getProperty($property->name, $property->profile);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPropert...e, $property->profile); (array|string|Jkphl\Micro...orts\Item\ItemInterface) is incompatible with the return type declared by the interface Jkphl\Micrometa\Ports\It...rface::getFirstProperty of type array.

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...
179 1
            } catch (OutOfBoundsException $e) {
180 1
                continue;
181
            }
182
        }
183
184 1
        throw $e;
185
    }
186
187
    /**
188
     * Return all properties
189
     *
190
     * @return array[] Properties
191
     * @api
192
     */
193
    public function getProperties()
194
    {
195
        return $this->item->getProperties()->export();
196
    }
197
198
    /**
199
     * Return an object representation of the item
200
     *
201
     * @return \stdClass Micro information item
202
     * @api
203
     */
204 1
    public function toObject()
205
    {
206 1
        return $this->item->export();
207
    }
208
}
209