Conditions | 14 |
Paths | 440 |
Total Lines | 85 |
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 |
||
114 | public function importParsedRecord(array $biblio, array $holdings = []) |
||
115 | { |
||
116 | // Convert Carbon date objects to ISO8601 strings |
||
117 | if (isset($biblio['created'])) { |
||
118 | $biblio['created'] = $biblio['created']->toIso8601String(); |
||
119 | } |
||
120 | if (isset($biblio['modified'])) { |
||
121 | $biblio['modified'] = $biblio['modified']->toIso8601String(); |
||
122 | } |
||
123 | |||
124 | // Find existing Document or create a new one |
||
125 | $doc = Document::firstOrNew(['bibsys_id' => $biblio['id']]); |
||
126 | |||
127 | // Update Document |
||
128 | $doc->bibliographic = $biblio; |
||
129 | $doc->holdings = $holdings; |
||
130 | |||
131 | // Extract description from bibliographic record if no description exists |
||
132 | if (isset($biblio['description']) && is_null($doc->description)) { |
||
133 | $this->scraper->updateDocument($doc, $biblio['description']); |
||
134 | } |
||
135 | |||
136 | if (!$doc->save()) { |
||
137 | $this->error("Document $biblio->id could not be saved!"); |
||
138 | |||
139 | return; |
||
140 | } |
||
141 | |||
142 | // Check other form |
||
143 | $other_id = array_get($biblio, 'other_form.id'); |
||
144 | if (!empty($other_id)) { |
||
|
|||
145 | |||
146 | // @TODO: https://github.com/scriptotek/colligator-backend/issues/34 |
||
147 | // Alma uses 776, but the IDs can't be looked up. Need to investigate! |
||
148 | // Example: 990824196984702204 (NZ: 990824196984702201), having |
||
149 | // 776 0_ $t The Earth after us : what legacy will humans leave in the rocks? $w 991234552274702201 |
||
150 | |||
151 | // $doc2 = Document::where('bibsys_id', '=', $other_id)->first(); |
||
152 | // if (is_null($doc2)) { |
||
153 | // $record = \SruClient::first('bs.objektid=' . $other_id); |
||
154 | // \Log::debug('Importing related record ' . $other_id); |
||
155 | // if (is_null($record)) { |
||
156 | // die('uh oh'); |
||
157 | // } else { |
||
158 | // $this->import($record->data); |
||
159 | // } |
||
160 | // } |
||
161 | |||
162 | // @TODO: Add a separate jobb that updates e-books weekly or so.. |
||
163 | } |
||
164 | |||
165 | // Sync subjects |
||
166 | $subject_ids = []; |
||
167 | foreach ($biblio['subjects'] as $value) { |
||
168 | $value['vocabulary'] = $this->fixVocabularyCode($value['vocabulary']); |
||
169 | $subject = Subject::lookup($value['vocabulary'], $value['term'], $value['type']); |
||
170 | if (is_null($subject)) { |
||
171 | $subject = Subject::create($value); |
||
172 | } |
||
173 | $subject_ids[] = $subject->id; |
||
174 | } |
||
175 | $doc->subjects()->sync($subject_ids); |
||
176 | |||
177 | // Sync genres |
||
178 | $genre_ids = []; |
||
179 | foreach ($biblio['genres'] as $value) { |
||
180 | $genre = Genre::lookup($value['vocabulary'], $value['term']); |
||
181 | if (is_null($genre)) { |
||
182 | $genre = Genre::create($value); |
||
183 | } |
||
184 | $genre_ids[] = $genre->id; |
||
185 | } |
||
186 | $doc->genres()->sync($genre_ids); |
||
187 | |||
188 | // Extract cover from bibliographic record if no local cover exists |
||
189 | if (isset($biblio['cover_image']) && is_null($doc->cover)) { |
||
190 | try { |
||
191 | $doc->storeCover($biblio['cover_image']); |
||
192 | } catch (\ErrorException $e) { |
||
193 | \Log::error('Failed to store cover: ' . $biblio['cover_image']); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | return $doc; |
||
198 | } |
||
199 | |||
226 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.