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