| Conditions | 27 |
| Paths | 15 |
| Total Lines | 87 |
| Code Lines | 56 |
| 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 |
||
| 97 | public function getPropertiesGraceful($data) |
||
| 98 | { |
||
| 99 | if (!empty($data)) { |
||
| 100 | $properties = []; |
||
| 101 | $attributes = []; |
||
| 102 | $instances = []; |
||
| 103 | foreach ($data as $did => $datum) { |
||
| 104 | if (isset($datum['type'])) { |
||
| 105 | $instance = new Instance($datum['type']); |
||
| 106 | $propertiesNodes = $instance->getPropertiesNodes(); |
||
| 107 | $instances[$did] = $propertiesNodes; |
||
| 108 | |||
| 109 | if (!empty($datum['data'])) { |
||
| 110 | foreach ($datum['data'] as $name) { |
||
| 111 | list($sids, $pids) = $instance->getSidPidByName($name); |
||
| 112 | |||
| 113 | if (!$sids || !$pids) { |
||
| 114 | throw new ApiErrorException('Invalid property! did:'.$did.',name: '.$name); |
||
| 115 | } |
||
| 116 | |||
| 117 | foreach ($sids as $sindex => $sid) { |
||
| 118 | $property = $propertiesNodes[($sid.'.'.$pids[$sindex])]; |
||
| 119 | |||
| 120 | if (!$property->canRead()) { |
||
| 121 | throw new ApiErrorException( |
||
| 122 | 'The property does\'t has the read access! did:'.$did.',name: '.$name |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | $properties[] = $did.'.'.$sid.'.'.$pids[$sindex]; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | foreach ($propertiesNodes as $property) { |
||
| 131 | $name = $property->getUrn()->getName(); |
||
| 132 | list($sids, $pids) = $instance->getSidPidByName($name); |
||
| 133 | |||
| 134 | if (!$sids || !$pids) { |
||
| 135 | throw new ApiErrorException('Invalid property! did:'.$did.',name: '.$name); |
||
| 136 | } |
||
| 137 | |||
| 138 | foreach ($sids as $sindex => $sid) { |
||
| 139 | $property = $propertiesNodes[($sid.'.'.$pids[$sindex])]; |
||
| 140 | |||
| 141 | if ($property->canRead()) { |
||
| 142 | $properties[] = $did.'.'.$sid.'.'.$pids[$sindex]; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } else { |
||
| 148 | throw new ApiErrorException('Properties data and device type required'); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $response = $this->properties(array_unique($properties)); |
||
| 153 | if (isset($response['properties']) && !empty($response['properties'])) { |
||
| 154 | foreach ($response['properties'] as $res) { |
||
| 155 | $pidArr = explode('.', $res['pid']); |
||
| 156 | if (isset($res['value']) // 是否获取到了值 |
||
| 157 | && isset($res['status']) // 是否有返回状态 |
||
| 158 | && $res['status'] == 0 // 是否正常返回 |
||
| 159 | && isset($pidArr[0]) // did |
||
| 160 | && isset($pidArr[1]) // sid |
||
| 161 | && isset($pidArr[2]) // pid |
||
| 162 | && isset($instances[$pidArr[0]][($pidArr[1].'.'.$pidArr[2])])) { |
||
| 163 | $attributeName = $instances[$pidArr[0]][($pidArr[1].'.'.$pidArr[2])]->getUrn()->getName(); |
||
| 164 | |||
| 165 | if (isset($attributes[$pidArr[0]][$attributeName])) { |
||
| 166 | if (is_array($attributes[$pidArr[0]][$attributeName])) { |
||
| 167 | $attributes[$pidArr[0]][$attributeName][] = $res['value']; |
||
| 168 | } else { |
||
| 169 | $attributes[$pidArr[0]][$attributeName] = [ |
||
| 170 | $attributes[$pidArr[0]][$attributeName], |
||
| 171 | $res['value'], |
||
| 172 | ]; |
||
| 173 | } |
||
| 174 | } else { |
||
| 175 | $attributes[$pidArr[0]][$attributeName] = $res['value']; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $attributes; |
||
| 182 | } else { |
||
| 183 | throw new ApiErrorException('devices data required'); |
||
| 184 | } |
||
| 371 |