| Conditions | 32 |
| Paths | 7 |
| Total Lines | 152 |
| Code Lines | 89 |
| 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 |
||
| 24 | protected function _parse(&$variable) |
||
| 25 | { |
||
| 26 | if (!is_object($variable)) { |
||
| 27 | return false; |
||
| 28 | } |
||
| 29 | |||
| 30 | $className = get_class($variable); |
||
| 31 | |||
| 32 | # assuming class definition will not change inside one request |
||
| 33 | if (!isset(self::$cache[$className])) { |
||
| 34 | $reflection = new \ReflectionClass($variable); |
||
| 35 | |||
| 36 | $public = $private = $protected = array(); |
||
| 37 | |||
| 38 | // Class methods |
||
| 39 | foreach ($reflection->getMethods() as $method) { |
||
| 40 | $params = array(); |
||
| 41 | |||
| 42 | // Access type |
||
| 43 | $access = implode(' ', \Reflection::getModifierNames($method->getModifiers())); |
||
| 44 | |||
| 45 | // Method parameters |
||
| 46 | foreach ($method->getParameters() as $param) { |
||
| 47 | $paramString = ''; |
||
| 48 | |||
| 49 | if ($param->isArray()) { |
||
| 50 | $paramString .= 'array '; |
||
| 51 | } else { |
||
| 52 | try { |
||
| 53 | $paramClassName = $param->getClass(); |
||
| 54 | if ($paramClassName) { |
||
| 55 | $paramString .= $paramClassName->name . ' '; |
||
| 56 | } |
||
| 57 | } catch (\ReflectionException $e) { |
||
| 58 | preg_match('/\[\s\<\w+?>\s([\w]+)/', (string)$param, $matches); |
||
| 59 | $paramClassName = isset($matches[1]) ? $matches[1] : ''; |
||
| 60 | |||
| 61 | $paramString .= ' UNDEFINED CLASS (' . $paramClassName . ') '; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $paramString .= ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName(); |
||
| 66 | |||
| 67 | if ($param->isDefaultValueAvailable()) { |
||
| 68 | if (is_array($param->getDefaultValue())) { |
||
| 69 | $arrayValues = array(); |
||
| 70 | foreach ($param->getDefaultValue() as $key => $value) { |
||
| 71 | $arrayValues[] = $key . ' => ' . $value; |
||
| 72 | } |
||
| 73 | |||
| 74 | $defaultValue = 'array(' . implode(', ', $arrayValues) . ')'; |
||
| 75 | } elseif ($param->getDefaultValue() === null) { |
||
| 76 | $defaultValue = 'NULL'; |
||
| 77 | } elseif ($param->getDefaultValue() === false) { |
||
| 78 | $defaultValue = 'false'; |
||
| 79 | } elseif ($param->getDefaultValue() === true) { |
||
| 80 | $defaultValue = 'true'; |
||
| 81 | } elseif ($param->getDefaultValue() === '') { |
||
| 82 | $defaultValue = '""'; |
||
| 83 | } else { |
||
| 84 | $defaultValue = $param->getDefaultValue(); |
||
| 85 | } |
||
| 86 | |||
| 87 | $paramString .= ' = ' . $defaultValue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $params[] = $paramString; |
||
| 91 | } |
||
| 92 | |||
| 93 | $output = new KintVariableData; |
||
| 94 | |||
| 95 | // Simple DocBlock parser, look for @return |
||
| 96 | $docBlock = $method->getDocComment(); |
||
| 97 | if ($docBlock) { |
||
| 98 | $matches = array(); |
||
| 99 | if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $docBlock, $matches)) { |
||
| 100 | $lines = array_combine($matches[1], $matches[2]); |
||
| 101 | if (isset($lines['return'])) { |
||
| 102 | $output->operator = '->'; |
||
| 103 | # since we're outputting code, assumption that the string is utf8 is most likely correct |
||
| 104 | # and saves resources |
||
| 105 | $output->type = self::escape($lines['return'], 'UTF-8'); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | $output->name = ($method->returnsReference() ? '&' : '') . $method->getName() . '(' |
||
| 111 | . implode(', ', $params) . ')'; |
||
| 112 | $output->access = $access; |
||
| 113 | |||
| 114 | if (is_string($docBlock)) { |
||
| 115 | $lines = array(); |
||
| 116 | foreach (explode("\n", $docBlock) as $line) { |
||
| 117 | $line = trim($line); |
||
| 118 | |||
| 119 | if (in_array($line, array('/**', '/*', '*/'), true)) { |
||
| 120 | continue; |
||
| 121 | } elseif (strpos($line, '*') === 0) { |
||
| 122 | $line = substr($line, 1); |
||
| 123 | } |
||
| 124 | |||
| 125 | $lines[] = self::escape(trim($line), 'UTF-8'); |
||
|
|
|||
| 126 | } |
||
| 127 | |||
| 128 | $output->extendedValue = implode("\n", $lines) . "\n\n"; |
||
| 129 | } |
||
| 130 | |||
| 131 | $declaringClass = $method->getDeclaringClass(); |
||
| 132 | $declaringClassName = $declaringClass->getName(); |
||
| 133 | |||
| 134 | if ($declaringClassName !== $className) { |
||
| 135 | /** @noinspection PhpToStringImplementationInspection */ |
||
| 136 | $output->extendedValue .= "<small>Inherited from <i>{$declaringClassName}</i></small>\n"; |
||
| 137 | } |
||
| 138 | |||
| 139 | $fileName = Kint::shortenPath($method->getFileName()) . ':' . $method->getStartLine(); |
||
| 140 | /** @noinspection PhpToStringImplementationInspection */ |
||
| 141 | $output->extendedValue .= "<small>Defined in {$fileName}</small>"; |
||
| 142 | |||
| 143 | $sortName = $access . $method->getName(); |
||
| 144 | |||
| 145 | if ($method->isPrivate()) { |
||
| 146 | $private[$sortName] = $output; |
||
| 147 | } elseif ($method->isProtected()) { |
||
| 148 | $protected[$sortName] = $output; |
||
| 149 | } else { |
||
| 150 | $public[$sortName] = $output; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if (!$private && !$protected && !$public) { |
||
| 155 | self::$cache[$className] = false; |
||
| 156 | } |
||
| 157 | |||
| 158 | ksort($public); |
||
| 159 | ksort($protected); |
||
| 160 | ksort($private); |
||
| 161 | |||
| 162 | /** @noinspection AdditionOperationOnArraysInspection */ |
||
| 163 | self::$cache[$className] = $public + $protected + $private; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (count(self::$cache[$className]) === 0) { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->value = self::$cache[$className]; |
||
| 171 | $this->type = 'Available methods'; |
||
| 172 | $this->size = count(self::$cache[$className]); |
||
| 173 | |||
| 174 | return true; |
||
| 175 | } |
||
| 176 | } |