Complex classes like Stream 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 Stream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Stream implements StreamInterface |
||
9 | { |
||
10 | |||
11 | protected $handle; |
||
12 | |||
13 | /** |
||
14 | * Stream constructor. |
||
15 | * @param $stream |
||
16 | */ |
||
17 | public function __construct($stream) |
||
25 | |||
26 | /** |
||
27 | * @param $stream |
||
28 | * @return bool |
||
29 | */ |
||
30 | protected function isStream($stream) |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | public function __toString() |
||
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | public function close() |
||
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | public function detach() |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function getSize() |
||
92 | |||
93 | /** |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | public function tell() |
||
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | public function eof() |
||
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | public function isSeekable() |
||
132 | |||
133 | /** |
||
134 | * @inheritdoc |
||
135 | */ |
||
136 | public function seek($offset, $whence = SEEK_SET) |
||
144 | |||
145 | /** |
||
146 | * @inheritdoc |
||
147 | */ |
||
148 | public function rewind() |
||
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | public function isWritable() |
||
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | public function write($string) |
||
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | public function isReadable() |
||
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | public function read($length) |
||
240 | |||
241 | /** |
||
242 | * @inheritdoc |
||
243 | */ |
||
244 | public function getContents() |
||
252 | |||
253 | /** |
||
254 | * @inheritdoc |
||
255 | */ |
||
256 | public function getMetadata($key = null) |
||
270 | } |
||
271 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.