Completed
Push — v2 ( 5e546d...a2e732 )
by Joschi
04:26
created

ItemObjectModel::rel()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 2
dl 0
loc 21
ccs 0
cts 12
cp 0
crap 30
rs 8.7624
c 0
b 0
f 0
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
use Jkphl\Micrometa\Application\Item\ItemInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Jkphl\Micrometa\Ports\Item\ItemInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
40
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException;
41
use Jkphl\Micrometa\Ports\Rel\AlternateInterface;
42
use Jkphl\Micrometa\Ports\Rel\RelInterface;
43
44
/**
45
 * Item object model
46
 *
47
 * @package Jkphl\Micrometa
48
 * @subpackage Jkphl\Micrometa\Ports
49
 */
50
class ItemObjectModel extends ItemList implements ItemObjectModelInterface
51
{
52
    /**
53
     * Rel declarations
54
     *
55
     * @var RelInterface[]
56
     */
57
    protected $rels;
58
59
    /**
60
     * Alternate resources
61
     *
62
     * @var AlternateInterface[]
63
     */
64
    protected $alternates;
65
66
    /**
67
     * ItemList constructor
68
     *
69
     * @param ItemInterface[] $items Items
70
     * @param RelInterface[] $rels Rel declarations
71
     * @param AlternateInterface[] $alternates Alternate resources
72
     */
73 1
    public function __construct(array $items = [], array $rels = [], array $alternates = [])
74
    {
75 1
        parent::__construct($items);
0 ignored issues
show
Documentation introduced by
$items is of type array<integer,object<Jkp...on\Item\ItemInterface>>, but the function expects a array<integer,object<Jkp...ts\Item\ItemInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76 1
        $this->rels = $rels;
77 1
        $this->alternates = $alternates;
78 1
    }
79
80
    /**
81
     * Return all rel declarations of a particular type
82
     *
83
     * @param string $type Rel type
84
     * @param int|null $index Optional: particular index
85
     * @return RelInterface|RelInterface[] Single rel=* declaration or list of particular rel declarations
86
     * @throws OutOfBoundsException If the rel type is out of bounds
87
     * @throws OutOfBoundsException If the rel index is out of bounds
88
     * @api
89
     */
90
    public function rel($type, $index = null)
91
    {
92
        if ($index === null) {
93
            return $this->rels();
94
        }
95
        // If the rel type is out of bounds
96
        if (!array_key_exists($type, $this->rels)) {
97
            throw new OutOfBoundsException(
98
                sprintf(OutOfBoundsException::INVALID_REL_TYPE_STR, $type),
99
                OutOfBoundsException::INVALID_REL_TYPE
100
            );
101
        }
102
        // If the rel index is out of bounds
103
        if (!is_int($index) || !array_key_exists($index, $this->rels[$type])) {
104
            throw new OutOfBoundsException(
105
                sprintf(OutOfBoundsException::INVALID_REL_INDEX_STR, $index, $type),
106
                OutOfBoundsException::INVALID_REL_INDEX
107
            );
108
        }
109
        return $this->rels[$type][$index];
110
    }
111
112
    /**
113
     * Return all rel=* declaration groups
114
     *
115
     * @return RelInterface[] Rel=* declaration groups
116
     * @api
117
     */
118
    public function rels()
119
    {
120
        return $this->rels;
121
    }
122
123
    /**
124
     * Return all alternate resources
125
     *
126
     * @return AlternateInterface[] Alternate resources
127
     * @api
128
     */
129
    public function alternates()
130
    {
131
        return $this->alternates;
132
    }
133
134
    /**
135
     * Return the alternate resource of a particular type
136
     *
137
     * @param string $type Alternate representation type
138
     * @return AlternateInterface|null Alternate resource
139
     * @throws OutOfBoundsException If the alternate type is out of bounds
140
     * @api
141
     */
142
    public function alternate($type)
143
    {
144
        // If the alternate type is out of bounds
145
        if (!array_key_exists($type, $this->alternates)) {
146
            throw new OutOfBoundsException(
147
                sprintf(OutOfBoundsException::INVALID_ALTERNATE_TYPE_STR, $type),
148
                OutOfBoundsException::INVALID_ALTERNATE_TYPE
149
            );
150
        }
151
        return $this->alternates[$type];
152
    }
153
}
154