Conditions | 20 |
Paths | 3888 |
Total Lines | 93 |
Code Lines | 64 |
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 |
||
113 | public function route() |
||
114 | { |
||
115 | $segments = server_request()->getUri()->segments->getArrayCopy(); |
||
116 | |||
117 | if (false !== ($key = array_search('images', $segments))) { |
||
118 | $segments = array_slice($segments, $key); |
||
|
|||
119 | } else { |
||
120 | array_shift($segments); |
||
121 | } |
||
122 | |||
123 | $this->imageFilePath = $this->imageNotFoundFilename; |
||
124 | |||
125 | $this->imageSize[ 'width' ] = $this->input->get('width'); |
||
126 | $this->imageSize[ 'height' ] = $this->input->get('height'); |
||
127 | $this->imageScale = $this->input->get('scale'); |
||
128 | $this->imageQuality = $this->input->get('quality'); |
||
129 | $this->imageCrop = $this->input->get('crop'); |
||
130 | |||
131 | if (false !== ($key = array_search('crop', $segments))) { |
||
132 | $this->imageCrop = true; |
||
133 | unset($segments[ $key ]); |
||
134 | $segments = array_values($segments); |
||
135 | } |
||
136 | |||
137 | if (count($segments) == 1) { |
||
138 | $this->imageFilePath = $this->storagePath . end($segments); |
||
139 | } elseif (count($segments) >= 2) { |
||
140 | if (preg_match("/(\d+)(x)(\d+)/", $segments[ count($segments) - 2 ], $matches)) { |
||
141 | $this->imageSize[ 'width' ] = $matches[ 1 ]; |
||
142 | $this->imageSize[ 'height' ] = $matches[ 3 ]; |
||
143 | |||
144 | if (count($segments) == 2) { |
||
145 | $this->imageFilePath = $this->storagePath . end($segments); |
||
146 | } else { |
||
147 | $this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, |
||
148 | array_slice($segments, 0, |
||
149 | count($segments) - 2)) . DIRECTORY_SEPARATOR . end($segments); |
||
150 | } |
||
151 | } elseif (preg_match("/(\d+)(p)/", $segments[ count($segments) - 2 ], |
||
152 | $matches) or is_numeric($segments[ count($segments) - 2 ]) |
||
153 | ) { |
||
154 | $this->imageScale = isset($matches[ 1 ]) ? $matches[ 1 ] : $segments[ count($segments) - 2 ]; |
||
155 | if (count($segments) == 2) { |
||
156 | $this->imageFilePath = $this->storagePath . end($segments); |
||
157 | } else { |
||
158 | $this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, |
||
159 | array_slice($segments, 0, |
||
160 | count($segments) - 2)) . DIRECTORY_SEPARATOR . end($segments); |
||
161 | } |
||
162 | } else { |
||
163 | $this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, $segments); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | $imageFilePath = $this->imageFilePath; |
||
168 | $extensions[ 0 ] = pathinfo($imageFilePath, PATHINFO_EXTENSION); |
||
169 | |||
170 | for ($i = 0; $i < 2; $i++) { |
||
171 | $extension = pathinfo($imageFilePath, PATHINFO_EXTENSION); |
||
172 | |||
173 | if ($extension !== '') { |
||
174 | $extensions[ $i ] = $extension; |
||
175 | $imageFilePath = str_replace('.' . $extensions[ $i ], '', $imageFilePath); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | $mimes = [ |
||
180 | 'gif' => 'image/gif', |
||
181 | 'jpg' => 'image/jpeg', |
||
182 | 'png' => 'image/png', |
||
183 | 'webp' => 'image/webp', |
||
184 | ]; |
||
185 | |||
186 | if (count($extensions) == 2) { |
||
187 | $this->imageFilePath = $imageFilePath . '.' . $extensions[ 1 ]; |
||
188 | } |
||
189 | |||
190 | if (array_key_exists($extension = reset($extensions), $mimes)) { |
||
191 | $this->imageFileMime = $mimes[ $extension ]; |
||
192 | } elseif (array_key_exists($extension = pathinfo($this->imageFilePath, PATHINFO_EXTENSION), $mimes)) { |
||
193 | $this->imageFileMime = $mimes[ $extension ]; |
||
194 | } |
||
195 | |||
196 | if ( ! is_file($this->imageFilePath)) { |
||
197 | $this->imageFilePath = $this->imageNotFoundFilename; |
||
198 | } |
||
199 | |||
200 | if ( ! empty($this->imageScale)) { |
||
201 | $this->scale(); |
||
202 | } elseif ( ! empty($this->imageSize[ 'width' ]) || ! empty($this->imageSize[ 'height' ])) { |
||
203 | $this->resize(); |
||
204 | } else { |
||
205 | $this->original(); |
||
206 | } |
||
330 | } |