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