| Conditions | 16 |
| Paths | 222 |
| Total Lines | 97 |
| Code Lines | 70 |
| 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 |
||
| 205 | public function api($var) : array |
||
| 206 | { |
||
| 207 | if (is_object($var)) { |
||
| 208 | $class = get_class($var); |
||
| 209 | $obj = $var; |
||
| 210 | } else { |
||
| 211 | if (!class_exists($var)) { |
||
| 212 | throw new \Exception('Class ['.$var.'] doesn\'t exist'); |
||
| 213 | } |
||
| 214 | $class = $var; |
||
| 215 | try { |
||
| 216 | $obj = new $class(); |
||
| 217 | } catch (\Exception $e) { |
||
| 218 | throw new \Exception('Debug::api could not instantiate ['.$var.'], send it an object.'); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | $reflection = new \ReflectionObject($obj); |
||
| 222 | $properties = array(); |
||
| 223 | foreach (array( |
||
| 224 | 'public' => \ReflectionProperty::IS_PUBLIC, |
||
| 225 | 'protected' => \ReflectionProperty::IS_PROTECTED, |
||
| 226 | 'private' => \ReflectionProperty::IS_PRIVATE |
||
| 227 | ) as $access => $rule) { |
||
| 228 | $vars = $reflection->getProperties($rule); |
||
| 229 | foreach ($vars as $refProp) { |
||
| 230 | $property = $refProp->getName(); |
||
| 231 | $refProp->setAccessible(true); |
||
| 232 | $value = $refProp->getValue($obj); |
||
| 233 | $type = gettype($value); |
||
| 234 | if (is_object($value)) { |
||
| 235 | $value = get_class($value); |
||
| 236 | } elseif (is_array($value)) { |
||
| 237 | $value = 'array['.count($value).']'; |
||
| 238 | } |
||
| 239 | |||
| 240 | $properties[$access][$property] = compact('value', 'type'); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | $constants = $reflection->getConstants(); |
||
| 244 | |||
| 245 | $methods = array(); |
||
| 246 | |||
| 247 | foreach (array( |
||
| 248 | 'public' => \ReflectionMethod::IS_PUBLIC, |
||
| 249 | 'protected' => \ReflectionMethod::IS_PROTECTED, |
||
| 250 | 'private' => \ReflectionMethod::IS_PRIVATE |
||
| 251 | ) as $access => $rule) { |
||
| 252 | $refMethods = $reflection->getMethods($rule); |
||
| 253 | foreach ($refMethods as $refMethod) { |
||
| 254 | $refParams = $refMethod->getParameters(); |
||
| 255 | $params = array(); |
||
| 256 | foreach ($refParams as $refParam) { |
||
| 257 | $params[] = $refParam->getName(); |
||
| 258 | }/* |
||
| 259 | $required = $refMethod->getNumberOfRequiredParameters(); |
||
| 260 | $requiredParams = array(); |
||
| 261 | for ($i=0;$i<$required;$i++) { |
||
| 262 | $requiredParams[] = array_shift($params); |
||
| 263 | }*/ |
||
| 264 | $method_name = $refMethod->getName(); |
||
| 265 | |||
| 266 | $string = $access .' function '.$method_name.'('; |
||
| 267 | $paramString = ''; |
||
| 268 | foreach ($params as $p) { |
||
| 269 | $paramString .= '$'.$p.', '; |
||
| 270 | } |
||
| 271 | $paramString = substr($paramString,0,-2); |
||
| 272 | $string .= $paramString; |
||
| 273 | $string .= ')'; |
||
| 274 | |||
| 275 | $comment = $refMethod->getDocComment(); |
||
| 276 | $comment = trim(preg_replace('/^(\s*\/\*\*|\s*\*{1,2}\/|\s*\* ?)/m', '', $comment)); |
||
| 277 | $comment = str_replace("\r\n", "\n", $comment); |
||
| 278 | $commentParts = explode('@', $comment); |
||
| 279 | $description = array_shift($commentParts); |
||
| 280 | $tags = array(); |
||
| 281 | foreach ($commentParts as $part) { |
||
| 282 | $tagArr = explode(' ', $part, 2); |
||
| 283 | if ($tagArr[0] == 'param') { |
||
| 284 | $paramArr = preg_split("/[\s\t]+/", $tagArr[1]); |
||
| 285 | $type = array_shift($paramArr); |
||
| 286 | if (empty($type)) $type = array_shift($paramArr); |
||
| 287 | $name = array_shift($paramArr); |
||
| 288 | $info = implode(' ', $paramArr); |
||
| 289 | $tags['param'][$name] = compact('type', 'info'); |
||
| 290 | } else { |
||
| 291 | $tags[$tagArr[0]] = isset($tagArr[1])?$tagArr[1]:''; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | |||
| 296 | $methods[$access][$string] = compact('description', 'tags'); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | return compact('properties', 'constants' ,'methods'); |
||
| 301 | } |
||
| 302 | |||
| 304 |