Complex classes like TableParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class TableParser implements BlockParserInterface, EnvironmentAwareInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var EnvironmentInterface |
||
| 31 | */ |
||
| 32 | private $environment; |
||
| 33 | |||
| 34 | 156 | public function parse(ContextInterface $context, Cursor $cursor): bool |
|
| 35 | { |
||
| 36 | 156 | $container = $context->getContainer(); |
|
| 37 | 156 | if (!$container instanceof Paragraph) { |
|
| 38 | 156 | return false; |
|
| 39 | } |
||
| 40 | |||
| 41 | 87 | $lines = $container->getStrings(); |
|
| 42 | 87 | if (count($lines) !== 1) { |
|
| 43 | 18 | return false; |
|
| 44 | } |
||
| 45 | |||
| 46 | 87 | if (\strpos($lines[0], '|') === false) { |
|
| 47 | 18 | return false; |
|
| 48 | } |
||
| 49 | |||
| 50 | 75 | $oldState = $cursor->saveState(); |
|
| 51 | 75 | $cursor->advanceToNextNonSpaceOrTab(); |
|
| 52 | 75 | $columns = $this->parseColumns($cursor); |
|
| 53 | |||
| 54 | 75 | if (empty($columns)) { |
|
| 55 | $cursor->restoreState($oldState); |
||
| 56 | |||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | 75 | $head = $this->parseRow(trim((string) array_pop($lines)), $columns, TableCell::TYPE_HEAD); |
|
| 61 | 75 | if (null === $head) { |
|
| 62 | 3 | $cursor->restoreState($oldState); |
|
| 63 | |||
| 64 | 3 | return false; |
|
| 65 | } |
||
| 66 | |||
| 67 | 48 | $table = new Table(function (Cursor $cursor, Table $table) use ($columns): bool { |
|
| 68 | // The next line cannot be a new block start |
||
| 69 | // This is a bit inefficient, but it's the only feasible way to check |
||
| 70 | // given the current v1 API. |
||
| 71 | 69 | if (self::isANewBlock($this->environment, $cursor->getLine())) { |
|
| 72 | 6 | return false; |
|
| 73 | } |
||
| 74 | |||
| 75 | 69 | $row = $this->parseRow(\trim($cursor->getLine()), $columns); |
|
| 76 | 69 | if (null === $row) { |
|
| 77 | 18 | return false; |
|
| 78 | } |
||
| 79 | |||
| 80 | 69 | $table->getBody()->appendChild($row); |
|
| 81 | |||
| 82 | 69 | return true; |
|
| 83 | 72 | }); |
|
| 84 | |||
| 85 | 72 | $table->getHead()->appendChild($head); |
|
| 86 | |||
| 87 | 72 | if (count($lines) >= 1) { |
|
| 88 | $paragraph = new Paragraph(); |
||
| 89 | foreach ($lines as $line) { |
||
| 90 | $paragraph->addLine($line); |
||
| 91 | } |
||
| 92 | |||
| 93 | $context->replaceContainerBlock($paragraph); |
||
| 94 | $context->addBlock($table); |
||
| 95 | } else { |
||
| 96 | 72 | $context->replaceContainerBlock($table); |
|
| 97 | } |
||
| 98 | |||
| 99 | 72 | return true; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $line |
||
| 104 | * @param array<int, string> $columns |
||
| 105 | * @param string $type |
||
| 106 | * |
||
| 107 | * @return TableRow|null |
||
| 108 | */ |
||
| 109 | 75 | private function parseRow(string $line, array $columns, string $type = TableCell::TYPE_BODY): ?TableRow |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param Cursor $cursor |
||
| 141 | * |
||
| 142 | * @return array<int, string> |
||
|
|
|||
| 143 | */ |
||
| 144 | 75 | private function split(Cursor $cursor): array |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @param Cursor $cursor |
||
| 186 | * |
||
| 187 | * @return array<int, string> |
||
| 188 | */ |
||
| 189 | 75 | private function parseColumns(Cursor $cursor): array |
|
| 249 | |||
| 250 | 75 | private static function getAlignment(bool $left, bool $right): ?string |
|
| 262 | |||
| 263 | 162 | public function setEnvironment(EnvironmentInterface $environment) |
|
| 267 | |||
| 268 | 69 | private static function isANewBlock(EnvironmentInterface $environment, string $line): bool |
|
| 283 | } |
||
| 284 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.