Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ResourceStreamWrapper 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 ResourceStreamWrapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class ResourceStreamWrapper implements StreamWrapper |
||
50 | { |
||
51 | const DEVICE_ASSOC = 'dev'; |
||
52 | |||
53 | const DEVICE_NUM = 0; |
||
54 | |||
55 | const INODE_ASSOC = 'ino'; |
||
56 | |||
57 | const INODE_NUM = 1; |
||
58 | |||
59 | const MODE_ASSOC = 'mode'; |
||
60 | |||
61 | const MODE_NUM = 2; |
||
62 | |||
63 | const NUM_LINKS_ASSOC = 'nlink'; |
||
64 | |||
65 | const NUM_LINK_NUM = 3; |
||
66 | |||
67 | const UID_ASSOC = 'uid'; |
||
68 | |||
69 | const UID_NUM = 4; |
||
70 | |||
71 | const GID_ASSOC = 'gid'; |
||
72 | |||
73 | const GID_NUM = 5; |
||
74 | |||
75 | const DEVICE_TYPE_ASSOC = 'rdev'; |
||
76 | |||
77 | const DEVICE_TYPE_NUM = 6; |
||
78 | |||
79 | const SIZE_ASSOC = 'size'; |
||
80 | |||
81 | const SIZE_NUM = 7; |
||
82 | |||
83 | const ACCESS_TIME_ASSOC = 'atime'; |
||
84 | |||
85 | const ACCESS_TIME_NUM = 8; |
||
86 | |||
87 | const MODIFY_TIME_ASSOC = 'mtime'; |
||
88 | |||
89 | const MODIFY_TIME_NUM = 9; |
||
90 | |||
91 | const CHANGE_TIME_ASSOC = 'ctime'; |
||
92 | |||
93 | const CHANGE_TIME_NUM = 10; |
||
94 | |||
95 | const BLOCK_SIZE_ASSOC = 'blksize'; |
||
96 | |||
97 | const BLOCK_SIZE_NUM = 11; |
||
98 | |||
99 | const NUM_BLOCKS_ASSOC = 'blocks'; |
||
100 | |||
101 | const NUM_BLOCKS_NUM = 12; |
||
102 | |||
103 | /** |
||
104 | * @var array |
||
105 | */ |
||
106 | private static $defaultStat = array( |
||
107 | self::DEVICE_ASSOC => -1, |
||
108 | self::DEVICE_NUM => -1, |
||
109 | self::INODE_ASSOC => -1, |
||
110 | self::INODE_NUM => -1, |
||
111 | self::MODE_ASSOC => -1, |
||
112 | self::MODE_NUM => -1, |
||
113 | self::NUM_LINKS_ASSOC => -1, |
||
114 | self::NUM_LINK_NUM => -1, |
||
115 | self::UID_ASSOC => 0, |
||
116 | self::UID_NUM => 0, |
||
117 | self::GID_ASSOC => 0, |
||
118 | self::GID_NUM => 0, |
||
119 | self::DEVICE_TYPE_ASSOC => -1, |
||
120 | self::DEVICE_TYPE_NUM => -1, |
||
121 | self::SIZE_ASSOC => 0, |
||
122 | self::SIZE_NUM => 0, |
||
123 | self::ACCESS_TIME_ASSOC => -1, |
||
124 | self::ACCESS_TIME_NUM => -1, |
||
125 | self::MODIFY_TIME_ASSOC => -1, |
||
126 | self::MODIFY_TIME_NUM => -1, |
||
127 | self::CHANGE_TIME_ASSOC => -1, |
||
128 | self::CHANGE_TIME_NUM => -1, |
||
129 | self::BLOCK_SIZE_ASSOC => -1, |
||
130 | self::BLOCK_SIZE_NUM => -1, |
||
131 | self::NUM_BLOCKS_ASSOC => 0, |
||
132 | self::NUM_BLOCKS_NUM => 0, |
||
133 | ); |
||
134 | |||
135 | /** |
||
136 | * @var ResourceRepository[]|callable[] |
||
137 | */ |
||
138 | private static $repos; |
||
139 | |||
140 | /** |
||
141 | * @var resource |
||
142 | */ |
||
143 | private $handle; |
||
144 | |||
145 | /** |
||
146 | * @var ResourceCollectionIterator |
||
147 | */ |
||
148 | private $childIterator; |
||
149 | |||
150 | /** |
||
151 | * Registers a repository as PHP stream wrapper. |
||
152 | * |
||
153 | * The resources of the repository can subsequently be accessed with PHP's |
||
154 | * file system by prefixing the resource paths with the registered URI |
||
155 | * scheme: |
||
156 | * |
||
157 | * ```php |
||
158 | * ResourceStreamWrapper::register('puli', $repo); |
||
159 | * |
||
160 | * // /app/css/style.css |
||
161 | * $contents = file_get_contents('puli:///app/css/style.css'); |
||
162 | * ``` |
||
163 | * |
||
164 | * Instead of passing a repository, you can also pass a callable. The |
||
165 | * callable is executed when the repository is accessed for the first time |
||
166 | * and should return a valid {@link ResourceRepository} instance. |
||
167 | * |
||
168 | * @param string $scheme The URI scheme. |
||
169 | * @param ResourceRepository|callable $repositoryFactory The repository to use. |
||
170 | * |
||
171 | * @throws StreamWrapperException If a repository was previously registered |
||
172 | * for the same scheme. Call |
||
173 | * {@link unregister()} to unregister the |
||
174 | * scheme first. |
||
175 | */ |
||
176 | 45 | public static function register($scheme, $repositoryFactory) |
|
177 | { |
||
178 | if (!$repositoryFactory instanceof ResourceRepository |
||
179 | 45 | && !is_callable($repositoryFactory)) { |
|
180 | 1 | throw new InvalidArgumentException(sprintf( |
|
181 | 'The repository factory should be a callable or an instance '. |
||
182 | 1 | 'of ResourceRepository. Got: %s', |
|
183 | $repositoryFactory |
||
184 | 1 | )); |
|
185 | } |
||
186 | |||
187 | 44 | Assert::string($scheme, 'The scheme must be a string. Got: %s'); |
|
188 | 43 | Assert::alnum($scheme, 'The scheme %s should consist of letters and digits only.'); |
|
189 | 42 | Assert::startsWithLetter($scheme, 'The scheme %s should start with a letter.'); |
|
190 | |||
191 | 41 | if (isset(self::$repos[$scheme])) { |
|
192 | 1 | throw new StreamWrapperException(sprintf( |
|
193 | 1 | 'The scheme "%s" has already been registered.', |
|
194 | $scheme |
||
195 | 1 | )); |
|
196 | } |
||
197 | |||
198 | 41 | self::$repos[$scheme] = $repositoryFactory; |
|
199 | |||
200 | 41 | stream_wrapper_register($scheme, __CLASS__); |
|
201 | 41 | } |
|
202 | |||
203 | /** |
||
204 | * Unregisters the given scheme. |
||
205 | * |
||
206 | * Unknown schemes are ignored. |
||
207 | * |
||
208 | * @param string $scheme A URI scheme. |
||
209 | */ |
||
210 | 47 | public static function unregister($scheme) |
|
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | * |
||
224 | * @internal |
||
225 | */ |
||
226 | 3 | public function dir_opendir($uri, $options) |
|
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | * |
||
244 | * @internal |
||
245 | */ |
||
246 | 2 | public function dir_closedir() |
|
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | * |
||
256 | * @internal |
||
257 | */ |
||
258 | 2 | public function dir_readdir() |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | * |
||
274 | * @internal |
||
275 | */ |
||
276 | 1 | public function dir_rewinddir() |
|
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | * |
||
286 | * @internal |
||
287 | */ |
||
288 | 1 | public function mkdir($uri, $mode, $options) |
|
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | * |
||
300 | * @internal |
||
301 | */ |
||
302 | 1 | public function rename($uriFrom, $uriTo) |
|
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | * |
||
320 | * @internal |
||
321 | */ |
||
322 | 1 | public function rmdir($uri, $options) |
|
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | * |
||
342 | * @internal |
||
343 | */ |
||
344 | 2 | public function stream_cast($castAs) |
|
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | * |
||
352 | * @internal |
||
353 | */ |
||
354 | 20 | public function stream_close() |
|
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | * |
||
364 | * @internal |
||
365 | */ |
||
366 | 8 | public function stream_eof() |
|
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | * |
||
376 | * @internal |
||
377 | */ |
||
378 | 20 | public function stream_flush() |
|
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | * |
||
388 | * @internal |
||
389 | */ |
||
390 | 2 | public function stream_lock($operation) |
|
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | * |
||
401 | * @internal |
||
402 | */ |
||
403 | public function stream_metadata($uri, $option) |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | * |
||
441 | * @internal |
||
442 | */ |
||
443 | 32 | public function stream_open($uri, $mode, $options, &$openedPath) |
|
479 | |||
480 | /** |
||
481 | * {@inheritdoc} |
||
482 | * |
||
483 | * @internal |
||
484 | */ |
||
485 | 8 | public function stream_read($length) |
|
491 | |||
492 | /** |
||
493 | * {@inheritdoc} |
||
494 | * |
||
495 | * @internal |
||
496 | */ |
||
497 | 6 | public function stream_seek($offset, $whence = SEEK_SET) |
|
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | * |
||
507 | * @internal |
||
508 | */ |
||
509 | public function stream_set_option($option, $arg1, $arg2) |
||
513 | |||
514 | /** |
||
515 | * {@inheritdoc} |
||
516 | * |
||
517 | * @internal |
||
518 | */ |
||
519 | 4 | public function stream_stat() |
|
525 | |||
526 | /** |
||
527 | * {@inheritdoc} |
||
528 | * |
||
529 | * @internal |
||
530 | */ |
||
531 | 6 | public function stream_tell() |
|
537 | |||
538 | /** |
||
539 | * {@inheritdoc} |
||
540 | * |
||
541 | * @internal |
||
542 | */ |
||
543 | public function stream_truncate($newSize) |
||
549 | |||
550 | /** |
||
551 | * {@inheritdoc} |
||
552 | * |
||
553 | * @internal |
||
554 | */ |
||
555 | public function stream_write($data) |
||
561 | |||
562 | /** |
||
563 | * {@inheritdoc} |
||
564 | * |
||
565 | * @internal |
||
566 | */ |
||
567 | 2 | public function unlink($uri) |
|
575 | |||
576 | /** |
||
577 | * {@inheritdoc} |
||
578 | * |
||
579 | * @internal |
||
580 | */ |
||
581 | 1 | public function url_stat($uri, $flags) |
|
617 | |||
618 | /** |
||
619 | * Constructs (if necessary) and returns the repository for the given scheme. |
||
620 | * |
||
621 | * @param string $scheme A URI scheme. |
||
622 | * |
||
623 | * @return ResourceRepository The resource repository. |
||
624 | * |
||
625 | * @throws RepositoryFactoryException If the callable did not return an |
||
626 | * instance of {@link ResourceRepository}. |
||
627 | * @throws StreamWrapperException If the scheme is not supported. |
||
628 | */ |
||
629 | 29 | private function getRepository($scheme) |
|
657 | } |
||
658 |