Conditions | 11 |
Paths | 192 |
Total Lines | 30 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
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:
1 | <?php |
||
80 | public function denormalize($data, $class, $format = null, array $context = array()) |
||
81 | { |
||
82 | $mbox = isset($data['mbox']) ? $data['mbox'] : null; |
||
83 | $mboxSha1Sum = isset($data['mboxSha1Sum']) ? $data['mboxSha1Sum'] : null; |
||
84 | $openId = isset($data['openid']) ? $data['openid'] : null; |
||
85 | $name = isset($data['name']) ? $data['name'] : null; |
||
86 | $account = null; |
||
87 | |||
88 | if (isset($data['account'])) { |
||
89 | $account = $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account'); |
||
90 | } |
||
91 | |||
92 | if (isset($data['name'])) { |
||
93 | $actorData['name'] = $data['name']; |
||
|
|||
94 | } |
||
95 | |||
96 | if (isset($data['objectType']) && 'Group' === $data['objectType']) { |
||
97 | $members = array(); |
||
98 | |||
99 | if (isset($data['member'])) { |
||
100 | foreach ($data['member'] as $member) { |
||
101 | $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent'); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | return new Group($mbox, $mboxSha1Sum, $openId, $account, $name, $members); |
||
106 | } |
||
107 | |||
108 | return new Agent($mbox, $mboxSha1Sum, $openId, $account, $name); |
||
109 | } |
||
110 | |||
119 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.