1 | <?php |
||
27 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
28 | { |
||
29 | public $file = 'composer-asset-plugin.lock'; |
||
30 | |||
31 | /** |
||
32 | * @var Composer |
||
33 | */ |
||
34 | protected $composer; |
||
35 | |||
36 | /** |
||
37 | * @var IOInterface |
||
38 | */ |
||
39 | public $io; |
||
40 | |||
41 | /** |
||
42 | * List of package managers instances. |
||
43 | * Initialized at activate. |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $managers = ['bower', 'npm']; |
||
47 | |||
48 | protected $packages; |
||
49 | |||
50 | /** |
||
51 | * Initializes the plugin object with passed $composer and $io. |
||
52 | * Also initializes package managers. |
||
53 | * |
||
54 | * @param Composer $composer |
||
55 | * @param IOInterface $io |
||
56 | */ |
||
57 | public function activate(Composer $composer, IOInterface $io) |
||
67 | |||
68 | /** |
||
69 | * Returns list of events the plugin wants to listen. |
||
70 | * |
||
71 | * @return array list of events |
||
72 | */ |
||
73 | public static function getSubscribedEvents() |
||
84 | |||
85 | /** |
||
86 | * Perform install. Called by composer after install. |
||
87 | * @param Event $event |
||
88 | */ |
||
89 | public function onPostInstall(Event $event) |
||
99 | |||
100 | /** |
||
101 | * Perform update. Called by composer after update. |
||
102 | * @param Event $event |
||
103 | */ |
||
104 | public function onPostUpdate(Event $event) |
||
109 | |||
110 | public function setPackages(array $packages) |
||
114 | |||
115 | public function getPackages() |
||
123 | |||
124 | /** |
||
125 | * Scan packages from the composer object. |
||
126 | */ |
||
127 | protected function scanPackages() |
||
137 | |||
138 | /** |
||
139 | * Load packages from given lock file. |
||
140 | * @param JsonFile $lockFile |
||
141 | */ |
||
142 | protected function loadPackages(JsonFile $lockFile) |
||
149 | |||
150 | /** |
||
151 | * Install packages after loading/scanning. |
||
152 | * @param string $action |
||
153 | */ |
||
154 | protected function runAction($action) |
||
161 | |||
162 | /** |
||
163 | * Get path to vendor dir from composer. |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getVendorDir() |
||
170 | } |
||
171 |
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.