Conditions | 8 |
Paths | 12 |
Total Lines | 56 |
Code Lines | 28 |
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 |
||
43 | public function getSpoofs($username) |
||
44 | { |
||
45 | // FIXME: domains! |
||
46 | /** @var Domain $domain */ |
||
47 | $domain = Domain::getById(1, $this->database); |
||
48 | |||
49 | /** @var AntiSpoofCache $cacheResult */ |
||
50 | $cacheResult = AntiSpoofCache::getByUsername($username, $this->database); |
||
51 | if ($cacheResult == false) { |
||
|
|||
52 | // get the data from the API |
||
53 | $data = $this->httpHelper->get($domain->getWikiApiPath(), array( |
||
54 | 'action' => 'antispoof', |
||
55 | 'format' => 'php', |
||
56 | 'username' => $username, |
||
57 | )); |
||
58 | |||
59 | $cacheEntry = new AntiSpoofCache(); |
||
60 | $cacheEntry->setDatabase($this->database); |
||
61 | $cacheEntry->setUsername($username); |
||
62 | $cacheEntry->setData($data); |
||
63 | $cacheEntry->save(); |
||
64 | |||
65 | $cacheResult = $cacheEntry; |
||
66 | } |
||
67 | else { |
||
68 | $data = $cacheResult->getData(); |
||
69 | } |
||
70 | |||
71 | $result = unserialize($data); |
||
72 | |||
73 | if (!isset($result['antispoof']) || !isset($result['antispoof']['result'])) { |
||
74 | $cacheResult->delete(); |
||
75 | |||
76 | if (isset($result['error']['info'])) { |
||
77 | throw new Exception("Unrecognised API response to query: " . $result['error']['info']); |
||
78 | } |
||
79 | |||
80 | throw new Exception("Unrecognised API response to query."); |
||
81 | } |
||
82 | |||
83 | if ($result['antispoof']['result'] == "pass") { |
||
84 | // All good here! |
||
85 | return array(); |
||
86 | } |
||
87 | |||
88 | if ($result['antispoof']['result'] == "conflict") { |
||
89 | // we've got conflicts, let's do something with them. |
||
90 | return $result['antispoof']['users']; |
||
91 | } |
||
92 | |||
93 | if ($result['antispoof']['result'] == "error") { |
||
94 | // we've got conflicts, let's do something with them. |
||
95 | throw new Exception("Encountered error while getting result: " . $result['antispoof']['error']); |
||
96 | } |
||
97 | |||
98 | throw new Exception("Unrecognised API response to query."); |
||
99 | } |
||
101 |