| Conditions | 14 |
| Paths | 19 |
| Total Lines | 89 |
| 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 |
||
| 61 | public function __set($propName, $propValue) |
||
| 62 | { |
||
| 63 | $methodName = 'set' . ucfirst($propName); |
||
| 64 | |||
| 65 | // Look for a setter, e.g. setDefaultChildRule() |
||
| 66 | if (method_exists($this, $methodName)) |
||
| 67 | { |
||
| 68 | $this->$methodName($propValue); |
||
| 69 | |||
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | // If the property isn't already set, we just create/set it |
||
| 74 | if (!isset($this->$propName)) |
||
| 75 | { |
||
| 76 | $this->$propName = $propValue; |
||
| 77 | |||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | // If we're trying to replace a NormalizedCollection, instead we clear it then |
||
| 82 | // iteratively set new values |
||
| 83 | if ($this->$propName instanceof NormalizedCollection) |
||
| 84 | { |
||
| 85 | if (!is_array($propValue) |
||
| 86 | && !($propValue instanceof Traversable)) |
||
| 87 | { |
||
| 88 | throw new InvalidArgumentException("Property '" . $propName . "' expects an array or a traversable object to be passed"); |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->$propName->clear(); |
||
| 92 | |||
| 93 | foreach ($propValue as $k => $v) |
||
| 94 | { |
||
| 95 | $this->$propName->set($k, $v); |
||
| 96 | } |
||
| 97 | |||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | // If this property is an object, test whether they are compatible. Otherwise, test if PHP |
||
| 102 | // types are compatible |
||
| 103 | if (is_object($this->$propName)) |
||
| 104 | { |
||
| 105 | if (!($propValue instanceof $this->$propName)) |
||
| 106 | { |
||
| 107 | throw new InvalidArgumentException("Cannot replace property '" . $propName . "' of class '" . get_class($this->$propName) . "' with instance of '" . get_class($propValue) . "'"); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | else |
||
| 111 | { |
||
| 112 | // Test whether the PHP types are compatible |
||
| 113 | $oldType = gettype($this->$propName); |
||
| 114 | $newType = gettype($propValue); |
||
| 115 | |||
| 116 | // If the property is a boolean, we'll accept "true" and "false" as strings |
||
| 117 | if ($oldType === 'boolean') |
||
| 118 | { |
||
| 119 | if ($propValue === 'false') |
||
| 120 | { |
||
| 121 | $newType = 'boolean'; |
||
| 122 | $propValue = false; |
||
| 123 | } |
||
| 124 | elseif ($propValue === 'true') |
||
| 125 | { |
||
| 126 | $newType = 'boolean'; |
||
| 127 | $propValue = true; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($oldType !== $newType) |
||
| 132 | { |
||
| 133 | // Test whether the PHP type roundtrip is lossless |
||
| 134 | $tmp = $propValue; |
||
| 135 | settype($tmp, $oldType); |
||
| 136 | settype($tmp, $newType); |
||
| 137 | |||
| 138 | if ($tmp !== $propValue) |
||
| 139 | { |
||
| 140 | throw new InvalidArgumentException("Cannot replace property '" . $propName . "' of type " . $oldType . ' with value of type ' . $newType); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Finally, set the new value to the correct type |
||
| 144 | settype($propValue, $oldType); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->$propName = $propValue; |
||
| 149 | } |
||
| 150 | |||
| 200 | } |