Conditions | 16 |
Paths | 1 |
Total Lines | 87 |
Code Lines | 49 |
Lines | 12 |
Ratio | 13.79 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
81 | public function tokenize($code) |
||
82 | { |
||
83 | // Initial state of parser |
||
84 | $baseIter = tail(function ($rest, $acc) use (&$baseIter, &$symbolIter, &$stringIter, &$commentIter) { |
||
85 | if (sizeof($rest) == 0) { |
||
86 | return $acc; |
||
87 | } |
||
88 | list ($head, $tail) = toHeadTail($rest); |
||
89 | switch ($head) { |
||
90 | // We got '(', so we just add it to accumulator. |
||
91 | case self::TOKEN_OPEN_BRACKET: |
||
92 | return $baseIter($tail, append($acc, new Tokens\OpenBracketToken)); |
||
93 | // We got ')', and doing the same as in previous case. |
||
94 | case self::TOKEN_CLOSE_BRACKET: |
||
95 | return $baseIter($tail, append($acc, new Tokens\CloseBracketToken)); |
||
96 | // We got '"'. That means that we're in the beginning of the string. |
||
97 | // So we switch our state to stringIter. |
||
98 | case self::TOKEN_DOUBLE_QUOTE: |
||
99 | return $stringIter($tail, '', $acc); |
||
100 | // We got ';'. And that means that comment is starting here. So we |
||
101 | // change our state to commentIter. |
||
102 | case self::TOKEN_SEMICOLON: |
||
103 | return $commentIter($tail, '', $acc); |
||
104 | default: |
||
105 | // If current char is a delimiter, we ignore it. |
||
106 | if ($this->isDelimiter($head)) { |
||
107 | return $baseIter($tail, $acc); |
||
108 | } |
||
109 | // In all other cases we interpret current char as first char |
||
110 | // of symbol and change our state to symbolIter. |
||
111 | return $symbolIter($tail, $head, $acc); |
||
112 | } |
||
113 | }); |
||
114 | |||
115 | // State when parser parses any symbol |
||
116 | $symbolIter = tail(function ($rest, $buffer, $acc) use (&$symbolIter, &$baseIter, &$delimiterIter) { |
||
117 | View Code Duplication | if (sizeof($rest) > 0) { |
|
118 | list ($head, $tail) = toHeadTail($rest); |
||
119 | if ($this->isSymbol($head)) { |
||
120 | return $symbolIter($tail, $buffer . $head, $acc); |
||
121 | } |
||
122 | } |
||
123 | if (is_numeric($buffer)) { |
||
124 | $symbolToken = new Tokens\NumericToken($buffer); |
||
125 | } else { |
||
126 | $symbolToken = new Tokens\IdentifierToken($buffer); |
||
127 | } |
||
128 | return $baseIter($rest, append($acc, $symbolToken)); |
||
129 | }); |
||
130 | |||
131 | // State when parser parses string |
||
132 | $stringIter = tail(function ($rest, $buffer, $acc) use (&$stringIter, &$baseIter, &$escapeIter) { |
||
133 | if (sizeof($rest) == 0) { |
||
134 | throw new TokenizerException("Unexpected end of string"); |
||
135 | } |
||
136 | list ($head, $tail) = toHeadTail($rest); |
||
137 | if ($head == self::TOKEN_DOUBLE_QUOTE) { |
||
138 | return $baseIter($tail, append($acc, new Tokens\StringToken($buffer))); |
||
139 | } |
||
140 | if ($head == Tokenizer::TOKEN_BACK_SLASH) { |
||
141 | return $escapeIter($tail, $buffer, $acc); |
||
142 | } |
||
143 | return $stringIter($tail, $buffer . $head, $acc); |
||
144 | }); |
||
145 | |||
146 | // State when parser parses escaped symbol |
||
147 | $escapeIter = tail(function ($rest, $buffer, $acc) use (&$stringIter) { |
||
148 | if (sizeof($rest) == 0) { |
||
149 | throw new TokenizerException("Unused escape character"); |
||
150 | } |
||
151 | list ($head, $tail) = toHeadTail($rest); |
||
152 | return $stringIter($tail, $buffer . $head, $acc); |
||
153 | }); |
||
154 | |||
155 | // State when parser ignores comments |
||
156 | $commentIter = function ($rest, $buffer, $acc) use (&$commentIter, &$baseIter) { |
||
157 | View Code Duplication | if (sizeof($rest) > 0) { |
|
158 | list ($head, $tail) = toHeadTail($rest); |
||
159 | if ($head != Tokenizer::TOKEN_NEW_LINE) { |
||
160 | return $commentIter($tail, $buffer . $head, $acc); |
||
161 | } |
||
162 | } |
||
163 | return $baseIter($rest, $acc); |
||
164 | }; |
||
165 | |||
166 | return $baseIter(toArray($code), []); |
||
167 | } |
||
168 | } |
||
169 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.