Total Complexity | 41 |
Total Lines | 221 |
Duplicated Lines | 0 % |
Changes | 16 | ||
Bugs | 2 | Features | 0 |
Complex classes like AbstractPage 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 AbstractPage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class AbstractPage implements PageContract, CompilableContract |
||
29 | { |
||
30 | use PageSchema; |
||
31 | |||
32 | public static string $sourceDirectory; |
||
33 | public static string $outputDirectory; |
||
34 | public static string $fileExtension; |
||
35 | public static string $template; |
||
36 | |||
37 | public string $identifier; |
||
38 | public FrontMatter $matter; |
||
39 | |||
40 | /** @inheritDoc */ |
||
41 | final public static function getSourceDirectory(): string |
||
42 | { |
||
43 | return unslash(static::$sourceDirectory); |
||
44 | } |
||
45 | |||
46 | /** @inheritDoc */ |
||
47 | final public static function getOutputDirectory(): string |
||
48 | { |
||
49 | return unslash(static::$outputDirectory); |
||
50 | } |
||
51 | |||
52 | /** @inheritDoc */ |
||
53 | final public static function getFileExtension(): string |
||
54 | { |
||
55 | return '.'.ltrim(static::$fileExtension, '.'); |
||
56 | } |
||
57 | |||
58 | /** @inheritDoc */ |
||
59 | public static function parse(string $slug): PageContract |
||
60 | { |
||
61 | return (new SourceFileParser(static::class, $slug))->get(); |
||
62 | } |
||
63 | |||
64 | /** @inheritDoc */ |
||
65 | public static function files(): array|false |
||
66 | { |
||
67 | return DiscoveryService::getSourceFileListForModel(static::class); |
||
68 | } |
||
69 | |||
70 | /** @inheritDoc */ |
||
71 | public static function all(): Collection |
||
72 | { |
||
73 | $collection = new Collection(); |
||
74 | |||
75 | foreach (static::files() as $basename) { |
||
76 | $collection->push(static::parse($basename)); |
||
77 | } |
||
78 | |||
79 | return $collection; |
||
80 | } |
||
81 | |||
82 | /** @inheritDoc */ |
||
83 | public static function qualifyBasename(string $basename): string |
||
84 | { |
||
85 | return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension(); |
||
86 | } |
||
87 | |||
88 | /** @inheritDoc */ |
||
89 | public static function getOutputLocation(string $basename): string |
||
90 | { |
||
91 | // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory. |
||
92 | return trim( |
||
93 | static::getOutputDirectory().'/'.unslash($basename), |
||
94 | '/' |
||
95 | ).'.html'; |
||
96 | } |
||
97 | |||
98 | public function __construct(string $identifier = '', FrontMatter|array $matter = []) |
||
99 | { |
||
100 | $this->identifier = $identifier; |
||
101 | $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter); |
||
|
|||
102 | $this->constructPageSchema(); |
||
103 | } |
||
104 | |||
105 | /** @inheritDoc */ |
||
106 | public function get(string $key = null, mixed $default = null): mixed |
||
107 | { |
||
108 | if (property_exists($this, $key) && isset($this->$key)) { |
||
109 | return $this->$key; |
||
110 | } |
||
111 | |||
112 | return $this->matter($key, $default); |
||
113 | } |
||
114 | |||
115 | /** @inheritDoc */ |
||
116 | public function matter(string $key = null, mixed $default = null): mixed |
||
117 | { |
||
118 | return $this->matter->get($key, $default); |
||
119 | } |
||
120 | |||
121 | /** @inheritDoc */ |
||
122 | public function getSourcePath(): string |
||
123 | { |
||
124 | return static::qualifyBasename($this->identifier); |
||
125 | } |
||
126 | |||
127 | /** @inheritDoc */ |
||
128 | public function getOutputPath(): string |
||
129 | { |
||
130 | return static::getCurrentPagePath().'.html'; |
||
131 | } |
||
132 | |||
133 | /** @inheritDoc */ |
||
134 | public function getCurrentPagePath(): string |
||
135 | { |
||
136 | return trim(static::getOutputDirectory().'/'.$this->identifier, '/'); |
||
137 | } |
||
138 | |||
139 | /** @inheritDoc */ |
||
140 | public function getRoute(): Route |
||
141 | { |
||
142 | return new Route($this); |
||
143 | } |
||
144 | |||
145 | /** @inheritDoc */ |
||
146 | public function htmlTitle(): string |
||
147 | { |
||
148 | return config('site.name', 'HydePHP').' - '.$this->title; |
||
149 | } |
||
150 | |||
151 | /** @inheritDoc */ |
||
152 | public function getBladeView(): string |
||
153 | { |
||
154 | return static::$template; |
||
155 | } |
||
156 | |||
157 | /** @inheritDoc */ |
||
158 | abstract public function compile(): string; |
||
159 | |||
160 | public function getCanonicalUrl(): string |
||
161 | { |
||
162 | return $this->getRoute()->getQualifiedUrl(); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return string[] |
||
167 | * |
||
168 | * @psalm-return list<string> |
||
169 | */ |
||
170 | public function getDynamicMetadata(): array |
||
171 | { |
||
172 | $array = []; |
||
173 | |||
174 | if ($this->canUseCanonicalUrl()) { |
||
175 | $array[] = '<link rel="canonical" href="'.$this->getCanonicalUrl().'" />'; |
||
176 | } |
||
177 | |||
178 | if (Features::sitemap()) { |
||
179 | $array[] = '<link rel="sitemap" type="application/xml" title="Sitemap" href="'.Hyde::url('sitemap.xml').'" />'; |
||
180 | } |
||
181 | |||
182 | if (Features::rss()) { |
||
183 | $array[] = $this->makeRssFeedLink(); |
||
184 | } |
||
185 | |||
186 | if (isset($this->title)) { |
||
187 | if ($this->hasTwitterTitleInConfig()) { |
||
188 | $array[] = '<meta name="twitter:title" content="'.$this->htmlTitle().'" />'; |
||
189 | } |
||
190 | if ($this->hasOpenGraphTitleInConfig()) { |
||
191 | $array[] = '<meta property="og:title" content="'.$this->htmlTitle().'" />'; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | if ($this instanceof MarkdownPost) { |
||
196 | $array[] = "\n<!-- Blog Post Meta Tags -->"; |
||
197 | foreach ($this->getMetadata() as $name => $content) { |
||
198 | $array[] = Meta::name($name, $content); |
||
199 | } |
||
200 | foreach ($this->getMetaProperties() as $property => $content) { |
||
201 | $array[] = Meta::property($property, $content); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return $array; |
||
206 | } |
||
207 | |||
208 | public function renderPageMetadata(): string |
||
209 | { |
||
210 | return Meta::render( |
||
211 | withMergedData: $this->getDynamicMetadata() |
||
212 | ); |
||
213 | } |
||
214 | |||
215 | public function canUseCanonicalUrl(): bool |
||
216 | { |
||
217 | return Hyde::hasSiteUrl() && isset($this->identifier); |
||
218 | } |
||
219 | |||
220 | public function hasTwitterTitleInConfig(): bool |
||
221 | { |
||
222 | return str_contains(json_encode(config('hyde.meta', [])), 'twitter:title'); |
||
223 | } |
||
224 | |||
225 | public function hasOpenGraphTitleInConfig(): bool |
||
226 | { |
||
227 | return str_contains(json_encode(config('hyde.meta', [])), 'og:title'); |
||
228 | } |
||
229 | |||
230 | protected function makeRssFeedLink(): string |
||
234 | } |
||
235 | |||
236 | public function showInNavigation(): bool |
||
237 | { |
||
238 | return ! $this->navigation['hidden']; |
||
239 | } |
||
240 | |||
241 | public function navigationMenuPriority(): int |
||
242 | { |
||
243 | return $this->navigation['priority']; |
||
244 | } |
||
245 | |||
246 | public function navigationMenuTitle(): string |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Not yet implemented. |
||
253 | * |
||
254 | * If an item returns a route collection, |
||
255 | * it will automatically be made into a dropdown. |
||
256 | * |
||
257 | * @return \Illuminate\Support\Collection<\Hyde\Framework\Models\Route> |
||
258 | */ |
||
259 | // public function navigationMenuChildren(): Collection; |
||
260 | } |
||
261 |