66
77use DI \Container ;
88use DI \ContainerBuilder ;
9+ use PhpSchool \PhpWorkshop \Check \CheckInterface ;
910use PhpSchool \PhpWorkshop \Check \CheckRepository ;
1011use PhpSchool \PhpWorkshop \Event \Event ;
1112use PhpSchool \PhpWorkshop \Event \EventDispatcher ;
@@ -185,6 +186,7 @@ public function configure(bool $debugMode = false): ContainerInterface
185186 }
186187 }
187188
189+ /** @var CheckRepository $checkRepository */
188190 $ checkRepository = $ container ->get (CheckRepository::class);
189191 foreach ($ this ->checks as $ check ) {
190192 if (false === $ container ->has ($ check )) {
@@ -193,10 +195,23 @@ public function configure(bool $debugMode = false): ContainerInterface
193195 );
194196 }
195197
196- $ checkRepository ->registerCheck ($ container ->get ($ check ));
198+ $ checkInstance = $ container ->get ($ check );
199+
200+ if (!$ checkInstance instanceof CheckInterface) {
201+ throw new RuntimeException (
202+ sprintf (
203+ 'Check: "%s" does not implement the required interface: "%s". ' ,
204+ $ check ,
205+ CheckInterface::class
206+ )
207+ );
208+ }
209+
210+ $ checkRepository ->registerCheck ($ checkInstance );
197211 }
198212
199213 if (!empty ($ this ->results )) {
214+ /** @var ResultRendererFactory $resultFactory */
200215 $ resultFactory = $ container ->get (ResultRendererFactory::class);
201216
202217 foreach ($ this ->results as $ result ) {
@@ -234,32 +249,35 @@ public function run(): int
234249 $ container = $ this ->configure ($ debug );
235250
236251 try {
237- $ exitCode = $ container ->get (CommandRouter::class)->route ($ args );
252+ /** @var CommandRouter $router */
253+ $ router = $ container ->get (CommandRouter::class);
254+ $ exitCode = $ router ->route ($ args );
238255 } catch (MissingArgumentException $ e ) {
239- $ container
240- ->get (OutputInterface::class)
241- ->printError (
242- sprintf (
243- 'Argument%s: "%s" %s missing! ' ,
244- count ($ e ->getMissingArguments ()) > 1 ? 's ' : '' ,
245- implode ('", " ' , $ e ->getMissingArguments ()),
246- count ($ e ->getMissingArguments ()) > 1 ? 'are ' : 'is '
247- )
248- );
256+ /** @var OutputInterface $output */
257+ $ output = $ container ->get (OutputInterface::class);
258+ $ output ->printError (
259+ sprintf (
260+ 'Argument%s: "%s" %s missing! ' ,
261+ count ($ e ->getMissingArguments ()) > 1 ? 's ' : '' ,
262+ implode ('", " ' , $ e ->getMissingArguments ()),
263+ count ($ e ->getMissingArguments ()) > 1 ? 'are ' : 'is '
264+ )
265+ );
249266 return 1 ;
250267 } catch (\Throwable $ e ) {
251268 $ message = $ e ->getMessage ();
252- $ basePath = canonicalise_path ($ container ->get ('basePath ' ));
269+ $ basePath = canonicalise_path (is_string ( $ path = $ container ->get ('basePath ' )) ? $ path : '' );
253270
254271 if (strpos ($ message , $ basePath ) !== null ) {
255272 $ message = str_replace ($ basePath , '' , $ message );
256273 }
257274
258275 $ this ->tearDown ($ container );
259276
260- $ container
261- ->get (OutputInterface::class)
262- ->printException ($ e );
277+ /** @var OutputInterface $output */
278+ $ output = $ container ->get (OutputInterface::class);
279+ $ output ->printException ($ e );
280+
263281 return 1 ;
264282 }
265283
@@ -307,11 +325,13 @@ private function getContainer(bool $debugMode): Container
307325 private function tearDown (ContainerInterface $ container ): void
308326 {
309327 try {
310- $ container
311- ->get (EventDispatcher::class)
312- ->dispatch (new Event ('application.tear-down ' ));
328+ /** @var EventDispatcher $eventDispatcher */
329+ $ eventDispatcher = $ container ->get (EventDispatcher::class);
330+ $ eventDispatcher ->dispatch (new Event ('application.tear-down ' ));
313331 } catch (\Throwable $ t ) {
314- $ container ->get (LoggerInterface::class)->error ($ t ->getMessage (), ['exception ' => $ t ]);
332+ /** @var LoggerInterface $logger */
333+ $ logger = $ container ->get (LoggerInterface::class);
334+ $ logger ->error ($ t ->getMessage (), ['exception ' => $ t ]);
315335 }
316336 }
317337}
0 commit comments