Complex classes like RawAction 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 RawAction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class RawAction extends FormlessAction { |
||
| 36 | public function getName() { |
||
| 39 | |||
| 40 | public function requiresWrite() { |
||
| 43 | |||
| 44 | public function requiresUnblock() { |
||
| 47 | |||
| 48 | function onView() { |
||
| 49 | $this->getOutput()->disable(); |
||
| 50 | $request = $this->getRequest(); |
||
| 51 | $response = $request->response(); |
||
| 52 | $config = $this->context->getConfig(); |
||
| 53 | |||
| 54 | if ( !$request->checkUrlExtension() ) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) { |
||
|
|
|||
| 59 | return; // Client cache fresh and headers sent, nothing more to do. |
||
| 60 | } |
||
| 61 | |||
| 62 | $gen = $request->getVal( 'gen' ); |
||
| 63 | if ( $gen == 'css' || $gen == 'js' ) { |
||
| 64 | $this->gen = true; |
||
| 65 | } |
||
| 66 | |||
| 67 | $contentType = $this->getContentType(); |
||
| 68 | |||
| 69 | $maxage = $request->getInt( 'maxage', $config->get( 'SquidMaxage' ) ); |
||
| 70 | $smaxage = $request->getIntOrNull( 'smaxage' ); |
||
| 71 | if ( $smaxage === null ) { |
||
| 72 | if ( $contentType == 'text/css' || $contentType == 'text/javascript' ) { |
||
| 73 | // CSS/JS raw content has its own CDN max age configuration. |
||
| 74 | // Note: Title::getCdnUrls() includes action=raw for css/js pages, |
||
| 75 | // so if using the canonical url, this will get HTCP purges. |
||
| 76 | $smaxage = intval( $config->get( 'ForcedRawSMaxage' ) ); |
||
| 77 | } else { |
||
| 78 | // No CDN cache for anything else |
||
| 79 | $smaxage = 0; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | // Set standard Vary headers so cache varies on cookies and such (T125283) |
||
| 84 | $response->header( $this->getOutput()->getVaryHeader() ); |
||
| 85 | if ( $config->get( 'UseKeyHeader' ) ) { |
||
| 86 | $response->header( $this->getOutput()->getKeyHeader() ); |
||
| 87 | } |
||
| 88 | |||
| 89 | $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' ); |
||
| 90 | // Output may contain user-specific data; |
||
| 91 | // vary generated content for open sessions on private wikis |
||
| 92 | $privateCache = !User::isEveryoneAllowed( 'read' ) && |
||
| 93 | ( $smaxage == 0 || MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ); |
||
| 94 | // Don't accidentally cache cookies if user is logged in (T55032) |
||
| 95 | $privateCache = $privateCache || $this->getUser()->isLoggedIn(); |
||
| 96 | $mode = $privateCache ? 'private' : 'public'; |
||
| 97 | $response->header( |
||
| 98 | 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage |
||
| 99 | ); |
||
| 100 | |||
| 101 | $text = $this->getRawText(); |
||
| 102 | |||
| 103 | // Don't return a 404 response for CSS or JavaScript; |
||
| 104 | // 404s aren't generally cached and it would create |
||
| 105 | // extra hits when user CSS/JS are on and the user doesn't |
||
| 106 | // have the pages. |
||
| 107 | if ( $text === false && $contentType == 'text/x-wiki' ) { |
||
| 108 | $response->statusHeader( 404 ); |
||
| 109 | } |
||
| 110 | |||
| 111 | if ( !Hooks::run( 'RawPageViewBeforeOutput', [ &$this, &$text ] ) ) { |
||
| 112 | wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" ); |
||
| 113 | } |
||
| 114 | |||
| 115 | echo $text; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the text that should be returned, or false if the page or revision |
||
| 120 | * was not found. |
||
| 121 | * |
||
| 122 | * @return string|bool |
||
| 123 | */ |
||
| 124 | public function getRawText() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the ID of the revision that should used to get the text. |
||
| 189 | * |
||
| 190 | * @return int |
||
| 191 | */ |
||
| 192 | public function getOldId() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the content type to use for the response |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getContentType() { |
||
| 244 | } |
||
| 245 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: