Conditions | 7 |
Paths | 2 |
Total Lines | 80 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | public function hydrate(Jarvis $app) |
||
30 | { |
||
31 | // settings process |
||
32 | // ================ |
||
33 | |||
34 | $app['debug'] = $app['settings']['debug'] ?? Jarvis::DEFAULT_DEBUG; |
||
35 | |||
36 | $extra = $app['settings']['extra'] ?? []; |
||
37 | foreach ((array) $extra as $key => $data) { |
||
38 | $app["{$key}.settings"] = $data; |
||
39 | } |
||
40 | |||
41 | unset($app['settings']); |
||
42 | |||
43 | // services declarations |
||
44 | // ===================== |
||
45 | |||
46 | $app['app'] = function () use ($app): Jarvis { |
||
47 | return $app; |
||
48 | }; |
||
49 | |||
50 | $app['request'] = function (Jarvis $app): Request { |
||
51 | $request = Request::createFromGlobals(); |
||
52 | |||
53 | $session = $request->getSession(); |
||
54 | if (null === $session) { |
||
55 | $settings = $app['session.settings'] ?? []; |
||
56 | $storageClassname = $settings['session.storage.classname'] ?? NativeSessionStorage::class; |
||
57 | unset($settings['session.storage.classname']); |
||
58 | $session = new Session(new $storageClassname($settings)); |
||
59 | } |
||
60 | |||
61 | $request->setSession($session); |
||
62 | |||
63 | return $request; |
||
64 | }; |
||
65 | |||
66 | $app['session'] = function (Jarvis $app): Session { |
||
67 | return $app['request']->getSession(); |
||
68 | }; |
||
69 | |||
70 | $app['router'] = function (): Router { |
||
71 | return new Router(); |
||
72 | }; |
||
73 | |||
74 | $app['callbackResolver'] = function (Jarvis $app): CallbackResolver { |
||
75 | return new CallbackResolver($app); |
||
76 | }; |
||
77 | |||
78 | $app->lock(['debug', 'request', 'session', 'router', 'callbackResolver']); |
||
79 | |||
80 | // events receivers |
||
81 | // ================ |
||
82 | |||
83 | $app->on(BroadcasterInterface::EXCEPTION_EVENT, function (ExceptionEvent $event): void { |
||
84 | $response = new Response($event->exception()->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); |
||
85 | $event->setResponse($response); |
||
86 | }, BroadcasterInterface::RECEIVER_LOW_PRIORITY); |
||
87 | |||
88 | $app->on(BroadcasterInterface::CONTROLLER_EVENT, function (ControllerEvent $event) use ($app): void { |
||
89 | $finalArgs = []; |
||
90 | $rawArgs = $event->arguments(); |
||
91 | $refMethod = new \ReflectionMethod($event->callback(), '__invoke'); |
||
92 | foreach ($refMethod->getParameters() as $refParam) { |
||
93 | if (null !== $refClass = $refParam->getClass()) { |
||
94 | if (isset($app[$refClass->getName()])) { |
||
95 | $finalArgs[$refParam->getPosition()] = $app[$refClass->getName()]; |
||
96 | |||
97 | continue; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if (in_array($refParam->name, array_keys($rawArgs))) { |
||
102 | $finalArgs[$refParam->getPosition()] = $rawArgs[$refParam->name]; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | $event->setArguments($finalArgs); |
||
107 | }, BroadcasterInterface::RECEIVER_LOW_PRIORITY); |
||
108 | } |
||
109 | } |
||
110 |