Completed
Push — master ( f60b13...3d0445 )
by Andrii
02:27
created

ManagerTrait::getItem()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 2
crap 5.2742
1
<?php
2
/**
3
 * Collection library for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-collection
6
 * @package   yii2-collection
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\collection;
12
13
use Yii;
14
15
/**
16
 * Manager Trait.
17
 * Like basic collection but instantiates items when getting.
18
 */
19
trait ManagerTrait
20
{
21
    use ObjectTrait;
22
23
    /**
24
     * @var string default class to create item objects
25
     */
26
    protected $_itemClass;
27
28
    public function setItemClass($class)
29
    {
30
        $this->_itemClass = $class;
31
    }
32
33 3
    public function getItemClass()
34
    {
35 3
        return $this->_itemClass ?: get_called_class();
36
    }
37
38 3
    public function getItemConfig($name = null, array $config = [])
39
    {
40 3
        if (!isset($config['class'])) {
41 3
            $config['class'] = $this->getItemClass($name, $config) ?: get_called_class();
0 ignored issues
show
Unused Code introduced by
The call to ManagerTrait::getItemClass() has too many arguments starting with $name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42 3
        }
43 3
        $class = new \ReflectionClass($config['class']);
44 3
        if ($class->implementsInterface('hiqdev\yii2\collection\ItemWithNameInterface')) {
45 2
            $config['name'] = $name;
46 2
        }
47 3
        if ($class->implementsInterface('hiqdev\yii2\collection\ItemWithCollectionInterface')) {
48 2
            $config['collection'] = $this;
49 2
        }
50
51 3
        return $config;
52
    }
53
54
    /**
55
     * Creates item instance from array configuration.
56
     * @param string $name   item name
57
     * @param array  $config item instance configuration
58
     * @return object instance
59
     */
60 3
    protected function createItem($name, array $config = [])
61
    {
62 3
        return Yii::createObject($this->getItemConfig($name, $config));
63
    }
64
65
    /**
66
     * Returns item by name. Instantiates it before.
67
     * @param string $name item name
68
     * @param mixed $default default value
69
     * @return object item instance
70
     */
71 13
    public function getItem($name, $default = null)
72
    {
73 13
        if (empty($this->_items[$name])) {
74
            $this->_items[$name] = $default;
75
        }
76 13
        $item = &$this->_items[$name];
77 13
        if (is_array($item) || is_null($item)) {
78 3
            $item = $this->createItem($name, $item ?: []);
79 3
        }
80
81 13
        return $item;
82
    }
83
84
    public function hasObject($name)
85
    {
86
        return is_object($this->_items[$name]);
87
    }
88
89
    /**
90
     * Get them all as array of items!
91
     * Instantiates all the items.
92
     * @return array list of items
93
     * @see get
94
     */
95
    public function getItems()
96
    {
97
        foreach ($this->_items as $name => $item) {
98
            $this->getItem($name);
99
        }
100
101
        return $this->_items;
102
    }
103
}
104