Conditions | 23 |
Paths | 25 |
Total Lines | 105 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
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 |
||
61 | public function getFunctions(array $constants = []) |
||
62 | { |
||
63 | $count = count($this->tokens); |
||
64 | /* @var ParsedFunction[] $bufferFunctions */ |
||
65 | $bufferFunctions = []; |
||
66 | /* @var ParsedComment[] $bufferComments */ |
||
67 | $bufferComments = []; |
||
68 | /* @var array $functions */ |
||
69 | $functions = []; |
||
70 | |||
71 | for ($k = 0; $k < $count; ++$k) { |
||
72 | $value = $this->tokens[$k]; |
||
73 | |||
74 | if (is_string($value)) { |
||
75 | if (isset($bufferFunctions[0])) { |
||
76 | switch ($value) { |
||
77 | case ',': |
||
78 | $bufferFunctions[0]->nextArgument(); |
||
79 | break; |
||
80 | case ')': |
||
81 | $functions[] = array_shift($bufferFunctions)->close(); |
||
82 | break; |
||
83 | case '.': |
||
84 | break; |
||
85 | default: |
||
86 | $bufferFunctions[0]->stopArgument(); |
||
87 | break; |
||
88 | } |
||
89 | } |
||
90 | continue; |
||
91 | } |
||
92 | |||
93 | switch ($value[0]) { |
||
94 | case T_CONSTANT_ENCAPSED_STRING: |
||
95 | //add an argument to the current function |
||
96 | if (isset($bufferFunctions[0])) { |
||
97 | $bufferFunctions[0]->addArgumentChunk(PhpCode::convertString($value[1])); |
||
98 | } |
||
99 | break; |
||
100 | |||
101 | case T_STRING: |
||
102 | if (isset($bufferFunctions[0])) { |
||
103 | if (isset($constants[$value[1]])) { |
||
104 | $bufferFunctions[0]->addArgumentChunk($constants[$value[1]]); |
||
105 | break; |
||
106 | } |
||
107 | |||
108 | if (strtolower($value[1]) === 'null') { |
||
109 | $bufferFunctions[0]->addArgumentChunk(null); |
||
110 | break; |
||
111 | } |
||
112 | |||
113 | $bufferFunctions[0]->stopArgument(); |
||
114 | } |
||
115 | |||
116 | //new function found |
||
117 | for ($j = $k + 1; $j < $count; ++$j) { |
||
118 | $nextToken = $this->tokens[$j]; |
||
119 | |||
120 | if (is_array($nextToken) && $nextToken[0] === T_COMMENT) { |
||
121 | continue; |
||
122 | } |
||
123 | |||
124 | if ($nextToken === '(') { |
||
125 | $newFunction = new ParsedFunction($value[1], $value[2]); |
||
126 | |||
127 | // add comment that was on the line before. |
||
128 | if (isset($bufferComments[0])) { |
||
129 | $comment = $bufferComments[0]; |
||
130 | |||
131 | if ($comment->isRelatedWith($newFunction)) { |
||
132 | $newFunction->addComment($comment->getComment()); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | array_unshift($bufferFunctions, $newFunction); |
||
137 | $k = $j; |
||
138 | } |
||
139 | break; |
||
140 | } |
||
141 | break; |
||
142 | |||
143 | case T_COMMENT: |
||
144 | $comment = $this->parsePhpComment($value[1], $value[2]); |
||
145 | |||
146 | if ($comment) { |
||
147 | array_unshift($bufferComments, $comment); |
||
148 | |||
149 | // The comment is inside the function call. |
||
150 | if (isset($bufferFunctions[0])) { |
||
151 | $bufferFunctions[0]->addComment($comment->getComment()); |
||
152 | } |
||
153 | } |
||
154 | break; |
||
155 | |||
156 | default: |
||
157 | if (isset($bufferFunctions[0])) { |
||
158 | $bufferFunctions[0]->stopArgument(); |
||
159 | } |
||
160 | break; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return $functions; |
||
165 | } |
||
166 | |||
195 |