Conditions | 10 |
Paths | 17 |
Total Lines | 34 |
Code Lines | 23 |
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 |
||
13 | public static function copy($obj1, $obj2) |
||
14 | { |
||
15 | if (!is_object($obj1)) { |
||
16 | throw new \Exception('$obj1 must be an object, not '.gettype($obj1)); |
||
17 | } |
||
18 | if (!is_object($obj2)) { |
||
19 | throw new \Exception('$obj2 must be an object, not '.gettype($obj2)); |
||
20 | } |
||
21 | $reflection1 = new \ReflectionObject($obj1); |
||
22 | $reflection2 = new \ReflectionObject($obj2); |
||
23 | |||
24 | $methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC); |
||
25 | $publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC); |
||
26 | foreach ($methods as $method) { |
||
27 | $methodName = $method->getName(); |
||
|
|||
28 | if (0 === strpos($methodName, 'get')) { |
||
29 | $getMethod = $methodName; |
||
30 | $setMethod = $methodName; |
||
31 | $setMethod[0] = 's'; |
||
32 | if ($reflection2->hasMethod($setMethod)) { |
||
33 | $value = $obj1->$getMethod(); |
||
34 | if (null !== $value) { |
||
35 | $obj2->$setMethod($value); |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | foreach ($publicVars as $property) { |
||
41 | $propertyName = $property->getName(); |
||
42 | if ($reflection2->hasPropery($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) { |
||
43 | $obj2->$propertyName = $obj1->$propertyName; |
||
44 | } |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 |