Conditions | 17 |
Total Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 306 |
Changes | 1 | ||
Bugs | 0 | Features | 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:
Complex classes like Mapper._build_request() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from plugin.core.environment import Environment |
||
201 | def _build_request(self, match, item, episode=None): |
||
202 | if not match: |
||
203 | log.warn('Invalid value provided for "match" parameter') |
||
204 | return None |
||
205 | |||
206 | if not item: |
||
207 | log.warn('Invalid value provided for "item" parameter') |
||
208 | return None |
||
209 | |||
210 | # Retrieve identifier |
||
211 | id_service = match.identifiers.keys()[0] |
||
212 | id_key = try_convert(match.identifiers[id_service], int, match.identifiers[id_service]) |
||
213 | |||
214 | if type(id_key) not in [int, str]: |
||
215 | log.info('Unsupported key: %r', id_key) |
||
216 | return None |
||
217 | |||
218 | # Determine media type |
||
219 | if isinstance(match, MovieMatch): |
||
220 | media = 'movie' |
||
221 | elif isinstance(match, EpisodeMatch): |
||
222 | media = 'show' |
||
223 | else: |
||
224 | log.warn('Unknown match: %r', match) |
||
225 | return None |
||
226 | |||
227 | # Strip media from identifier key |
||
228 | id_service_parts = id_service.split(':', 1) |
||
229 | |||
230 | if len(id_service_parts) == 2: |
||
231 | id_service, id_media = tuple(id_service_parts) |
||
232 | else: |
||
233 | id_media = None |
||
234 | |||
235 | if id_media and id_media != media: |
||
236 | log.warn('Identifier mismatch, [%s: %r] doesn\'t match %r', id_service, id_key, media) |
||
237 | return None |
||
238 | |||
239 | # Build request |
||
240 | request = { |
||
241 | media: { |
||
242 | 'title': item.title, |
||
243 | |||
244 | 'ids': { |
||
245 | id_service: id_key |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | if item.year: |
||
251 | request[media]['year'] = item.year |
||
252 | elif episode and episode.year: |
||
253 | request[media]['year'] = episode.year |
||
254 | else: |
||
255 | log.warn('Missing "year" parameter on %r', item) |
||
256 | |||
257 | # Add episode parameters |
||
258 | if isinstance(match, EpisodeMatch): |
||
259 | if match.absolute_num is not None: |
||
260 | log.info('Absolute mappings are not supported') |
||
261 | return None |
||
262 | |||
263 | if match.season_num is None or match.episode_num is None: |
||
264 | log.warn('Missing season or episode number in %r', match) |
||
265 | return None |
||
266 | |||
267 | request['episode'] = { |
||
268 | 'season': match.season_num, |
||
269 | 'number': match.episode_num |
||
270 | } |
||
271 | |||
272 | if episode: |
||
273 | request['episode']['title'] = episode.title |
||
274 | |||
275 | return request |
||
276 | |||
292 |