| Conditions | 23 |
| Paths | 81 |
| Total Lines | 109 |
| Code Lines | 71 |
| 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 |
||
| 156 | public static function fromString(string $serializedList): self |
||
| 157 | { |
||
| 158 | $list = new self(); |
||
| 159 | |||
| 160 | $index = 0; |
||
| 161 | $state = self::STATE_NAME; |
||
| 162 | $sequence = ''; |
||
| 163 | |||
| 164 | if ($serializedList === '') { |
||
| 165 | throw new \InvalidArgumentException('Cannot create list from empty string'); |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($serializedList[0] === '(' && $serializedList[-1] === ')') { |
||
| 169 | $serializedList = substr($serializedList, 1, -1); |
||
| 170 | } |
||
| 171 | |||
| 172 | while (isset($serializedList[$index])) { |
||
| 173 | $char = $serializedList[$index]; |
||
| 174 | $sequence .= $char; |
||
| 175 | |||
| 176 | switch ($char) { |
||
| 177 | case '[': |
||
| 178 | if ($state !== self::STATE_NAME) { |
||
| 179 | throw new \InvalidArgumentException('Invalid character [ found'); |
||
| 180 | } |
||
| 181 | |||
| 182 | $list = $list->withItem(new NameItem(substr($sequence, 0, -1))); |
||
| 183 | $sequence = '['; |
||
| 184 | $state = self::STATE_SECTION; |
||
| 185 | break; |
||
| 186 | case ']': |
||
| 187 | if ($state !== self::STATE_SECTION) { |
||
| 188 | throw new \InvalidArgumentException('Invalid character ] found'); |
||
| 189 | } |
||
| 190 | |||
| 191 | $list = $list->withItem( |
||
| 192 | new SectionItem($list->last(), SectionList::fromString($sequence)) |
||
| 193 | ); |
||
| 194 | |||
| 195 | $sequence = ''; |
||
| 196 | $state = self::STATE_NAME; |
||
| 197 | break; |
||
| 198 | case '<': |
||
| 199 | if ($state !== self::STATE_NAME) { |
||
| 200 | throw new \InvalidArgumentException('Invalid character < found'); |
||
| 201 | } |
||
| 202 | |||
| 203 | $state = self::STATE_PARTIAL; |
||
| 204 | break; |
||
| 205 | case '>': |
||
| 206 | if ($state !== self::STATE_PARTIAL) { |
||
| 207 | throw new \InvalidArgumentException('Invalid character > found'); |
||
| 208 | } |
||
| 209 | |||
| 210 | $list = $list->withItem( |
||
| 211 | new PartialItem($list->last(), Partial::fromString($sequence)) |
||
| 212 | ); |
||
| 213 | |||
| 214 | $sequence = ''; |
||
| 215 | $state = self::STATE_NAME; |
||
| 216 | break; |
||
| 217 | case '{': |
||
| 218 | if ($state !== self::STATE_NONE) { |
||
| 219 | throw new \InvalidArgumentException('Invalid character { found'); |
||
| 220 | } |
||
| 221 | |||
| 222 | $state = self::STATE_OCTET; |
||
| 223 | break; |
||
| 224 | case '}': |
||
| 225 | if ($state !== self::STATE_OCTET) { |
||
| 226 | throw new \InvalidArgumentException('Invalid characters } found'); |
||
| 227 | } |
||
| 228 | |||
| 229 | $list = $list->withOctet((int)substr($sequence, 1, -1)); |
||
| 230 | $sequence = ''; |
||
| 231 | |||
| 232 | $state = self::STATE_NAME; |
||
| 233 | break; |
||
| 234 | case ' ': |
||
| 235 | if ($sequence === ' ') { |
||
| 236 | $state = self::STATE_NONE; |
||
| 237 | } |
||
| 238 | |||
| 239 | if ($state === self::STATE_NONE) { |
||
| 240 | $sequence = ''; |
||
| 241 | } |
||
| 242 | |||
| 243 | if ($state === self::STATE_NAME) { |
||
| 244 | $list = $list->withItem(new NameItem(substr($sequence, 0, -1))); |
||
| 245 | $sequence = ''; |
||
| 246 | $state = self::STATE_NONE; |
||
| 247 | } |
||
| 248 | |||
| 249 | break; |
||
| 250 | case "\n": |
||
| 251 | $list = $list->withBody(substr($serializedList, $index + 1)); |
||
| 252 | $sequence = ''; |
||
| 253 | break 2; |
||
| 254 | } |
||
| 255 | |||
| 256 | $index++; |
||
| 257 | } |
||
| 258 | |||
| 259 | if ($sequence) { |
||
| 260 | $list = $list->withItem(new NameItem($sequence)); |
||
| 261 | } |
||
| 262 | |||
| 263 | return $list; |
||
| 264 | } |
||
| 265 | } |