Conditions | 11 |
Paths | 80 |
Total Lines | 64 |
Code Lines | 40 |
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 |
||
123 | public function storeRawRow(array $flattenedRow) |
||
124 | { |
||
125 | $song = new Song(); |
||
126 | $song |
||
127 | ->setArtist($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_ARTIST]]) |
||
128 | ->setTitle($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_TITLE]]); |
||
129 | |||
130 | $durationMS = trim($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_DURATION_MMSS]]); |
||
131 | if ($durationMS && preg_match('/^\s*(\d+):(\d+)\s*$/', $durationMS, $matches)) { |
||
132 | $song->setDuration(($matches[1] * 60) + $matches[2]); |
||
133 | } |
||
134 | |||
135 | $sourceName = trim($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_SOURCE]]); |
||
136 | if ($sourceName) { |
||
137 | $source = $this->dataStore->fetchSourceByName($sourceName); |
||
138 | if (!$source) { |
||
139 | $source = new Source($sourceName); |
||
140 | $this->dataStore->storeSource($source); |
||
141 | } |
||
142 | if ($source) { |
||
143 | $song->setSourceId($source->getId()); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | $this->dataStore->storeSong($song); // Store song before all xrefs as we need ID |
||
148 | |||
149 | // Platforms |
||
150 | $platformIds = []; |
||
151 | $platformFields = [ |
||
152 | self::INPUT_FIELD_IN_RB3 => 'RB3', |
||
153 | self::INPUT_FIELD_IN_RB4 => 'RB4', |
||
154 | ]; |
||
155 | foreach ($platformFields as $field => $platformName) { |
||
156 | if (trim($flattenedRow[$this->fieldLookup[$field]])) { |
||
157 | $platform = $this->dataStore->fetchPlatformByName($platformName); |
||
158 | if (!$platform) { |
||
159 | $platform = new Platform($platformName); |
||
160 | $this->dataStore->storePlatform($platform); |
||
161 | } |
||
162 | $platformIds[] = $platform->getId(); |
||
163 | } |
||
164 | } |
||
165 | $this->dataStore->storeSongPlatformLinks($song->getId(), $platformIds); |
||
166 | |||
167 | // Instruments - all stored at init(); // TODO add harmony |
||
168 | $instruments = ['Vocals', 'Guitar', 'Bass', 'Drums']; |
||
169 | if (trim($flattenedRow[$this->fieldLookup[self::INPUT_FIELD_HAS_KEYS]])) { |
||
170 | $instruments[] = 'Keyboard'; |
||
171 | } |
||
172 | |||
173 | $datastore = $this->dataStore; |
||
174 | $instrumentIds = array_map( |
||
175 | function ($name) use ($datastore) { |
||
176 | $instrument = $datastore->fetchInstrumentByName($name); |
||
177 | return $instrument ? $instrument->getId() : null; |
||
178 | }, |
||
179 | $instruments |
||
180 | ); |
||
181 | |||
182 | $this->dataStore->storeSongInstrumentLinks($song->getId(), $instrumentIds); |
||
183 | |||
184 | |||
185 | return true; |
||
186 | } |
||
187 | |||
236 |