Conditions | 11 |
Paths | 28 |
Total Lines | 66 |
Code Lines | 28 |
Lines | 8 |
Ratio | 12.12 % |
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 |
||
39 | public static function getPhpDocOfMethod($sClassName, string $sMethodName) : array |
||
40 | { |
||
41 | $oReflectionClass = new \ReflectionClass($sClassName); |
||
42 | $oReflectionMethod = $oReflectionClass->getMethod($sMethodName); |
||
43 | $sCommentPhpDoc = $oReflectionMethod->getDocComment(); |
||
44 | |||
45 | /** |
||
46 | * parse this PhpDoc @cache(param=1,param=2) |
||
47 | */ |
||
48 | |||
49 | preg_match_all('/@([a-zA-Z0-9_-]+)[ \t]*\(([^\)]+)\)/', $sCommentPhpDoc, $aMatchs, PREG_SET_ORDER); |
||
50 | $aParams = array(); |
||
51 | |||
52 | foreach ($aMatchs as $aOneMatch) { |
||
|
|||
53 | |||
54 | $aParams[$aOneMatch[1]] = array(); |
||
55 | |||
56 | foreach (explode(',', $aOneMatch[2]) as $sValue) { |
||
57 | |||
58 | $aFinalExplode = explode('=', $sValue); |
||
59 | |||
60 | if (preg_match('/^".+"$/', $aFinalExplode[1]) || preg_match("/^'.+'$/", $aFinalExplode[1])) { |
||
61 | |||
62 | $aFinalExplode[1] = substr($aFinalExplode[1], 1); |
||
63 | $aFinalExplode[1] = substr($aFinalExplode[1], 0, -1); |
||
64 | } |
||
65 | |||
66 | $aParams[$aOneMatch[1]][$aFinalExplode[0]] = $aFinalExplode[1]; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * parse this PhpDoc @param string $title ... |
||
72 | */ |
||
73 | |||
74 | preg_match_all('/@([a-zA-Z0-9_-]+)[ \t]*([^\r\n]+)/', $sCommentPhpDoc, $aMatchs, PREG_SET_ORDER); |
||
75 | $aParams = array(); |
||
76 | |||
77 | foreach ($aMatchs as $aOneMatch) { |
||
78 | |||
79 | if ($aOneMatch[1] == 'param') { |
||
80 | |||
81 | if (!isset($aParams['param'])) { $aParams['param'] = array(); } |
||
82 | |||
83 | $iIndex = count($aParams['param']); |
||
84 | |||
85 | if (!isset($aParams['param'][$iIndex])) { $aParams['param'][$iIndex] = array(); } |
||
86 | |||
87 | View Code Duplication | foreach (explode(' ', $aOneMatch[2]) as $sValue) { |
|
88 | |||
89 | $aParams['param'][$iIndex][] = $sValue; |
||
90 | } |
||
91 | } |
||
92 | else { |
||
93 | |||
94 | $aParams[$aOneMatch[1]] = array(); |
||
95 | |||
96 | View Code Duplication | foreach (explode(' ', $aOneMatch[2]) as $sValue) { |
|
97 | |||
98 | $aParams[$aOneMatch[1]][] = $sValue; |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $aParams; |
||
104 | } |
||
105 | } |
||
106 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.