Conditions | 19 |
Paths | 741 |
Total Lines | 92 |
Code Lines | 34 |
Lines | 3 |
Ratio | 3.26 % |
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 namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section\Command; |
||
43 | public function handle(Request $request, Authorizer $authorizer, BreadcrumbCollection $breadcrumbs) |
||
44 | { |
||
45 | $controlPanel = $this->builder->getControlPanel(); |
||
46 | $sections = $controlPanel->getSections(); |
||
47 | |||
48 | /* |
||
49 | * If we already have an active section |
||
50 | * then we don't need to do this. |
||
51 | */ |
||
52 | if ($active = $sections->active()) { |
||
53 | return; |
||
54 | } |
||
55 | |||
56 | /* @var SectionInterface $section */ |
||
57 | foreach ($sections as $section) { |
||
58 | |||
59 | if (($matcher = $section->getMatcher()) && str_is($matcher, $request->path())) { |
||
60 | $active = $section; |
||
61 | } |
||
62 | |||
63 | /* |
||
64 | * Get the HREF for both the active |
||
65 | * and loop iteration section. |
||
66 | */ |
||
67 | $href = $section->getPermalink() ?: array_get($section->getAttributes(), 'href'); |
||
68 | $activeHref = ''; |
||
69 | |||
70 | if ($active && $active instanceof SectionInterface) { |
||
71 | $activeHref = $active->getPermalink() ?: array_get($active->getAttributes(), 'href'); |
||
72 | } |
||
73 | |||
74 | /* |
||
75 | * If the request URL does not even |
||
76 | * contain the HREF then skip it. |
||
77 | */ |
||
78 | if (!str_contains($request->url(), $href)) { |
||
79 | continue; |
||
80 | } |
||
81 | |||
82 | /* |
||
83 | * Compare the length of the active HREF |
||
84 | * and loop iteration HREF. The longer the |
||
85 | * HREF the more detailed and exact it is and |
||
86 | * the more likely it is the active HREF and |
||
87 | * therefore the active section. |
||
88 | */ |
||
89 | $hrefLength = strlen($href); |
||
90 | $activeHrefLength = strlen($activeHref); |
||
91 | |||
92 | if ($hrefLength > $activeHrefLength) { |
||
93 | $active = $section; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * If we have an active section determined |
||
99 | * then mark it as such. |
||
100 | * |
||
101 | * @var SectionInterface $active |
||
102 | * @var SectionInterface $section |
||
103 | */ |
||
104 | if ($active) { |
||
105 | if ($active->getParent()) { |
||
|
|||
106 | $active->setActive(true); |
||
107 | |||
108 | $section = $sections->get($active->getParent(), $sections->first()); |
||
109 | |||
110 | $section->setHighlighted(true); |
||
111 | |||
112 | $breadcrumbs->put($section->getBreadcrumb() ?: $section->getTitle(), $section->getHref()); |
||
113 | } else { |
||
114 | $active->setActive(true)->setHighlighted(true); |
||
115 | } |
||
116 | } elseif ($active = $sections->first()) { |
||
117 | $active->setActive(true)->setHighlighted(true); |
||
118 | } |
||
119 | |||
120 | // No active section! |
||
121 | if (!$active) { |
||
122 | return; |
||
123 | } |
||
124 | |||
125 | // Authorize the active section. |
||
126 | if (!$authorizer->authorize($active->getPermission())) { |
||
127 | abort(403); |
||
128 | } |
||
129 | |||
130 | // Add the bread crumb. |
||
131 | View Code Duplication | if (($breadcrumb = $active->getBreadcrumb()) !== false) { |
|
132 | $breadcrumbs->put($breadcrumb ?: $active->getTitle(), $active->getHref()); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: