Complex classes like ApiFormatBase 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 ApiFormatBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | abstract class ApiFormatBase extends ApiBase { |
||
33 | private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp; |
||
34 | private $mBuffer, $mDisabled = false; |
||
35 | private $mIsWrappedHtml = false; |
||
36 | private $mHttpStatus = false; |
||
37 | protected $mForceDefaultParams = false; |
||
38 | |||
39 | /** |
||
40 | * If $format ends with 'fm', pretty-print the output in HTML. |
||
41 | * @param ApiMain $main |
||
42 | * @param string $format Format name |
||
43 | */ |
||
44 | public function __construct( ApiMain $main, $format ) { |
||
56 | |||
57 | /** |
||
58 | * Overriding class returns the MIME type that should be sent to the client. |
||
59 | * |
||
60 | * When getIsHtml() returns true, the return value here is used for syntax |
||
61 | * highlighting but the client sees text/html. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | abstract public function getMimeType(); |
||
66 | |||
67 | /** |
||
68 | * Get the internal format name |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getFormat() { |
||
74 | |||
75 | /** |
||
76 | * Returns true when the HTML pretty-printer should be used. |
||
77 | * The default implementation assumes that formats ending with 'fm' |
||
78 | * should be formatted in HTML. |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function getIsHtml() { |
||
84 | |||
85 | /** |
||
86 | * Returns true when the special wrapped mode is enabled. |
||
87 | * @since 1.27 |
||
88 | * @return bool |
||
89 | */ |
||
90 | protected function getIsWrappedHtml() { |
||
93 | |||
94 | /** |
||
95 | * Disable the formatter. |
||
96 | * |
||
97 | * This causes calls to initPrinter() and closePrinter() to be ignored. |
||
98 | */ |
||
99 | public function disable() { |
||
102 | |||
103 | /** |
||
104 | * Whether the printer is disabled |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function isDisabled() { |
||
110 | |||
111 | /** |
||
112 | * Whether this formatter can handle printing API errors. |
||
113 | * |
||
114 | * If this returns false, then on API errors the default printer will be |
||
115 | * instantiated. |
||
116 | * @since 1.23 |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function canPrintErrors() { |
||
122 | |||
123 | /** |
||
124 | * Ignore request parameters, force a default. |
||
125 | * |
||
126 | * Used as a fallback if errors are being thrown. |
||
127 | * @since 1.26 |
||
128 | */ |
||
129 | public function forceDefaultParams() { |
||
132 | |||
133 | /** |
||
134 | * Overridden to honor $this->forceDefaultParams(), if applicable |
||
135 | * @since 1.26 |
||
136 | */ |
||
137 | protected function getParameterFromSettings( $paramName, $paramSettings, $parseLimit ) { |
||
150 | |||
151 | /** |
||
152 | * Set the HTTP status code to be used for the response |
||
153 | * @since 1.29 |
||
154 | * @param int $code |
||
155 | */ |
||
156 | public function setHttpStatus( $code ) { |
||
167 | |||
168 | /** |
||
169 | * Initialize the printer function and prepare the output headers. |
||
170 | * @param bool $unused Always false since 1.25 |
||
171 | */ |
||
172 | public function initPrinter( $unused = false ) { |
||
195 | |||
196 | /** |
||
197 | * Finish printing and output buffered data. |
||
198 | */ |
||
199 | public function closePrinter() { |
||
296 | |||
297 | /** |
||
298 | * Append text to the output buffer. |
||
299 | * @param string $text |
||
300 | */ |
||
301 | public function printText( $text ) { |
||
304 | |||
305 | /** |
||
306 | * Get the contents of the buffer. |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getBuffer() { |
||
312 | |||
313 | public function getAllowedParams() { |
||
324 | |||
325 | protected function getExamplesMessages() { |
||
331 | |||
332 | public function getHelpUrls() { |
||
335 | |||
336 | } |
||
337 | |||
342 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.