Complex classes like Service 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 Service, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Service implements \Caridea\Container\ContainerAware |
||
29 | { |
||
30 | use \Caridea\Container\ContainerSetter; |
||
31 | |||
32 | /** |
||
33 | * @var \Minotaur\View\Page The page, or null |
||
34 | */ |
||
35 | private $page; |
||
36 | |||
37 | /** |
||
38 | * @var \Minotaur\View\BlockLayout The block layout, or null |
||
39 | */ |
||
40 | private $blocks; |
||
41 | |||
42 | /** |
||
43 | * @var array<string> List of statuses |
||
44 | */ |
||
45 | private const STATUSES = ['msg-warning', 'msg-info', 'msg-error']; |
||
46 | |||
47 | /** |
||
48 | * Creates a new ViewService. |
||
49 | * |
||
50 | * @param $container - The dependency injection container |
||
51 | */ |
||
52 | public function __construct(?\Caridea\Container\Container $container) |
||
56 | |||
57 | /** |
||
58 | * Gets the Page for this request (created lazily). |
||
59 | * |
||
60 | * @param string $title The page title |
||
61 | * @return \Minotaur\View\Page A Page |
||
62 | */ |
||
63 | public function getPage(string $title): Page |
||
72 | |||
73 | /** |
||
74 | * Calls the page visitors. |
||
75 | * |
||
76 | * @param \Minotaur\View\Page $page The page to visit |
||
77 | */ |
||
78 | protected function callPageVisitors(Page $page) |
||
84 | |||
85 | /** |
||
86 | * Generates a page title. |
||
87 | * |
||
88 | * @param string|null $title The page title |
||
89 | * @return string The formatted page title |
||
90 | */ |
||
91 | protected function getPageTitle(?string $title): string |
||
99 | |||
100 | /** |
||
101 | * Sets a Flash Message. |
||
102 | * |
||
103 | * @param string $name The status |
||
104 | * @param string $value The message |
||
105 | * @param bool $current Whether to add message to the current request |
||
106 | */ |
||
107 | public function setFlashMessage(string $name, string $value, bool $current = false): void |
||
120 | |||
121 | /** |
||
122 | * Clears Flash Messages. |
||
123 | * |
||
124 | * @param bool $current Whether to add message to the current request |
||
125 | */ |
||
126 | public function clearFlashMessages(bool $current = false): void |
||
139 | |||
140 | /** |
||
141 | * Keeps all current flash messages for the next request. |
||
142 | */ |
||
143 | public function keepFlashMessages(): void |
||
156 | |||
157 | /** |
||
158 | * Gets any flash messages in the session keyed by status. |
||
159 | * |
||
160 | * @return array<string,array<string>> map of flash messages |
||
161 | */ |
||
162 | public function getFlashMessages(): array |
||
186 | |||
187 | /** |
||
188 | * Gets the last request that the Dispatcher sent to a controller. |
||
189 | * |
||
190 | * @return \Psr\Http\Message\ServerRequestInterface The last dispatched request, or `null` |
||
191 | */ |
||
192 | public function getDispatchedRequest(): ?\Psr\Http\Message\ServerRequestInterface |
||
198 | |||
199 | /** |
||
200 | * Gets all blocks registered for a given region |
||
201 | * |
||
202 | * @param string $region The region to search |
||
203 | * @return array<\Minotaur\View\Block> The found blocks in that region, or an empty array. |
||
204 | */ |
||
205 | public function getBlocks(string $region): array |
||
214 | |||
215 | protected function getBlockLayout(): BlockLayout |
||
239 | |||
240 | /** |
||
241 | * Gets any `Minotaur\Db\DbRefResolver` objects in the container. |
||
242 | * |
||
243 | * @return array<\Minotaur\Db\DbRefResolver> The DbRefResolver objects found. |
||
244 | */ |
||
245 | public function getDbRefResolvers(): array |
||
249 | |||
250 | /** |
||
251 | * Gets any `Minotaur\View\EntityLinker` objects in the container. |
||
252 | * |
||
253 | * @return array<\Minotaur\View\EntityLinker> The EntityLinker objects found. |
||
254 | */ |
||
255 | public function getEntityLinkers(): array |
||
259 | |||
260 | /** |
||
261 | * Gets the CSRF token. |
||
262 | * |
||
263 | * @return string|null The CSRF token or `null` |
||
264 | * @throws \UnexpectedValueException if the plugin wasn't in the container |
||
265 | */ |
||
266 | public function getCsrfToken(): ?string |
||
274 | |||
275 | /** |
||
276 | * Checks to see if the provided token matches the session CSRF token. |
||
277 | * |
||
278 | * @param string the provided token |
||
279 | * @return bool whether the provided token matches |
||
280 | * @throws \UnexpectedValueException if the plugin wasn't in the container |
||
281 | */ |
||
282 | public function isCsrfValid(string $token): bool |
||
290 | } |
||
291 |