Conditions | 6 |
Paths | 7 |
Total Lines | 56 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
75 | public function explain(Request $request, $token, $index_query, $format) |
||
76 | { |
||
77 | $panel = 'pomm'; |
||
78 | $page = 'home'; |
||
79 | |||
80 | if (!($profile = $this->profiler->loadProfile($token))) { |
||
81 | return new Response( |
||
82 | $this->twig->render( |
||
83 | '@WebProfiler/Profiler/info.html.twig', |
||
84 | array('about' => 'no_token', 'token' => $token) |
||
85 | ), |
||
86 | 200, |
||
87 | array('Content-Type' => 'text/html') |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | $this->profiler->disable(); |
||
92 | |||
93 | if (!$profile->hasCollector($panel)) { |
||
94 | throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token)); |
||
95 | } |
||
96 | |||
97 | if (!array_key_exists($index_query, $profile->getCollector($panel)->getQueries())) { |
||
|
|||
98 | throw new \InvalidArgumentException(sprintf("No such query index '%s'.", $index_query)); |
||
99 | } |
||
100 | |||
101 | $query_data = $profile->getCollector($panel)->getQueries()[$index_query]; |
||
102 | |||
103 | $explain = 'explain'; |
||
104 | |||
105 | if ($format === 'json') { |
||
106 | $explain .= ' (COSTS, VERBOSE, FORMAT JSON)'; |
||
107 | } |
||
108 | |||
109 | $explain = $this->pomm[$query_data['session_stamp']] |
||
110 | ->getClientUsingPooler('query_manager', null) |
||
111 | ->query(sprintf("%s %s", $explain, $query_data['sql']), $query_data['parameters']); |
||
112 | |||
113 | if ($format === 'json') { |
||
114 | $template = '@Pomm/Profiler/graph.html.twig'; |
||
115 | } |
||
116 | else { |
||
117 | $template = '@Pomm/Profiler/explain.html.twig'; |
||
118 | } |
||
119 | |||
120 | return new Response($this->twig->render($template, array( |
||
121 | 'token' => $token, |
||
122 | 'profile' => $profile, |
||
123 | 'collector' => $profile->getCollector($panel), |
||
124 | 'panel' => $panel, |
||
125 | 'page' => $page, |
||
126 | 'request' => $request, |
||
127 | 'query_index' => $index_query, |
||
128 | 'explain' => $explain, |
||
129 | )), 200, array('Content-Type' => 'text/html')); |
||
130 | } |
||
131 | } |
||
132 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: