Conditions | 18 |
Paths | 20 |
Total Lines | 78 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
38 | public function bulkDeserialise(ResourceEntityType $entityType, ODataEntry $payload) |
||
39 | { |
||
40 | if (!isset($payload->type)) { |
||
41 | $msg = 'ODataEntry payload type not set'; |
||
42 | throw new InvalidArgumentException($msg); |
||
43 | } |
||
44 | |||
45 | /** @scrutinizer ignore-call */ |
||
46 | $payloadType = $payload->type->getTerm(); |
||
47 | $pay = explode('.', $payloadType); |
||
48 | $payloadType = $pay[count($pay) - 1]; |
||
49 | $actualType = $entityType->getName(); |
||
50 | |||
51 | if ($payloadType !== $actualType) { |
||
52 | $msg = 'Payload resource type does not match supplied resource type.'; |
||
53 | throw new InvalidArgumentException($msg); |
||
54 | } |
||
55 | |||
56 | if (!isset(self::$nonKeyPropertiesCache[$actualType])) { |
||
57 | $rawProp = $entityType->getAllProperties(); |
||
58 | $keyProp = $entityType->getKeyProperties(); |
||
59 | $keyNames = array_keys($keyProp); |
||
60 | $nonRelProp = []; |
||
61 | foreach ($rawProp as $prop) { |
||
62 | $propName = $prop->getName(); |
||
63 | if (!in_array($propName, $keyNames) && !($prop->getResourceType() instanceof ResourceEntityType)) { |
||
64 | $nonRelProp[] = $propName; |
||
65 | $nonRelProp[] = strtolower($propName); |
||
66 | } |
||
67 | } |
||
68 | self::$nonKeyPropertiesCache[$actualType] = $nonRelProp; |
||
69 | } |
||
70 | |||
71 | $nonRelProp = self::$nonKeyPropertiesCache[$actualType]; |
||
72 | |||
73 | // assemble data array |
||
74 | $data = []; |
||
75 | foreach ($payload->propertyContent as $propName => $propSpec) { |
||
76 | if (in_array($propName, $nonRelProp) || in_array(strtolower($propName), $nonRelProp)) { |
||
|
|||
77 | /** @var string $rawVal */ |
||
78 | $rawVal = $propSpec->getValue(); |
||
79 | $value = null; |
||
80 | switch ($propSpec->getTypeName()) { |
||
81 | case 'Edm.Boolean': |
||
82 | $rawVal = trim(strtolower(strval($rawVal))); |
||
83 | $value = 'true' == $rawVal; |
||
84 | break; |
||
85 | case 'Edm.DateTime': |
||
86 | $rawVal = trim(strval($rawVal)); |
||
87 | if (1 < strlen($rawVal)) { |
||
88 | $valLen = strlen($rawVal) - 6; |
||
89 | $offsetChek = $rawVal[$valLen]; |
||
90 | $timezone = new DateTimeZone('UTC'); |
||
91 | if (18 < $valLen && ('-' == $offsetChek || '+' == $offsetChek)) { |
||
92 | $rawTz = substr($rawVal, $valLen); |
||
93 | $rawVal = substr($rawVal, 0, $valLen); |
||
94 | $rawBitz = explode('.', $rawVal); |
||
95 | $rawVal = $rawBitz[0]; |
||
96 | $timezone = new DateTimeZone($rawTz); |
||
97 | } |
||
98 | $newValue = new DateTime($rawVal, $timezone); |
||
99 | // clamp assignable times to: |
||
100 | // after 1752, since OData DateTime epoch is apparently midnight 1 Jan 1753 |
||
101 | // before 10000, since OData has a Y10K problem |
||
102 | if (1752 < $newValue->format('Y') && 10000 > $newValue->format('Y')) { |
||
103 | $value = $newValue; |
||
104 | } |
||
105 | } |
||
106 | break; |
||
107 | default: |
||
108 | $value = trim(strval($rawVal)); |
||
109 | break; |
||
110 | } |
||
111 | $data[$propName] = $value; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $data; |
||
116 | } |
||
128 |