Conditions | 14 |
Paths | 33 |
Total Lines | 154 |
Code Lines | 35 |
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 |
||
66 | public function parse(array $args): array |
||
67 | { |
||
68 | // Init an array of Arguments |
||
69 | $arguments = array(); |
||
70 | |||
71 | if(count($args) == 0) |
||
72 | { |
||
73 | // Nothing to parse |
||
74 | return $arguments; |
||
75 | } |
||
76 | |||
77 | // Get all Rules from Langugage |
||
78 | $rules = $this->language->rules(); |
||
79 | |||
80 | // Get all Parameters from ParameterCollection |
||
81 | $params = $this->parameterCollection->all(); |
||
82 | |||
83 | if( count($rules) == 0 ) |
||
84 | { |
||
85 | throw new ArghException(__CLASS__ . ': Language needs at least one rule to parse arguments.'); |
||
86 | } |
||
87 | |||
88 | if( count($params) == 0 ) |
||
89 | { |
||
90 | throw new ArghException(__CLASS__ . ': ParameterCollection needs at least one parameter to parse arguments.'); |
||
91 | } |
||
92 | |||
93 | // As parsing progresses, args will be divided into 2-sides (Left-and-Right) |
||
94 | // The Left-Hand-Side will contain args to attempt matching with rules |
||
95 | // The Right-Hand-Side will save args that didn't match in previous iterations (to be checked again later) |
||
96 | |||
97 | do |
||
98 | { |
||
99 | // Reset temporary variables |
||
100 | $argsL = array(); |
||
|
|||
101 | $argsR = array(); |
||
102 | $argsS = ""; |
||
103 | |||
104 | // Copy remaining $args to $argsL (left-hand-side array) |
||
105 | $argsL = array_merge($args); |
||
106 | |||
107 | do |
||
108 | { |
||
109 | // Combine $argsL elements into a single string, for matching against rules |
||
110 | $argsS = implode(' ', $argsL); |
||
111 | |||
112 | // |
||
113 | // DEBUG: Show detailed contents of each variable |
||
114 | // |
||
115 | |||
116 | /* |
||
117 | echo "\n\n"; |
||
118 | echo implode(' ', $argsL) . " | " . implode(' ', $argsR) . "\n"; |
||
119 | |||
120 | for($i=0; $i<count($argsL); $i++) |
||
121 | { |
||
122 | for($j=0; $j<strlen($argsL[$i]); $j++) |
||
123 | { |
||
124 | echo $i; |
||
125 | } |
||
126 | echo " "; |
||
127 | } |
||
128 | echo "| "; |
||
129 | for($i=0; $i<count($argsR); $i++) |
||
130 | { |
||
131 | for($j=0; $j<strlen($argsR[$i]); $j++) |
||
132 | { |
||
133 | echo $i; |
||
134 | } |
||
135 | echo " "; |
||
136 | } |
||
137 | echo "\n\n"; |
||
138 | |||
139 | echo "\nDEBUG: Considering: " . $argsS . " ... \n\n"; |
||
140 | */ |
||
141 | |||
142 | |||
143 | // |
||
144 | // END DEBUG |
||
145 | // |
||
146 | |||
147 | foreach($rules as $rule) |
||
148 | { |
||
149 | //echo "DEBUG: Checking for match with rule: " . $rule->name() . " (" . $rule->syntax() . ")" . "\n"; |
||
150 | |||
151 | $tokens = array(); // init array to capture matching tokens from Rule->match() |
||
152 | |||
153 | if( $rule->match($argsS, $tokens) ) |
||
154 | { |
||
155 | // Count the number of arguments that were matched |
||
156 | $count = count($argsL); |
||
157 | |||
158 | //echo "* MATCHED $count \$argv elements *\n"; |
||
159 | |||
160 | // Empty $argsL; prevent this inner foreach loop from continuing |
||
161 | for($i=0; $i<$count; $i++) array_shift($argsL); |
||
162 | |||
163 | // Remove (shift) matching elements from $args |
||
164 | // These arguments have been consumed by the parser and are no longer needed |
||
165 | for($i=0; $i<$count; $i++) array_shift($args); |
||
166 | |||
167 | // |
||
168 | // Try yielding Arguments from this Rule |
||
169 | // If this Rule does not yield any Arguments, continue checking the next Rule |
||
170 | // |
||
171 | |||
172 | $yield = $this->yieldArgumentsFromRule($rule, $tokens); |
||
173 | |||
174 | if( count($yield) > 0 ) |
||
175 | { |
||
176 | //? TODO: Validate Arguments before adding them to the Arguments array? |
||
177 | |||
178 | // Add the new Arguments yielded from this Rule |
||
179 | foreach($yield as $y) $arguments[] = $y; |
||
180 | |||
181 | // !IMPORTANT! Stop checking Rules |
||
182 | break; |
||
183 | |||
184 | } // END: if(count($argument) > 0) |
||
185 | else |
||
186 | { |
||
187 | // This Rule did not create any Arguments, keep checking with next Rule |
||
188 | } |
||
189 | |||
190 | } // END: if( preg_match($rule->syntax, $args[$i], $matches) ) |
||
191 | |||
192 | } // END: foreach($rules as $rule) |
||
193 | |||
194 | if( count($tokens) == 0 ) |
||
195 | { |
||
196 | // $argsS did NOT match any rules |
||
197 | |||
198 | // Pop last element off of $argsL |
||
199 | $arg = array_pop($argsL); |
||
200 | |||
201 | // Prepend popped elemented to beginning of $argsR |
||
202 | array_unshift($argsR, $arg); |
||
203 | |||
204 | if( count($argsL) == 0 ) |
||
205 | { |
||
206 | // There was no match, and there are no arguments left to pop from $argsL |
||
207 | throw new ArghException(__METHOD__ . ': Syntax Error: ' . $argsS); |
||
208 | } |
||
209 | |||
210 | } // END: if( count($tokens) == 0 ) |
||
211 | |||
212 | } // END do |
||
213 | while( count($argsL) > 0 ); |
||
214 | |||
215 | } // END: do |
||
216 | while( count($args) > 0 ); |
||
217 | |||
218 | // Return Arguments array |
||
219 | return $arguments; |
||
220 | |||
405 | } |