Conditions | 15 |
Paths | 8 |
Total Lines | 80 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
95 | private function parseGrammar(Buffer $src, int $offset): ?array |
||
96 | { |
||
97 | $this->tokens = [ |
||
98 | new Token(self::TYPE_OPEN_TAG, $offset, '$' . $src->next()->char) |
||
99 | ]; |
||
100 | |||
101 | while ($n = $src->next()) { |
||
102 | if (!$n instanceof Byte) { |
||
103 | // no other grammars are allowed |
||
104 | return null; |
||
105 | } |
||
106 | |||
107 | switch ($n->char) { |
||
108 | case '"': |
||
109 | case "'": |
||
110 | if ($this->default === null) { |
||
111 | // " and ' not allowed in names |
||
112 | return null; |
||
113 | } |
||
114 | |||
115 | $this->default[] = $n; |
||
116 | while ($nn = $src->next()) { |
||
117 | $this->default[] = $nn; |
||
118 | if ($nn instanceof Byte && $nn->char === $n->char) { |
||
119 | break; |
||
120 | } |
||
121 | } |
||
122 | break; |
||
123 | |||
124 | case '}': |
||
125 | $this->flushName(); |
||
126 | $this->flushDefault(); |
||
127 | |||
128 | $this->tokens[] = new Token( |
||
129 | self::TYPE_CLOSE_TAG, |
||
130 | $n->offset, |
||
131 | $n->char |
||
132 | ); |
||
133 | |||
134 | break 2; |
||
135 | |||
136 | case '|': |
||
137 | $this->flushName(); |
||
138 | $this->flushDefault(); |
||
139 | |||
140 | $this->tokens[] = new Token( |
||
141 | self::TYPE_SEPARATOR, |
||
142 | $n->offset, |
||
143 | $n->char |
||
144 | ); |
||
145 | |||
146 | $this->default = []; |
||
147 | |||
148 | break; |
||
149 | |||
150 | default: |
||
151 | if ($this->default !== null) { |
||
152 | // default allows spaces |
||
153 | $this->default[] = $n; |
||
154 | break; |
||
155 | } |
||
156 | |||
157 | if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
||
158 | break; |
||
159 | } |
||
160 | |||
161 | if (preg_match(self::REGEXP_KEYWORD, $n->char)) { |
||
162 | $this->name[] = $n; |
||
163 | break; |
||
164 | } |
||
165 | |||
166 | return null; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if (!$this->isValid()) { |
||
171 | return null; |
||
172 | } |
||
173 | |||
174 | return $this->tokens; |
||
175 | } |
||
238 |