Conditions | 23 |
Paths | 1242 |
Total Lines | 84 |
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 |
||
83 | public function buildView(array $parameters) |
||
84 | { |
||
85 | $view = new ContentView(null, [], $parameters['viewType']); |
||
86 | $view->setIsEmbed($this->isEmbed($parameters)); |
||
87 | |||
88 | if ($view->isEmbed() && $parameters['viewType'] === null) { |
||
89 | $view->setViewType(EmbedView::DEFAULT_VIEW_TYPE); |
||
90 | } |
||
91 | |||
92 | if (isset($parameters['location']) && $parameters['location'] instanceof Location) { |
||
93 | $location = $parameters['location']; |
||
94 | } elseif (isset($parameters['locationId'])) { |
||
95 | $location = $this->loadLocation($parameters['locationId']); |
||
96 | } else { |
||
97 | $location = null; |
||
98 | } |
||
99 | |||
100 | if (isset($parameters['content'])) { |
||
101 | $content = $parameters['content']; |
||
102 | } elseif ($location instanceof Location) { |
||
103 | // if we already have location load content true it so we avoid dual loading in case user does that in view |
||
104 | $content = $location->getContent(); |
||
105 | if (!$this->canRead($content, $location, $view->isEmbed())) { |
||
106 | $missingPermission = 'read' . ($view->isEmbed() ? '|view_embed' : ''); |
||
107 | throw new UnauthorizedException( |
||
108 | 'content', |
||
109 | $missingPermission, |
||
110 | [ |
||
111 | 'contentId' => $content->id, |
||
112 | 'locationId' => $location->id, |
||
113 | ] |
||
114 | ); |
||
115 | } |
||
116 | } else { |
||
117 | if (isset($parameters['contentId'])) { |
||
118 | $contentId = $parameters['contentId']; |
||
119 | } elseif (isset($location)) { |
||
120 | $contentId = $location->contentId; |
||
121 | } else { |
||
122 | throw new InvalidArgumentException('Content', 'No content could be loaded from parameters'); |
||
123 | } |
||
124 | |||
125 | $content = $view->isEmbed() ? $this->loadEmbeddedContent($contentId, $location) : $this->loadContent($contentId); |
||
126 | } |
||
127 | |||
128 | $view->setContent($content); |
||
|
|||
129 | |||
130 | if (isset($location)) { |
||
131 | if ($location->contentId !== $content->id) { |
||
132 | throw new InvalidArgumentException('Location', 'Provided location does not belong to selected content'); |
||
133 | } |
||
134 | |||
135 | if (isset($parameters['contentId']) && $location->contentId !== (int)$parameters['contentId']) { |
||
136 | throw new InvalidArgumentException( |
||
137 | 'Location', |
||
138 | 'Provided location does not belong to selected content as requested via contentId parameter' |
||
139 | ); |
||
140 | } |
||
141 | } elseif (isset($this->locationLoader)) { |
||
142 | try { |
||
143 | $location = $this->locationLoader->loadLocation($content->contentInfo); |
||
144 | } catch (NotFoundException $e) { |
||
145 | // nothing else to do |
||
146 | } |
||
147 | } |
||
148 | |||
149 | if (isset($location)) { |
||
150 | $view->setLocation($location); |
||
151 | } |
||
152 | |||
153 | $this->viewParametersInjector->injectViewParameters($view, $parameters); |
||
154 | $this->viewConfigurator->configure($view); |
||
155 | |||
156 | // deprecated controller actions are replaced with their new equivalent, viewAction and embedAction |
||
157 | if (!$view->getControllerReference() instanceof ControllerReference) { |
||
158 | if (\in_array($parameters['_controller'], ['ez_content:viewLocation', 'ez_content:viewContent'])) { |
||
159 | $view->setControllerReference(new ControllerReference('ez_content:viewAction')); |
||
160 | } elseif (\in_array($parameters['_controller'], ['ez_content:embedLocation', 'ez_content:embedContent'])) { |
||
161 | $view->setControllerReference(new ControllerReference('ez_content:embedAction')); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $view; |
||
166 | } |
||
167 | |||
281 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.