Conditions | 16 |
Paths | 42 |
Total Lines | 78 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Tests | 45 |
CRAP Score | 17.5382 |
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 |
||
32 | 26 | public function __call($name, array $args) |
|
33 | { |
||
34 | 26 | $this->getPropertiesInfo(); |
|
|
|||
35 | |||
36 | 26 | $methodCallInfo = $this->getMethodCallInfo($name); |
|
37 | 24 | $method = $methodCallInfo['method']; |
|
38 | 24 | $property = $methodCallInfo['property']; |
|
39 | 24 | $collectionProperties = $methodCallInfo['collectionProperties']; |
|
40 | |||
41 | 24 | $valuesToUpdate = array(); |
|
42 | |||
43 | switch ($method) { |
||
44 | 24 | case 'get': |
|
45 | 24 | case 'is': |
|
46 | 19 | return $this->$property; |
|
47 | |||
48 | 21 | case 'set': |
|
49 | // a setter should have exactly one argument |
||
50 | 14 | MethodCallManager::assertArgsNumber(1, $args); |
|
51 | // we set a collection here if there is an association with it |
||
52 | if ( |
||
53 | 14 | !empty($this->_collectionsItemNames['byProperty'][$property]) |
|
54 | 14 | && !(empty($this->_associationsList[$property])) |
|
55 | 14 | ) { |
|
56 | $itemName = $this->_collectionsItemNames['byProperty'][$property]['itemName']; |
||
1 ignored issue
–
show
|
|||
57 | $propertyAddMethod = 'add' . strtoupper(substr($itemName, 0, 1)) . substr($itemName, 1); |
||
58 | $propertyRemoveMethod = 'remove' . strtoupper(substr($itemName, 0, 1)) . substr($itemName, 1); |
||
59 | |||
60 | foreach ($this->$property as $item) { |
||
61 | $this->$propertyRemoveMethod($item); |
||
62 | } |
||
63 | foreach ($args[0] as $item) { |
||
64 | $this->$propertyAddMethod($item); |
||
65 | } |
||
66 | } |
||
67 | // we set a regular property here |
||
68 | else { |
||
69 | 14 | $oldValue = $this->$property; |
|
70 | 14 | $newValue = $args[0]; |
|
71 | $valuesToUpdate = array( |
||
72 | 14 | 'oldValue' => $oldValue, |
|
73 | 'newValue' => $newValue |
||
74 | 14 | ); |
|
75 | // check that the setter argument respects the property constraints |
||
76 | 14 | $this->assertPropertyValue($property, $newValue); |
|
77 | |||
78 | 11 | if ($oldValue !== $newValue) { |
|
79 | 11 | $this->$property = $newValue; |
|
80 | 11 | } |
|
81 | } |
||
82 | 11 | break; |
|
83 | |||
84 | 10 | case 'add': |
|
85 | 10 | case 'remove': |
|
86 | 10 | $valueToUpdate = ($method === 'add') ? 'newValue' : 'oldValue'; |
|
87 | 10 | switch ($collectionProperties['behavior']) { |
|
88 | 10 | case 'list': |
|
89 | 4 | ListManager::$method($this->$property, $args); |
|
90 | 4 | $valuesToUpdate[$valueToUpdate] = $args[0]; |
|
91 | 4 | break; |
|
92 | 6 | case 'map': |
|
93 | 2 | MapManager::$method($this->$property, $args); |
|
94 | 1 | break; |
|
95 | 4 | case 'set': |
|
96 | 4 | SetManager::$method($this->$property, $args); |
|
97 | 4 | $valuesToUpdate[$valueToUpdate] = $args[0]; |
|
98 | 4 | break; |
|
99 | 9 | } |
|
100 | 9 | break; |
|
101 | } |
||
102 | |||
103 | // manage associations |
||
104 | 17 | if (in_array($method, array('set', 'add', 'remove'))) { |
|
105 | 17 | $this->updatePropertyAssociation($property, $valuesToUpdate); |
|
106 | 17 | } |
|
107 | |||
108 | 17 | return $this; |
|
109 | } |
||
110 | |||
150 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: