Total Complexity | 41 |
Total Lines | 226 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 0 | Features | 0 |
Complex classes like MarkdownService 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 MarkdownService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class MarkdownService |
||
23 | { |
||
24 | use SetsUpMarkdownConverter; |
||
25 | |||
26 | public string $markdown; |
||
27 | public ?string $sourceModel = null; |
||
28 | |||
29 | protected array $config = []; |
||
30 | protected array $extensions = []; |
||
31 | protected MarkdownConverter $converter; |
||
32 | |||
33 | protected string $html; |
||
34 | protected array $features = []; |
||
35 | |||
36 | protected array $preprocessors = []; |
||
37 | protected array $postprocessors = []; |
||
38 | |||
39 | public function __construct(string $markdown, ?string $sourceModel = null) |
||
40 | { |
||
41 | $this->sourceModel = $sourceModel; |
||
42 | $this->markdown = $markdown; |
||
43 | } |
||
44 | |||
45 | public function parse(): string |
||
56 | } |
||
57 | |||
58 | protected function setupConverter(): void |
||
74 | } |
||
75 | |||
76 | public function addExtension(string $extensionClassName): void |
||
80 | } |
||
81 | } |
||
82 | |||
83 | protected function runPreProcessing(): void |
||
88 | } |
||
89 | } |
||
90 | |||
91 | protected function runPostProcessing(): void |
||
92 | { |
||
93 | if ($this->determineIfTorchlightAttributionShouldBeInjected()) { |
||
94 | $this->html .= $this->injectTorchlightAttribution(); |
||
95 | } |
||
96 | |||
97 | /** @var PostProcessor $processor */ |
||
98 | foreach ($this->postprocessors as $postprocessor) { |
||
99 | $this->html = $postprocessor::postprocess($this->html); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public function getExtensions(): array |
||
104 | { |
||
105 | return $this->extensions; |
||
106 | } |
||
107 | |||
108 | public function removeFeature(string $feature): static |
||
109 | { |
||
110 | if (in_array($feature, $this->features)) { |
||
111 | $this->features = array_diff($this->features, [$feature]); |
||
112 | } |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | public function addFeature(string $feature): static |
||
118 | { |
||
119 | if (! in_array($feature, $this->features)) { |
||
120 | $this->features[] = $feature; |
||
121 | } |
||
122 | |||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | public function withPermalinks(): static |
||
131 | } |
||
132 | |||
133 | public function isDocumentationPage(): bool |
||
134 | { |
||
135 | return isset($this->sourceModel) && $this->sourceModel === DocumentationPage::class; |
||
136 | } |
||
137 | |||
138 | public function withTableOfContents(): static |
||
139 | { |
||
140 | $this->addFeature('table-of-contents'); |
||
141 | |||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | public function canEnableTorchlight(): bool |
||
149 | } |
||
150 | |||
151 | public function canEnablePermalinks(): bool |
||
152 | { |
||
153 | if ($this->hasFeature('permalinks')) { |
||
154 | return true; |
||
155 | } |
||
156 | |||
157 | if ($this->isDocumentationPage() && DocumentationPage::hasTableOfContents()) { |
||
158 | return true; |
||
159 | } |
||
160 | |||
161 | return false; |
||
162 | } |
||
163 | |||
164 | public function hasFeature(string $feature): bool |
||
165 | { |
||
166 | return in_array($feature, $this->features); |
||
167 | } |
||
168 | |||
169 | protected function determineIfTorchlightAttributionShouldBeInjected(): bool |
||
170 | { |
||
171 | return ! $this->isDocumentationPage() |
||
172 | && config('torchlight.attribution.enabled', true) |
||
173 | && str_contains($this->html, 'Syntax highlighted by torchlight.dev'); |
||
174 | } |
||
175 | |||
176 | protected function injectTorchlightAttribution(): string |
||
177 | { |
||
178 | return '<br>'.$this->converter->convert(config( |
||
179 | 'torchlight.attribution.markdown', |
||
180 | 'Syntax highlighted by torchlight.dev' |
||
181 | )); |
||
182 | } |
||
183 | |||
184 | protected function configurePermalinksExtension(): void |
||
197 | } |
||
198 | |||
199 | protected function enableAllHtmlElements(): void |
||
200 | { |
||
201 | $this->addExtension(DisallowedRawHtmlExtension::class); |
||
202 | |||
203 | $this->config = array_merge([ |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Normalize indentation for an un-compiled Markdown string. |
||
212 | */ |
||
213 | public static function normalizeIndentationLevel(string $string): string |
||
214 | { |
||
215 | $lines = self::getNormalizedLines($string); |
||
216 | |||
217 | [$startNumber, $indentationLevel] = self::findLineContentPositions($lines); |
||
218 | |||
219 | foreach ($lines as $lineNumber => $line) { |
||
220 | if ($lineNumber >= $startNumber) { |
||
221 | $lines[$lineNumber] = substr($line, $indentationLevel); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return implode("\n", $lines); |
||
226 | } |
||
227 | |||
228 | protected static function getNormalizedLines(string $string): array |
||
229 | { |
||
230 | return explode("\n", str_replace(["\t", "\r\n"], [' ', "\n"], $string)); |
||
231 | } |
||
232 | |||
233 | /** @return int[] Find the indentation level and position of the first line that has content */ |
||
234 | protected static function findLineContentPositions(array $lines): array |
||
248 | } |
||
249 | } |
||
250 |