Complex classes like EntityDataRequestHandler 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 EntityDataRequestHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class EntityDataRequestHandler { |
||
36 | |||
37 | /** |
||
38 | * Allowed smallest and biggest number of seconds for the "max-age=..." and "s-maxage=..." cache |
||
39 | * control parameters. |
||
40 | * |
||
41 | * @todo Hard maximum could be configurable somehow. |
||
42 | */ |
||
43 | const MINIMUM_MAX_AGE = 0; |
||
44 | const MAXIMUM_MAX_AGE = 2678400; // 31 days |
||
45 | |||
46 | /** |
||
47 | * @var EntityDataSerializationService |
||
48 | */ |
||
49 | private $serializationService; |
||
50 | |||
51 | /** |
||
52 | * @var EntityDataUriManager |
||
53 | */ |
||
54 | private $uriManager; |
||
55 | |||
56 | /** |
||
57 | * @var EntityIdParser |
||
58 | */ |
||
59 | private $entityIdParser; |
||
60 | |||
61 | /** |
||
62 | * @var EntityRevisionLookup |
||
63 | */ |
||
64 | private $entityRevisionLookup; |
||
65 | |||
66 | /** |
||
67 | * @var EntityRedirectLookup |
||
68 | */ |
||
69 | private $entityRedirectLookup; |
||
70 | |||
71 | /** |
||
72 | * @var EntityTitleLookup |
||
73 | */ |
||
74 | private $entityTitleLookup; |
||
75 | |||
76 | /** |
||
77 | * @var EntityDataFormatProvider |
||
78 | */ |
||
79 | private $entityDataFormatProvider; |
||
80 | |||
81 | /** |
||
82 | * @var HtmlCacheUpdater |
||
83 | */ |
||
84 | private $htmlCacheUpdater; |
||
85 | |||
86 | /** |
||
87 | * @var LoggerInterface |
||
88 | */ |
||
89 | private $logger; |
||
90 | |||
91 | /** |
||
92 | * @var string |
||
93 | */ |
||
94 | private $defaultFormat; |
||
95 | |||
96 | /** |
||
97 | * @var int Number of seconds to cache entity data. |
||
98 | */ |
||
99 | private $maxAge; |
||
100 | |||
101 | /** |
||
102 | * @var bool |
||
103 | */ |
||
104 | private $useCdn; |
||
105 | |||
106 | /** |
||
107 | * @var string|null |
||
108 | */ |
||
109 | private $frameOptionsHeader; |
||
110 | |||
111 | /** |
||
112 | * @var string[] |
||
113 | */ |
||
114 | private $entityTypesWithoutRdfOutput; |
||
115 | |||
116 | /** |
||
117 | * @param EntityDataUriManager $uriManager |
||
118 | * @param HtmlCacheUpdater $htmlCacheUpdater |
||
119 | * @param EntityTitleLookup $entityTitleLookup |
||
120 | * @param EntityIdParser $entityIdParser |
||
121 | * @param EntityRevisionLookup $entityRevisionLookup |
||
122 | * @param EntityRedirectLookup $entityRedirectLookup |
||
123 | * @param EntityDataSerializationService $serializationService |
||
124 | * @param EntityDataFormatProvider $entityDataFormatProvider |
||
125 | * @param LoggerInterface $logger |
||
126 | * @param string[] $entityTypesWithoutRdfOutput |
||
127 | * @param string $defaultFormat The format as a file extension or MIME type. |
||
128 | * @param int $maxAge number of seconds to cache entity data |
||
129 | * @param bool $useCdn do we have web caches configured? |
||
130 | * @param string|null $frameOptionsHeader for X-Frame-Options |
||
131 | */ |
||
132 | public function __construct( |
||
163 | |||
164 | /** |
||
165 | * Checks whether the request is complete, i.e. whether it contains all information needed |
||
166 | * to reply with entity data. |
||
167 | * |
||
168 | * This does not check whether the request is valid and will actually produce a successful |
||
169 | * response. |
||
170 | * |
||
171 | * @param string|null $doc Document name, e.g. Q5 or Q5.json or Q5:33.xml |
||
172 | * @param WebRequest $request |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function canHandleRequest( $doc, WebRequest $request ) { |
||
185 | |||
186 | /** |
||
187 | * Main method for handling requests. |
||
188 | * |
||
189 | * @param string $doc Document name, e.g. Q5 or Q5.json or Q5:33.xml |
||
190 | * @param WebRequest $request The request parameters. Known parameters are: |
||
191 | * - id: the entity ID |
||
192 | * - format: the format |
||
193 | * - oldid|revision: the revision ID |
||
194 | * - action=purge: to purge cached data from (web) caches |
||
195 | * @param OutputPage $output |
||
196 | * |
||
197 | * @note Instead of an output page, a WebResponse could be sufficient, but |
||
198 | * redirect logic is currently implemented in OutputPage. |
||
199 | * |
||
200 | * @throws HttpError |
||
201 | * @suppress SecurityCheck-DoubleEscaped |
||
202 | */ |
||
203 | public function handleRequest( $doc, WebRequest $request, OutputPage $output ) { |
||
278 | |||
279 | /** |
||
280 | * Returns the canonical format name for the given format. |
||
281 | * |
||
282 | * @param string $format |
||
283 | * |
||
284 | * @return string |
||
285 | * @throws HttpError code 415 if the format is not supported. |
||
286 | */ |
||
287 | public function getCanonicalFormat( $format ) { |
||
306 | |||
307 | /** |
||
308 | * Purges the entity data identified by the doc parameter from any HTTP caches. |
||
309 | * Does nothing if $wgUseCdn is not set. |
||
310 | * |
||
311 | * @param EntityId $id The entity ID for which to purge all data. |
||
312 | * @param int $revision The revision ID (0 for current/unspecified) |
||
313 | */ |
||
314 | public function purgeWebCache( EntityId $id, int $revision ) { |
||
320 | |||
321 | /** |
||
322 | * Applies HTTP content negotiation. |
||
323 | * If the negotiation is successfull, this method will set the appropriate redirect |
||
324 | * in the OutputPage object and return. Otherwise, an HttpError is thrown. |
||
325 | * |
||
326 | * @param WebRequest $request |
||
327 | * @param OutputPage $output |
||
328 | * @param EntityId $id The ID of the entity to show |
||
329 | * @param int $revision The desired revision |
||
330 | * |
||
331 | * @throws HttpError |
||
332 | */ |
||
333 | public function httpContentNegotiation( WebRequest $request, OutputPage $output, EntityId $id, $revision = 0 ) { |
||
371 | |||
372 | /** |
||
373 | * Loads the requested Entity. Redirects are resolved if no specific revision |
||
374 | * is requested or they are explicitly allowed by $allowRedirects. |
||
375 | * |
||
376 | * @param EntityId $id |
||
377 | * @param int $revision The revision ID (use 0 for the current revision). |
||
378 | * @param bool $allowRedirects Can we fetch redirects when revision is set? |
||
379 | * |
||
380 | * @return array list( EntityRevision, RedirectRevision|null ) |
||
381 | * @throws HttpError |
||
382 | * @suppress SecurityCheck-DoubleEscaped |
||
383 | */ |
||
384 | private function getEntityRevision( EntityId $id, $revision, $allowRedirects = false ) { |
||
457 | |||
458 | /** |
||
459 | * Loads incoming redirects referring to the given entity ID. |
||
460 | * |
||
461 | * @param EntityId $id |
||
462 | * |
||
463 | * @return EntityId[] |
||
464 | * @throws HttpError |
||
465 | */ |
||
466 | private function getIncomingRedirects( EntityId $id ) { |
||
483 | |||
484 | /** |
||
485 | * Output entity data. |
||
486 | * |
||
487 | * @param WebRequest $request |
||
488 | * @param OutputPage $output |
||
489 | * @param string $format The name (mime type of file extension) of the format to use |
||
490 | * @param EntityId $id The entity ID |
||
491 | * @param int $revision The revision ID (use 0 for the current revision). |
||
492 | * |
||
493 | * @throws HttpError |
||
494 | */ |
||
495 | public function showData( WebRequest $request, OutputPage $output, $format, EntityId $id, $revision ) { |
||
544 | |||
545 | /** |
||
546 | * Output the entity data and set the appropriate HTTP response headers. |
||
547 | * |
||
548 | * @param WebRequest $request |
||
549 | * @param EntityId $requestId the original entity ID of the request |
||
550 | * @param int $requestRevision the original revision ID of the request (0 for latest) |
||
551 | * @param WebResponse $response |
||
552 | * @param string $data the data to output |
||
553 | * @param string $contentType the data's mime type |
||
554 | * @param string $lastModified |
||
555 | */ |
||
556 | public function outputData( |
||
597 | |||
598 | } |
||
599 |