Completed
Branch master (6c7c3c)
by Joschi
02:33
created

Item::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 13
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 © 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\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 31
    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 31
        $this->format = $format;
92 31
        parent::__construct($type, $properties, $itemId, $itemLanguage, $propertyListFactory);
93 31
        $this->children = $children;
94 31
        $this->value = $value;
95 31
    }
96
97
    /**
98
     * Return the item value
99
     *
100
     * @return string Item value
101
     */
102 5
    public function getValue()
103
    {
104 5
        return $this->value;
105
    }
106
107
    /**
108
     * Export the object
109
     *
110
     * @return mixed
111
     */
112 3
    public function export()
113
    {
114
        return (object)[
115 3
            'format' => $this->getFormat(),
116 3
            'id' => $this->getId(),
117 3
            'language' => $this->getLanguage(),
118 3
            'value' => $this->getValue(),
119 3
            'types' => array_map(
120
                function ($type) {
121 3
                    return $type->profile.$type->name;
122 3
                },
123 3
                $this->getType()
124
            ),
125 3
            'properties' => $this->getProperties()->export(),
126 3
            'items' => array_map(
127 3
                function (ItemInterface $item) {
128 3
                    return $item->export();
129 3
                },
130 3
                $this->getChildren()
131
            )
132
        ];
133
    }
134
135
    /**
136
     * Return the parser format
137
     *
138
     * @return int Parser format
139
     */
140 15
    public function getFormat()
141
    {
142 15
        return $this->format;
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