| Conditions | 12 |
| Paths | 65 |
| Total Lines | 88 |
| 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 |
||
| 31 | public function __construct(\stdClass $response) |
||
| 32 | { |
||
| 33 | if (isset($response->characters->error)) { |
||
| 34 | throw new NotFoundException('Character does not exists.'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $achievements = collect(); |
||
| 38 | foreach ($response->characters->achievements as $achievement) { |
||
| 39 | $achievements->push(new Achievement($achievement->name, $achievement->stars)); |
||
| 40 | } |
||
| 41 | |||
| 42 | $accountInformation = null; |
||
| 43 | if (!empty($response->characters->account_information)) { |
||
| 44 | $accountInformation = new AccountInformation( |
||
| 45 | $response->characters->account_information->loyalty_title, |
||
| 46 | new Carbon( |
||
| 47 | $response->characters->account_information->created->date, |
||
| 48 | $response->characters->account_information->created->timezone |
||
| 49 | ) |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | |||
| 53 | $deaths = collect(); |
||
| 54 | if (!empty($response->characters->deaths)) { |
||
| 55 | foreach ($response->characters->deaths as $death) { |
||
| 56 | $involved = collect(); |
||
| 57 | foreach ($death->involved as $deathInvolved) { |
||
| 58 | $involved->push($deathInvolved->name); |
||
| 59 | } |
||
| 60 | |||
| 61 | $deaths->push(new Death( |
||
| 62 | new Carbon($death->date->date, $death->date->timezone), |
||
| 63 | $death->level, |
||
| 64 | $death->reason, |
||
| 65 | $involved |
||
| 66 | )); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $otherCharacters = collect(); |
||
| 71 | if (!empty($response->characters->account_information)) { |
||
| 72 | foreach ($response->characters->other_characters as $other_character) { |
||
| 73 | $otherCharacters->push(new OtherCharacter( |
||
| 74 | $other_character->name, |
||
| 75 | $other_character->world, |
||
| 76 | $other_character->status |
||
| 77 | )); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | $guild = null; |
||
| 82 | if (isset($response->characters->data->guild)) { |
||
| 83 | $guild = new Guild($response->characters->data->guild->name, $response->characters->data->guild->rank); |
||
| 84 | } |
||
| 85 | |||
| 86 | $lastLogin = null; |
||
| 87 | if (isset($response->characters->data->last_login)) { |
||
| 88 | $lastLogin = new Carbon( |
||
| 89 | $response->characters->data->last_login[0]->date, |
||
| 90 | $response->characters->data->last_login[0]->timezone |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->character = Character::createFromArray([ |
||
| 95 | 'name' => $response->characters->data->name, |
||
| 96 | 'former_names' => !empty($response->characters->data->former_names) |
||
| 97 | ? collect($response->characters->data->former_names) |
||
| 98 | : collect(), |
||
| 99 | 'sex' => $response->characters->data->sex, |
||
| 100 | 'vocation' => $response->characters->data->vocation, |
||
| 101 | 'level' => $response->characters->data->level, |
||
| 102 | 'achievement_points' => $response->characters->data->achievement_points, |
||
| 103 | 'world' => $response->characters->data->world, |
||
| 104 | 'former_world' => $response->characters->data->former_world ?? null, |
||
| 105 | 'residence' => $response->characters->data->residence, |
||
| 106 | 'guild' => $guild, |
||
| 107 | 'last_login' => $lastLogin, |
||
| 108 | 'comment' => $response->characters->data->comment ?? '', |
||
| 109 | 'account_status' => $response->characters->data->account_status, |
||
| 110 | 'status' => $response->characters->data->status, |
||
| 111 | 'achievements' => $achievements, |
||
| 112 | 'account_information' => $accountInformation, |
||
| 113 | 'deaths' => $deaths, |
||
| 114 | 'other_characters' => $otherCharacters, |
||
| 115 | ]); |
||
| 116 | |||
| 117 | parent::__construct($response); |
||
| 118 | } |
||
| 119 | |||
| 128 |