Conditions | 9 |
Paths | 14 |
Total Lines | 60 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
102 | public static function writeComplexValue( |
||
103 | ResourceType &$resourceType, |
||
104 | &$result, |
||
105 | $propertyName = null, |
||
106 | array &$instanceCollection = [] |
||
107 | ) { |
||
108 | if (!is_object($result)) { |
||
109 | throw new InvalidOperationException('Supplied $customObject must be an object'); |
||
110 | } |
||
111 | |||
112 | $count = count($instanceCollection); |
||
113 | for ($i = 0; $i < $count; ++$i) { |
||
114 | if ($instanceCollection[$i] === $result) { |
||
115 | throw new InvalidOperationException( |
||
116 | Messages::objectModelSerializerLoopsNotAllowedInComplexTypes($propertyName) |
||
117 | ); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | $instanceCollection[$count] = &$result; |
||
122 | |||
123 | $internalContent = new ODataPropertyContent(); |
||
124 | $resourceProperties = $resourceType->getAllProperties(); |
||
125 | // first up, handle primitive properties |
||
126 | foreach ($resourceProperties as $prop) { |
||
127 | $resourceKind = $prop->getKind(); |
||
128 | $propName = $prop->getName(); |
||
129 | $internalProperty = new ODataProperty(); |
||
130 | $internalProperty->name = $propName; |
||
131 | if (SerialiserUtilities::isMatchPrimitive($resourceKind)) { |
||
|
|||
132 | $iType = $prop->getInstanceType(); |
||
133 | if (!$iType instanceof IType) { |
||
134 | throw new InvalidOperationException(get_class($iType)); |
||
135 | } |
||
136 | $internalProperty->typeName = $iType->getFullTypeName(); |
||
137 | |||
138 | $rType = $prop->getResourceType()->getInstanceType(); |
||
139 | if (!$rType instanceof IType) { |
||
140 | throw new InvalidOperationException(get_class($rType)); |
||
141 | } |
||
142 | |||
143 | $internalProperty->value = SerialiserLowLevelWriters::primitiveToString($rType, $result->$propName); |
||
144 | |||
145 | $internalContent->properties[$propName] = $internalProperty; |
||
146 | } elseif (ResourcePropertyKind::COMPLEX_TYPE == $resourceKind) { |
||
147 | $rType = $prop->getResourceType(); |
||
148 | $internalProperty->typeName = $rType->getFullName(); |
||
149 | $internalProperty->value = SerialiserLowLevelWriters::writeComplexValue( |
||
150 | $rType, |
||
151 | $result->$propName, |
||
152 | $propName, |
||
153 | $instanceCollection |
||
154 | ); |
||
155 | |||
156 | $internalContent->properties[$propName] = $internalProperty; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | unset($instanceCollection[$count]); |
||
161 | return $internalContent; |
||
162 | } |
||
198 |