| Conditions | 10 |
| Paths | 28 |
| Total Lines | 44 |
| Code Lines | 27 |
| 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 |
||
| 43 | public function reflect($object, $method){ |
||
| 44 | $reflection = new \ReflectionMethod($object, $method); |
||
| 45 | $docs = $reflection->getDocComment(); |
||
| 46 | |||
| 47 | // extract everything prefixed by @ and first letter uppercase |
||
| 48 | preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)((?P<parameter>.*))?$/m', $docs, $matches); |
||
| 49 | foreach($matches['annotation'] as $key => $annontation) { |
||
| 50 | $annotationValue = $matches['parameter'][$key]; |
||
| 51 | if(isset($annotationValue[0]) && $annotationValue[0] === '(' && $annotationValue[strlen($annotationValue) - 1] === ')') { |
||
| 52 | $cutString = substr($annotationValue, 1, -1); |
||
| 53 | $cutString = str_replace(' ', '', $cutString); |
||
| 54 | $splittedArray = explode(',', $cutString); |
||
| 55 | foreach($splittedArray as $annotationValues) { |
||
| 56 | list($key, $value) = explode('=', $annotationValues); |
||
| 57 | $this->annotations[$annontation][$key] = $value; |
||
| 58 | } |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->annotations[$annontation] = [$annotationValue]; |
||
| 63 | } |
||
| 64 | |||
| 65 | // extract type parameter information |
||
| 66 | preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches); |
||
| 67 | $this->types = array_combine($matches['var'], $matches['type']); |
||
| 68 | |||
| 69 | foreach ($reflection->getParameters() as $param) { |
||
| 70 | // extract type information from PHP 7 scalar types and prefer them |
||
| 71 | // over phpdoc annotations |
||
| 72 | if (method_exists($param, 'getType')) { |
||
| 73 | $type = $param->getType(); |
||
| 74 | if ($type !== null) { |
||
| 75 | $this->types[$param->getName()] = (string) $type; |
||
|
|
|||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | if($param->isOptional()) { |
||
| 80 | $default = $param->getDefaultValue(); |
||
| 81 | } else { |
||
| 82 | $default = null; |
||
| 83 | } |
||
| 84 | $this->parameters[$param->name] = $default; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 134 |