Conditions | 6 |
Paths | 8 |
Total Lines | 56 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 6.0118 |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
97 | 1 | public function processResponse() |
|
98 | { |
||
99 | // Implode and rip out the JSON |
||
100 | 1 | preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
|
101 | |||
102 | // Return should be JSON, let's validate |
||
103 | 1 | if (($json = json_decode($matches[0])) === null) { |
|
104 | throw new Exception("JSON response from Tshock protocol is invalid."); |
||
105 | } |
||
106 | |||
107 | // Check the status response |
||
108 | 1 | if ($json->status != 200) { |
|
109 | throw new Exception("JSON status from Tshock protocol response was '{$json->status}', expected '200'."); |
||
110 | } |
||
111 | |||
112 | 1 | $result = new Result(); |
|
113 | |||
114 | // Server is always dedicated |
||
115 | 1 | $result->add('dedicated', 1); |
|
116 | |||
117 | // Add server items |
||
118 | 1 | $result->add('hostname', $json->name); |
|
119 | 1 | $result->add('game_port', $json->port); |
|
120 | 1 | $result->add('serverversion', $json->serverversion); |
|
121 | 1 | $result->add('world', $json->world); |
|
122 | 1 | $result->add('uptime', $json->uptime); |
|
123 | 1 | $result->add('password', (int)$json->serverpassword); |
|
124 | 1 | $result->add('numplayers', $json->playercount); |
|
125 | 1 | $result->add('maxplayers', $json->maxplayers); |
|
126 | |||
127 | // Parse players |
||
128 | 1 | foreach ($json->players as $player) { |
|
129 | 1 | $result->addPlayer('nickname', $player->nickname); |
|
130 | 1 | $result->addPlayer('username', $player->username); |
|
131 | 1 | $result->addPlayer('group', $player->group); |
|
132 | 1 | $result->addPlayer('active', (int)$player->active); |
|
133 | 1 | $result->addPlayer('state', $player->state); |
|
134 | 1 | $result->addPlayer('team', $player->team); |
|
135 | } |
||
136 | |||
137 | // Make rules into simple array |
||
138 | 1 | $rules = []; |
|
139 | |||
140 | // Parse rules |
||
141 | 1 | foreach ($json->rules as $rule => $value) { |
|
142 | // Add rule but convert boolean into int (0|1) |
||
143 | 1 | $rules[$rule] = (is_bool($value)) ? (int)$value : $value; |
|
144 | } |
||
145 | |||
146 | // Add rules |
||
147 | 1 | $result->add('rules', $rules); |
|
1 ignored issue
–
show
|
|||
148 | |||
149 | 1 | unset($rules, $rule, $player, $value); |
|
150 | |||
151 | 1 | return $result->fetch(); |
|
152 | } |
||
153 | } |
||
154 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: