Conditions | 9 |
Paths | 144 |
Total Lines | 72 |
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 |
||
35 | public function toArray() |
||
36 | { |
||
37 | $body = $this->doc->bibliographic; // PHP makes a copy for us |
||
38 | |||
39 | $body['id'] = $this->doc->id; |
||
40 | $body['bibsys_id'] = $this->doc->bibsys_id; |
||
41 | |||
42 | // Remove some stuff we don't need |
||
43 | foreach (['agency', 'catalogingRules', 'debug', 'modified', 'extent', 'cover_image'] as $key) { |
||
44 | unset($body[$key]); |
||
45 | } |
||
46 | |||
47 | // Add local collections |
||
48 | $body['collections'] = []; |
||
49 | foreach ($this->doc->collections as $collection) { |
||
50 | $body['collections'][] = $collection['name']; |
||
51 | } |
||
52 | |||
53 | // Add cover |
||
54 | $body['cover'] = $this->doc->cover ? $this->doc->cover->toArray() : null; |
||
55 | |||
56 | // Add subjects |
||
57 | $body['subjects'] = []; |
||
58 | foreach ($this->doc->subjects as $subject) { |
||
59 | $body['subjects'][$subject['vocabulary'] ?: 'keywords'][] = [ |
||
60 | 'id' => array_get($subject, 'id'), |
||
61 | 'prefLabel' => str_replace('--', ' : ', array_get($subject, 'term')), |
||
62 | 'type' => array_get($subject, 'type'), |
||
63 | 'count' => $this->docIndex->getUsageCount(array_get($subject, 'id'), 'subject'), |
||
64 | ]; |
||
65 | } |
||
66 | |||
67 | // Add genres |
||
68 | $body['genres'] = []; |
||
69 | foreach ($this->doc->genres as $genre) { |
||
70 | $body['genres'][$genre['vocabulary'] ?: 'keywords'][] = [ |
||
71 | 'id' => array_get($genre, 'id'), |
||
72 | 'prefLabel' => array_get($genre, 'term'), |
||
73 | 'count' => $this->docIndex->getUsageCount(array_get($genre, 'id'), 'genre'), |
||
74 | ]; |
||
75 | } |
||
76 | |||
77 | // Add holdings |
||
78 | $this->addHoldings($body, $this->doc); |
||
79 | |||
80 | // Add xisbns |
||
81 | $body['xisbns'] = (new XisbnResponse($this->doc->xisbn))->getSimpleRepr(); |
||
82 | |||
83 | // Add description |
||
84 | $body['description'] = $this->doc->description; |
||
85 | |||
86 | // Add 'other form' |
||
87 | $otherFormId = array_get($body, 'other_form.id'); |
||
88 | if (!empty($otherFormId)) { |
||
89 | |||
90 | // @TODO: https://github.com/scriptotek/colligator-backend/issues/34 |
||
91 | // Not sure how to handle this in Alma yet |
||
92 | unset($body['other_form']); |
||
93 | // $otherFormDoc = Document::where('bibsys_id', '=', $otherFormId)->firstOrFail(); |
||
|
|||
94 | // $body['other_form'] = [ |
||
95 | // 'id' => $otherFormDoc->id, |
||
96 | // 'bibsys_id' => $otherFormDoc->bibsys_id, |
||
97 | // 'electronic' => $otherFormDoc->isElectronic(), |
||
98 | // ]; |
||
99 | // $this->addHoldings($body['other_form'], $otherFormDoc); |
||
100 | } |
||
101 | |||
102 | // Add 'cannot_find_cover' |
||
103 | $body['cannot_find_cover'] = $this->doc->cannot_find_cover; |
||
104 | |||
105 | return $body; |
||
106 | } |
||
107 | |||
162 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.