Complex classes like Frame 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 Frame, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Frame implements Serializable |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $frame; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $fileContentsCache; |
||
23 | |||
24 | /** |
||
25 | * @var array[] |
||
26 | */ |
||
27 | protected $comments = []; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $application; |
||
33 | |||
34 | /** |
||
35 | * @param array[] |
||
36 | */ |
||
37 | 1 | public function __construct(array $frame) |
|
41 | |||
42 | /** |
||
43 | * @param bool $shortened |
||
44 | * @return string|null |
||
45 | */ |
||
46 | 2 | public function getFile($shortened = false) |
|
74 | |||
75 | /** |
||
76 | * @return int|null |
||
77 | */ |
||
78 | 2 | public function getLine() |
|
82 | |||
83 | /** |
||
84 | * @return string|null |
||
85 | */ |
||
86 | 2 | public function getClass() |
|
90 | |||
91 | /** |
||
92 | * @return string|null |
||
93 | */ |
||
94 | 2 | public function getFunction() |
|
98 | |||
99 | /** |
||
100 | * @return array |
||
101 | */ |
||
102 | 1 | public function getArgs() |
|
106 | |||
107 | /** |
||
108 | * Returns the full contents of the file for this frame, |
||
109 | * if it's known. |
||
110 | * @return string|null |
||
111 | */ |
||
112 | 3 | public function getFileContents() |
|
113 | { |
||
114 | 3 | if ($this->fileContentsCache === null && $filePath = $this->getFile()) { |
|
115 | // Leave the stage early when 'Unknown' or '[internal]' is passed |
||
116 | // this would otherwise raise an exception when |
||
117 | // open_basedir is enabled. |
||
118 | 3 | if ($filePath === "Unknown" || $filePath === '[internal]') { |
|
119 | 2 | return null; |
|
120 | } |
||
121 | |||
122 | try { |
||
123 | 1 | $this->fileContentsCache = file_get_contents($filePath); |
|
124 | 1 | } catch (ErrorException $exception) { |
|
125 | // Internal file paths of PHP extensions cannot be opened |
||
126 | } |
||
127 | 1 | } |
|
128 | |||
129 | 1 | return $this->fileContentsCache; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Adds a comment to this frame, that can be received and |
||
134 | * used by other handlers. For example, the PrettyPage handler |
||
135 | * can attach these comments under the code for each frame. |
||
136 | * |
||
137 | * An interesting use for this would be, for example, code analysis |
||
138 | * & annotations. |
||
139 | * |
||
140 | * @param string $comment |
||
141 | * @param string $context Optional string identifying the origin of the comment |
||
142 | */ |
||
143 | 2 | public function addComment($comment, $context = 'global') |
|
150 | |||
151 | /** |
||
152 | * Returns all comments for this frame. Optionally allows |
||
153 | * a filter to only retrieve comments from a specific |
||
154 | * context. |
||
155 | * |
||
156 | * @param string $filter |
||
157 | * @return array[] |
||
158 | */ |
||
159 | 2 | public function getComments($filter = null) |
|
171 | |||
172 | /** |
||
173 | * Returns the array containing the raw frame data from which |
||
174 | * this Frame object was built |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getRawFrame() |
||
182 | |||
183 | /** |
||
184 | * Returns the contents of the file for this frame as an |
||
185 | * array of lines, and optionally as a clamped range of lines. |
||
186 | * |
||
187 | * NOTE: lines are 0-indexed |
||
188 | * |
||
189 | * @example |
||
190 | * Get all lines for this file |
||
191 | * $frame->getFileLines(); // => array( 0 => '<?php', 1 => '...', ...) |
||
192 | * @example |
||
193 | * Get one line for this file, starting at line 10 (zero-indexed, remember!) |
||
194 | * $frame->getFileLines(9, 1); // array( 9 => '...' ) |
||
195 | * |
||
196 | * @throws InvalidArgumentException if $length is less than or equal to 0 |
||
197 | * @param int $start |
||
198 | * @param int $length |
||
199 | * @return string[]|null |
||
200 | */ |
||
201 | 2 | public function getFileLines($start = 0, $length = null) |
|
226 | |||
227 | /** |
||
228 | * Implements the Serializable interface, with special |
||
229 | * steps to also save the existing comments. |
||
230 | * |
||
231 | * @see Serializable::serialize |
||
232 | * @return string |
||
233 | */ |
||
234 | 1 | public function serialize() |
|
243 | |||
244 | /** |
||
245 | * Unserializes the frame data, while also preserving |
||
246 | * any existing comment data. |
||
247 | * |
||
248 | * @see Serializable::unserialize |
||
249 | * @param string $serializedFrame |
||
250 | */ |
||
251 | 1 | public function unserialize($serializedFrame) |
|
262 | |||
263 | /** |
||
264 | * Compares Frame against one another |
||
265 | * @param Frame $frame |
||
266 | * @return bool |
||
267 | */ |
||
268 | 1 | public function equals(Frame $frame) |
|
275 | |||
276 | /** |
||
277 | * Returns whether this frame belongs to the application or not. |
||
278 | * |
||
279 | * @return boolean |
||
280 | */ |
||
281 | public function isApplication() |
||
285 | |||
286 | /** |
||
287 | * Mark as an frame belonging to the application. |
||
288 | * |
||
289 | * @param boolean $application |
||
290 | */ |
||
291 | public function setApplication($application) |
||
295 | } |
||
296 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: