Completed
Push — master ( 60435d...ef0089 )
by Richard van
08:28 queued 06:58
created

Item::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Application\Item
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2018 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 © 2018 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\Application\Item;
38
39
use Jkphl\Micrometa\Application\Factory\PropertyListFactoryInterface;
40
41
/**
42
 * Item
43
 *
44
 * @package    Jkphl\Micrometa
45
 * @subpackage Jkphl\Micrometa\Application
46
 * @method PropertyListInterface getProperties() Item properties list
47
 */
48
class Item extends \Jkphl\Micrometa\Domain\Item\Item implements ItemInterface
49
{
50
    /**
51
     * Parser format
52
     *
53
     * @var int
54
     */
55
    protected $format;
56
    /**
57
     * Item value
58
     *
59
     * @var string
60
     */
61
    protected $value;
62
    /**
63
     * Nested Items
64
     *
65
     * @var ItemInterface[]
66
     */
67
    protected $children;
68
69
    /**
70
     * Item constructor
71
     *
72
     * @param int $format                                       Parser format
73
     * @param PropertyListFactoryInterface $propertyListFactory Property list factory
74
     * @param string|\stdClass|\stdClass[] $type                Item type(s)
75
     * @param \stdClass[] $properties                           Item properties
76
     * @param ItemInterface[] $children                         Nested items
77
     * @param string|null $itemId                               Item id
78
     * @param string|null $itemLanguage                         Item language
79
     * @param string|null $value                                Item value
80
     */
81 32
    public function __construct(
82
        $format,
83
        PropertyListFactoryInterface $propertyListFactory,
84
        $type,
85
        array $properties = [],
86
        array $children = [],
87
        $itemId = null,
88
        $itemLanguage = null,
89
        $value = null
90
    ) {
91 32
        $this->format = $format;
92 32
        parent::__construct($type, $properties, $itemId, $itemLanguage, $propertyListFactory);
93 32
        $this->children = $children;
94 32
        $this->value    = $value;
95 32
    }
96
97
    /**
98
     * Export the object
99
     *
100
     * @return mixed
101
     */
102 3
    public function export()
103
    {
104
        return (object)[
105 3
            'format'     => $this->getFormat(),
106 3
            'id'         => $this->getId(),
107 3
            'language'   => $this->getLanguage(),
108 3
            'value'      => $this->getValue(),
109 3
            'types'      => array_map(
110
                function ($type) {
111 3
                    return $type->profile.$type->name;
112 3
                },
113 3
                $this->getType()
114
            ),
115 3
            'properties' => $this->getProperties()->export(),
116 3
            'items'      => array_map(
117
                function (ItemInterface $item) {
118 3
                    return $item->export();
119 3
                },
120 3
                $this->getChildren()
121
            )
122
        ];
123
    }
124
125
    /**
126
     * Return the parser format
127
     *
128
     * @return int Parser format
129
     */
130 15
    public function getFormat()
131
    {
132 15
        return $this->format;
133
    }
134
135
    /**
136
     * Return the item value
137
     *
138
     * @return string Item value
139
     */
140 5
    public function getValue()
141
    {
142 5
        return $this->value;
143
    }
144
145
    /**
146
     * Return the nested children
147
     *
148
     * @return ItemInterface[] Nested children
149
     */
150 19
    public function getChildren()
151
    {
152 19
        return $this->children;
153
    }
154
}
155