Conditions | 10 |
Paths | 13 |
Total Lines | 29 |
Code Lines | 19 |
Lines | 18 |
Ratio | 62.07 % |
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 |
||
51 | static public function setFieldValue(&$object, $field, $value) |
||
52 | { |
||
53 | $reflectionClass = new ReflectionClass($object); |
||
54 | |||
55 | View Code Duplication | foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
|
56 | if (!$reflectionMethod->isStatic() |
||
57 | && $reflectionMethod->getNumberOfRequiredParameters() == 1 |
||
58 | && strcasecmp($reflectionMethod->getName(), "set$field") == 0) { |
||
59 | $reflectionMethod->invoke($object, $value); |
||
60 | return; |
||
61 | } |
||
62 | } |
||
63 | View Code Duplication | if ($reflectionClass->hasProperty($field)) { |
|
64 | $property = $reflectionClass->getProperty($field); |
||
65 | if (!$property->isStatic()) { |
||
66 | if (!$property->isPublic()) { |
||
67 | $property->setAccessible(true); |
||
68 | } |
||
69 | $property->setValue($object, $value); |
||
70 | return; |
||
71 | } |
||
72 | } |
||
73 | if (is_array($object) || $object instanceof \ArrayAccess) { |
||
74 | $object[$field] = $value; |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | throw new ReflectionException("could not set field '$field' from object"); |
||
79 | } |
||
80 | } |
||
81 |