Conditions | 10 |
Paths | 13 |
Total Lines | 26 |
Code Lines | 16 |
Lines | 16 |
Ratio | 61.54 % |
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 |
||
17 | static public function getFieldValue($object, $field) |
||
|
|||
18 | { |
||
19 | $reflectionClass = new ReflectionClass($object); |
||
20 | |||
21 | View Code Duplication | foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
|
22 | if (!$reflectionMethod->isStatic() |
||
23 | && $reflectionMethod->getNumberOfRequiredParameters() == 0 |
||
24 | && strcasecmp($reflectionMethod->getName(), "get$field") == 0) { |
||
25 | return $reflectionMethod->invoke($object); |
||
26 | } |
||
27 | } |
||
28 | View Code Duplication | if ($reflectionClass->hasProperty($field)) { |
|
29 | $property = $reflectionClass->getProperty($field); |
||
30 | if (!$property->isStatic()) { |
||
31 | if (!$property->isPublic()) { |
||
32 | $property->setAccessible(true); |
||
33 | } |
||
34 | return $property->getValue($object); |
||
35 | } |
||
36 | } |
||
37 | if (is_array($object) || $object instanceof \ArrayAccess) { |
||
38 | return $object[$field]; |
||
39 | } |
||
40 | |||
41 | throw new ReflectionException("could not extract field '$field' from object"); |
||
42 | } |
||
43 | |||
81 |