| Total Complexity | 44 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DumpDocs 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.
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 DumpDocs, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | final class DumpDocs |
||
| 31 | { |
||
| 32 | public const MODE_HTML = 'html'; |
||
| 33 | public const MODE_MARKDOWN = 'markdown'; |
||
| 34 | |||
| 35 | /** @var array<string, AbstractDescriptor> */ |
||
| 36 | private $descriptors = []; |
||
| 37 | |||
| 38 | /** @var "html"|"md" */ |
||
|
|
|||
| 39 | private $ext; |
||
| 40 | |||
| 41 | public function __invoke(Profile $profile, string $alpsFile, string $format = self::MODE_HTML): void |
||
| 62 | } |
||
| 63 | |||
| 64 | private function dumpImage(string $title, string $docsDir, string $format, string $alpsFile, string $type): void |
||
| 65 | { |
||
| 66 | $imgSrc = str_replace(['json', 'xml'], "{$type}svg", basename($alpsFile)); |
||
| 67 | $format === self::MODE_HTML ? |
||
| 68 | $this->dumpImageHtml($title, $docsDir, $imgSrc, $type): |
||
| 69 | $this->dumpImageMd($title, $docsDir, $imgSrc, $type); |
||
| 70 | } |
||
| 71 | |||
| 72 | private function dumpImageMd(string $title, string $docsDir, string $imgSrc, string $type): void |
||
| 73 | { |
||
| 74 | $isIdMode = $type === ''; |
||
| 75 | $link = $isIdMode ? 'id | [title](asd.title.md)' : '[id](asd.md) | title'; |
||
| 76 | $html = <<<EOT |
||
| 77 | {$link} |
||
| 78 | <img src="../{$imgSrc}" alt="application state diagram"> |
||
| 79 | EOT; |
||
| 80 | file_put_contents($docsDir . "/asd.{$type}md", $html); |
||
| 81 | } |
||
| 82 | |||
| 83 | private function dumpImageHtml(string $title, string $docsDir, string $imgSrc, string $type): void |
||
| 84 | { |
||
| 85 | $isIdMode = $type === ''; |
||
| 86 | $link = $isIdMode ? 'id | <a href="asd.title.html">title</a>' : '<a href="asd.html">id</a> | title'; |
||
| 87 | $html = <<<EOT |
||
| 88 | <html lang="en"> |
||
| 89 | <head> |
||
| 90 | <title>{$title}</title> |
||
| 91 | <meta charset="UTF-8"> |
||
| 92 | </head> |
||
| 93 | <body> |
||
| 94 | <div style="font-size: medium;" >{$link}</div> |
||
| 95 | <iframe src="../{$imgSrc}" style="border:0; width:100%; height:95%" allow="fullscreen"></iframe> |
||
| 96 | </body> |
||
| 97 | </html> |
||
| 98 | |||
| 99 | EOT; |
||
| 100 | file_put_contents($docsDir . "/asd.{$type}html", $html); |
||
| 101 | } |
||
| 102 | |||
| 103 | private function convertHtml(string $title, string $markdown): string |
||
| 104 | { |
||
| 105 | return (new MdToHtml())($title, $markdown) . PHP_EOL; |
||
| 106 | } |
||
| 107 | |||
| 108 | private function fileOutput(string $title, string $markDown, string $basePath, string $format): void |
||
| 109 | { |
||
| 110 | $file = sprintf('%s.%s', $basePath, $this->ext); |
||
| 111 | if ($format === self::MODE_MARKDOWN) { |
||
| 112 | file_put_contents($file, $markDown); |
||
| 113 | |||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | file_put_contents($file, $this->convertHtml($title, $markDown)); |
||
| 118 | } |
||
| 119 | |||
| 120 | private function mkDir(string $baseDir, string $dirName): string |
||
| 121 | { |
||
| 122 | $dir = sprintf('%s/%s', $baseDir, $dirName); |
||
| 123 | if (! is_dir($dir)) { |
||
| 124 | mkdir($dir, 0777, true); // @codeCoverageIgnore |
||
| 125 | } |
||
| 126 | |||
| 127 | return $dir; |
||
| 128 | } |
||
| 129 | |||
| 130 | private function getSemanticDoc(AbstractDescriptor $descriptor, string $asd, string $title): string |
||
| 131 | { |
||
| 132 | $descriptorSemantic = $this->getDescriptorInDescriptor($descriptor); |
||
| 133 | $rt = $this->getRt($descriptor); |
||
| 134 | $description = ''; |
||
| 135 | $description .= $this->getDescriptorProp('type', $descriptor); |
||
| 136 | $description .= $this->getDescriptorProp('title', $descriptor); |
||
| 137 | $description .= $this->getDescriptorProp('href', $descriptor); |
||
| 138 | $description .= $this->getDescriptorKeyValue('doc', (string) ($descriptor->doc->value ?? '')); |
||
| 139 | $description .= $this->getDescriptorProp('def', $descriptor); |
||
| 140 | $description .= $this->getDescriptorProp('rel', $descriptor); |
||
| 141 | $description .= $this->getTag($descriptor->tags); |
||
| 142 | $linkRelations = $this->getLinkRelations($descriptor->linkRelations); |
||
| 143 | $titleHeader = $title ? sprintf('%s: Semantic Descriptor', $title) : 'Semantic Descriptor'; |
||
| 144 | |||
| 145 | return <<<EOT |
||
| 146 | {$titleHeader} |
||
| 147 | # {$descriptor->id} |
||
| 148 | {$description}{$rt}{$linkRelations}{$descriptorSemantic} |
||
| 149 | --- |
||
| 150 | |||
| 151 | [home](../index.{$this->ext}) | [asd]($asd) |
||
| 152 | EOT; |
||
| 153 | } |
||
| 154 | |||
| 155 | private function getDescriptorProp(string $key, AbstractDescriptor $descriptor): string |
||
| 156 | { |
||
| 157 | if (! property_exists($descriptor, $key) || ! $descriptor->{$key}) { |
||
| 158 | return ''; |
||
| 159 | } |
||
| 160 | |||
| 161 | $value = (string) $descriptor->{$key}; |
||
| 162 | if ($this->isUrl($value)) { |
||
| 163 | return " * {$key}: [{$value}]({$value})" . PHP_EOL; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($this->isFragment($value)) { |
||
| 167 | [, $id] = explode('#', $value); |
||
| 168 | |||
| 169 | return " * {$key}: [{$id}](semantic.{$id}.{$this->ext})" . PHP_EOL; |
||
| 170 | } |
||
| 171 | |||
| 172 | return " * {$key}: {$value}" . PHP_EOL; |
||
| 173 | } |
||
| 174 | |||
| 175 | private function isUrl(string $text): bool |
||
| 176 | { |
||
| 177 | return filter_var($text, FILTER_VALIDATE_URL) !== false; |
||
| 178 | } |
||
| 179 | |||
| 180 | private function isFragment(string $text): bool |
||
| 181 | { |
||
| 182 | return $text[0] === '#'; |
||
| 183 | } |
||
| 184 | |||
| 185 | private function getDescriptorKeyValue(string $key, string $value): string |
||
| 192 | } |
||
| 193 | |||
| 194 | private function getRt(AbstractDescriptor $descriptor): string |
||
| 195 | { |
||
| 196 | if ($descriptor instanceof SemanticDescriptor) { |
||
| 197 | return ''; |
||
| 198 | } |
||
| 199 | |||
| 200 | assert($descriptor instanceof TransDescriptor); |
||
| 201 | |||
| 202 | return sprintf(' * rt: [%s](semantic.%s.%s)', $descriptor->rt, $descriptor->rt, $this->ext) . PHP_EOL; |
||
| 203 | } |
||
| 204 | |||
| 205 | private function getDescriptorInDescriptor(AbstractDescriptor $descriptor): string |
||
| 206 | { |
||
| 207 | if ($descriptor->descriptor === []) { |
||
| 208 | return ''; |
||
| 209 | } |
||
| 210 | |||
| 211 | $descriptors = $this->getInlineDescriptors($descriptor->descriptor); |
||
| 212 | |||
| 213 | $table = sprintf(' * descriptor%s%s| id | type | title |%s|---|---|---|%s', PHP_EOL, PHP_EOL, PHP_EOL, PHP_EOL); |
||
| 214 | foreach ($descriptors as $descriptor) { |
||
| 215 | $table .= sprintf('| %s | %s | %s |', $descriptor->htmlLink($this->ext), $descriptor->type, $descriptor->title) . PHP_EOL; |
||
| 216 | } |
||
| 217 | |||
| 218 | return $table; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param non-empty-list<stdClass> $inlineDescriptors |
||
| 223 | * |
||
| 224 | * @return non-empty-list<AbstractDescriptor> |
||
| 225 | */ |
||
| 226 | private function getInlineDescriptors(array $inlineDescriptors): array |
||
| 227 | { |
||
| 228 | $descriptors = []; |
||
| 229 | foreach ($inlineDescriptors as $descriptor) { |
||
| 230 | if (isset($descriptor->id)) { |
||
| 231 | assert(is_string($descriptor->id)); |
||
| 232 | $descriptors[] = $this->descriptors[$descriptor->id]; |
||
| 233 | continue; |
||
| 234 | } |
||
| 235 | |||
| 236 | assert(is_string($descriptor->href)); |
||
| 237 | $id = substr($descriptor->href, (int) strpos($descriptor->href, '#') + 1); |
||
| 238 | assert(isset($this->descriptors[$id])); |
||
| 239 | |||
| 240 | $original = clone $this->descriptors[$id]; |
||
| 241 | if (isset($descriptor->title)) { |
||
| 242 | $original->title = (string) $descriptor->title; |
||
| 243 | } |
||
| 244 | |||
| 245 | $descriptors[] = $original; |
||
| 246 | } |
||
| 247 | |||
| 248 | usort($descriptors, static function (AbstractDescriptor $a, AbstractDescriptor $b): int { |
||
| 249 | $order = ['semantic' => 0, 'safe' => 1, 'unsafe' => 2, 'idempotent' => 3]; |
||
| 250 | |||
| 251 | return $order[$a->type] <=> $order[$b->type]; |
||
| 252 | }); |
||
| 253 | |||
| 254 | assert($descriptors !== []); |
||
| 255 | |||
| 256 | return $descriptors; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param list<string> $tags |
||
| 261 | */ |
||
| 262 | private function getTag(array $tags): string |
||
| 263 | { |
||
| 264 | if ($tags === []) { |
||
| 265 | return ''; |
||
| 266 | } |
||
| 267 | |||
| 268 | return " * tag: {$this->getTagString($tags)}"; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param list<string> $tags |
||
| 273 | */ |
||
| 274 | private function getTagString(array $tags): string |
||
| 275 | { |
||
| 276 | $string = []; |
||
| 277 | foreach ($tags as $tag) { |
||
| 278 | $string[] = "[{$tag}](tag.{$tag}.{$this->ext})"; |
||
| 279 | } |
||
| 280 | |||
| 281 | return implode(', ', $string) . PHP_EOL; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param list<string> $descriptorIds |
||
| 286 | */ |
||
| 287 | private function getTagDoc(string $tag, array $descriptorIds, string $title, string $asd): string |
||
| 305 | EOT; |
||
| 306 | } |
||
| 307 | |||
| 308 | private function getLinkRelations(LinkRelations $linkRelations): string |
||
| 309 | { |
||
| 310 | if ((string) $linkRelations === '') { |
||
| 311 | return ''; |
||
| 312 | } |
||
| 313 | |||
| 314 | return ' * links' . PHP_EOL . $linkRelations . PHP_EOL; |
||
| 315 | } |
||
| 316 | } |
||
| 317 |