Conditions | 11 |
Paths | 83 |
Total Lines | 67 |
Code Lines | 37 |
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 |
||
37 | public function players() |
||
|
|||
38 | { |
||
39 | $valid = $this->validateRequestVars(); |
||
40 | |||
41 | // If validation didn't pass, chuck 'em out |
||
42 | if ($valid !== true) { |
||
43 | return $this->respondWithError($valid->getMessage(), self::CODE_WRONG_ARGS); |
||
44 | } |
||
45 | |||
46 | $server = $_GET['server']; |
||
47 | $limit = $_GET['limit']; |
||
48 | $offset = $_GET['offset']; |
||
49 | |||
50 | // Translate field into table specific columns |
||
51 | if (isset($_GET['field'])) { |
||
52 | $field = $this->getField($_GET['field']); |
||
53 | } |
||
54 | |||
55 | if (! isset($field)) { |
||
56 | return $this->respondWithError('Field wasn\'t provided and is required.', self::CODE_WRONG_ARGS); |
||
57 | } |
||
58 | |||
59 | // Perform Query |
||
60 | $query = $this->repository->newQuery(); |
||
61 | $query->cols(['*']); |
||
62 | $query->orderBy(["{$field} desc"]); |
||
63 | |||
64 | if (isset($server)) { |
||
65 | $query->where('playerServer = ?', $server); |
||
66 | } |
||
67 | |||
68 | if (isset($limit)) { |
||
69 | $query->limit($limit); |
||
70 | } else { |
||
71 | $query->limit(10); // Set default limit |
||
72 | } |
||
73 | |||
74 | if (isset($offset)) { |
||
75 | $query->offset($offset); |
||
76 | } |
||
77 | |||
78 | $players = $this->repository->fireStatementAndReturn($query); |
||
79 | |||
80 | $count = count($players); |
||
81 | |||
82 | // Gets outfit details |
||
83 | for ($i = 0; $i < $count; $i++) { |
||
84 | if (! empty($players[$i]['playerOutfit'])) { |
||
85 | // Gets outfit details |
||
86 | try { |
||
87 | $outfit = $this->dataEndpoint->getOutfit($players[$i]['playerOutfit']); |
||
88 | } catch (CensusErrorException $e) { |
||
89 | $outfit = null; |
||
90 | } catch (CensusEmptyException $e) { |
||
91 | $outfit = null; |
||
92 | } |
||
93 | |||
94 | $players[$i]['playerOutfit'] = $outfit; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $this->respond( |
||
99 | 'collection', |
||
100 | $players, |
||
101 | new PlayerLeaderboardTransformer |
||
102 | ); |
||
103 | } |
||
104 | |||
134 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: