1 | <?php |
||
15 | class CatalogElementService extends BaseService |
||
16 | { |
||
17 | /** |
||
18 | * @var CatalogElement[] |
||
19 | */ |
||
20 | protected $entities = []; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $listPage = 1; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $listQuery; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $listParams = []; |
||
36 | |||
37 | /** |
||
38 | * @param EntityInterface|CatalogElement $catalogElement |
||
39 | */ |
||
40 | 11 | public function add(EntityInterface $catalogElement) |
|
41 | { |
||
42 | 11 | if ($catalogElement instanceof CatalogElement) { |
|
43 | 11 | $this->entities[] = $catalogElement; |
|
44 | } |
||
45 | 11 | } |
|
46 | |||
47 | /** |
||
48 | * @param int $page |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | 2 | public function setPage($page) |
|
58 | |||
59 | /** |
||
60 | * @param string $query |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | 2 | public function setQuery($query) |
|
70 | |||
71 | /** |
||
72 | * @param array $params |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | 1 | public function setParams(array $params) |
|
82 | |||
83 | /** |
||
84 | * @param $link |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 7 | protected function composeListLink($link) |
|
102 | |||
103 | /** |
||
104 | * @deprecated |
||
105 | * |
||
106 | * @param int $page |
||
107 | * @param string|null $query |
||
108 | * @param array $params |
||
109 | * |
||
110 | * @return array|bool |
||
111 | */ |
||
112 | 4 | public function lists($page = 1, $query = null, array $params = []) |
|
120 | |||
121 | /** |
||
122 | * @param $array |
||
123 | * |
||
124 | * @return CatalogElement |
||
125 | */ |
||
126 | 11 | public function parseArrayToEntity($array) |
|
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | 11 | protected function getLink() |
|
157 | |||
158 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.