| Conditions | 12 |
| Paths | 48 |
| Total Lines | 45 |
| 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 |
||
| 53 | public function toObjectValue($value, $type, \ReflectionProperty $property, $object) |
||
| 54 | { |
||
| 55 | $className = null; |
||
| 56 | $context = new \ReflectionClass($property->class); |
||
| 57 | |||
| 58 | //try to get the class from use statements in the class file |
||
| 59 | if ($className === null) { |
||
| 60 | $classContent = file_get_contents($context->getFileName()); |
||
| 61 | |||
| 62 | $regExps = [ |
||
| 63 | "/use\s+([\w\\\]+$type);/", //use NameSpace\ClassName; |
||
| 64 | "/use\s+([\w\\\]+)\s+as\s+$type/", //use NameSpace\ClassName as ClassAlias; |
||
| 65 | ]; |
||
| 66 | foreach ($regExps as $regExp) { |
||
| 67 | if ($matchClass = $this->extractClass($regExp, $classContent)) { |
||
| 68 | $className = $matchClass; |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | //use the same namespace as class container |
||
| 75 | if ($className === null && class_exists($context->getNamespaceName().'\\'.$type)) { |
||
| 76 | $className = $context->getNamespaceName().'\\'.$type; |
||
| 77 | } |
||
| 78 | |||
| 79 | //use the type as class |
||
| 80 | if (class_exists($type)) { |
||
| 81 | $className = $type; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (is_array($value) && $className !== null && class_exists($className) && $this->array2Object) { |
||
| 85 | $property->setAccessible(true); |
||
| 86 | $currentValue = $property->getValue($object); |
||
| 87 | if (is_object($currentValue)) { |
||
| 88 | $this->array2Object->populate($currentValue, $value); |
||
| 89 | |||
| 90 | return $currentValue; |
||
| 91 | } else { |
||
| 92 | return $this->array2Object->createObject($className, $value); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return $value; |
||
| 97 | } |
||
| 98 | |||
| 127 |