| Conditions | 14 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 210 |
| 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 |
||
| 135 | def _build_request(self, match, item, episode=None): |
||
| 136 | if not match: |
||
| 137 | log.warn('Invalid value provided for "match" parameter') |
||
| 138 | return None |
||
| 139 | |||
| 140 | if not item: |
||
| 141 | log.warn('Invalid value provided for "item" parameter') |
||
| 142 | return None |
||
| 143 | |||
| 144 | # Retrieve identifier |
||
| 145 | service = match.identifiers.keys()[0] |
||
| 146 | key = try_convert(match.identifiers[service], int, match.identifiers[service]) |
||
| 147 | |||
| 148 | if type(key) not in [int, str]: |
||
| 149 | log.info('Unsupported key: %r', key) |
||
| 150 | return None |
||
| 151 | |||
| 152 | # Determine media type |
||
| 153 | if isinstance(match, MovieMatch): |
||
| 154 | media = 'movie' |
||
| 155 | elif isinstance(match, EpisodeMatch): |
||
| 156 | media = 'show' |
||
| 157 | else: |
||
| 158 | log.warn('Unknown match: %r', match) |
||
| 159 | return None |
||
| 160 | |||
| 161 | # Build request |
||
| 162 | request = { |
||
| 163 | media: { |
||
| 164 | 'title': item.title, |
||
| 165 | |||
| 166 | 'ids': { |
||
| 167 | service: key |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | if item.year: |
||
| 173 | request[media]['year'] = item.year |
||
| 174 | elif episode and episode.year: |
||
| 175 | request[media]['year'] = episode.year |
||
| 176 | else: |
||
| 177 | log.warn('Missing "year" parameter on %r', item) |
||
| 178 | |||
| 179 | # Add episode parameters |
||
| 180 | if isinstance(match, EpisodeMatch): |
||
| 181 | if not episode: |
||
| 182 | log.warn('Missing "episode" parameter') |
||
| 183 | return None |
||
| 184 | |||
| 185 | if match.absolute_num is not None: |
||
| 186 | # TODO support for absolute episode scrobbling |
||
| 187 | log.info('Absolute mappings are not supported yet') |
||
| 188 | return None |
||
| 189 | |||
| 190 | if match.season_num is None or match.episode_num is None: |
||
| 191 | log.warn('Missing season or episode number in %r', match) |
||
| 192 | return None |
||
| 193 | |||
| 194 | request['episode'] = { |
||
| 195 | 'title': episode.title, |
||
| 196 | |||
| 197 | 'season': match.season_num, |
||
| 198 | 'number': match.episode_num |
||
| 199 | } |
||
| 200 | |||
| 201 | return request |
||
| 202 | |||
| 218 |