Conditions | 11 |
Paths | 8 |
Total Lines | 75 |
Code Lines | 53 |
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 |
||
111 | public function createWithTemplate(array $data) |
||
112 | { |
||
113 | if (! isset($data['file_upload'])) { |
||
114 | throw new DisplayException('No template file was found submitted with this request.'); |
||
115 | } |
||
116 | |||
117 | if (! $data['file_upload']->isValid()) { |
||
118 | throw new DisplayException('The file provided does not appear to be valid.'); |
||
119 | } |
||
120 | |||
121 | if (! in_array($data['file_upload']->getMimeType(), [ |
||
122 | 'application/zip', |
||
123 | 'text/plain', |
||
124 | 'application/json', |
||
125 | ])) { |
||
126 | throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.'); |
||
127 | } |
||
128 | |||
129 | if ($data['file_upload']->getMimeType() === 'application/zip') { |
||
130 | $zip = new \ZipArchive; |
||
131 | if (! $zip->open($data['file_upload']->path())) { |
||
132 | throw new DisplayException('The uploaded archive was unable to be opened.'); |
||
133 | } |
||
134 | |||
135 | $isZip = $zip->locateName('archive.zip'); |
||
136 | $isTar = $zip->locateName('archive.tar.gz'); |
||
137 | |||
138 | if ($zip->locateName('import.json') === false || ($isZip === false && $isTar === false)) { |
||
139 | throw new DisplayException('This contents of the provided archive were in an invalid format.'); |
||
140 | } |
||
141 | |||
142 | $json = json_decode($zip->getFromName('import.json')); |
||
143 | $id = $this->create([ |
||
144 | 'name' => $json->name, |
||
145 | 'version' => $json->version, |
||
146 | 'description' => $json->description, |
||
147 | 'option' => $data['option'], |
||
148 | 'selectable' => $json->selectable, |
||
149 | 'visible' => $json->visible, |
||
150 | 'build_memory' => $json->build->memory, |
||
151 | 'build_swap' => $json->build->swap, |
||
152 | 'build_cpu' => $json->build->cpu, |
||
153 | 'build_io' => $json->build->io, |
||
154 | 'build_container' => $json->build->container, |
||
155 | 'build_script' => $json->build->script, |
||
156 | ]); |
||
157 | |||
158 | $pack = Models\ServicePack::findOrFail($id); |
||
159 | if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), ($isZip === false) ? 'archive.tar.gz' : 'archive.zip')) { |
||
160 | $pack->delete(); |
||
161 | throw new DisplayException('Unable to extract the archive file to the correct location.'); |
||
162 | } |
||
163 | |||
164 | $zip->close(); |
||
165 | |||
166 | return $pack->id; |
||
167 | } else { |
||
168 | $json = json_decode(file_get_contents($data['file_upload']->path())); |
||
169 | |||
170 | return $this->create([ |
||
171 | 'name' => $json->name, |
||
172 | 'version' => $json->version, |
||
173 | 'description' => $json->description, |
||
174 | 'option' => $data['option'], |
||
175 | 'selectable' => $json->selectable, |
||
176 | 'visible' => $json->visible, |
||
177 | 'build_memory' => $json->build->memory, |
||
178 | 'build_swap' => $json->build->swap, |
||
179 | 'build_cpu' => $json->build->cpu, |
||
180 | 'build_io' => $json->build->io, |
||
181 | 'build_container' => $json->build->container, |
||
182 | 'build_script' => $json->build->script, |
||
183 | ]); |
||
184 | } |
||
185 | } |
||
186 | |||
238 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.