1 | <?php |
||
12 | class MongoFixturesLoader |
||
13 | { |
||
14 | /** @var array|MongoFixtureInterface[] */ |
||
15 | private $loadedClasses; |
||
16 | |||
17 | /** @var ContainerInterface */ |
||
18 | private $container; |
||
19 | |||
20 | /** |
||
21 | * MongoFixturesLoader constructor. |
||
22 | * |
||
23 | * @param ContainerInterface $container |
||
24 | */ |
||
25 | public function __construct(ContainerInterface $container) |
||
29 | |||
30 | /** |
||
31 | * @param string $dir |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | public function loadFromDirectory(string $dir) |
||
47 | |||
48 | /** |
||
49 | * @param string $fileName |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | public function loadFromFile(string $fileName) |
||
62 | |||
63 | /** |
||
64 | * @param \Iterator $iterator |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | private function loadFromIterator(\Iterator $iterator) |
||
93 | |||
94 | /** |
||
95 | * @param MongoFixtureInterface $list |
||
96 | */ |
||
97 | public function addInstance(MongoFixtureInterface $list) |
||
105 | |||
106 | /** |
||
107 | * @return array|MongoFixtureInterface[] |
||
108 | */ |
||
109 | public function getLoadedClasses() |
||
113 | |||
114 | /** |
||
115 | * @param string $className |
||
116 | * |
||
117 | * @return MongoFixtureInterface |
||
118 | */ |
||
119 | private function buildFixture($className): MongoFixtureInterface |
||
132 | } |
||
133 |
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.