Complex classes like WP_Filesystem_MockFS 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 WP_Filesystem_MockFS, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
2 | class WP_Filesystem_MockFS extends WP_Filesystem_Base { |
||
3 | private $cwd; |
||
4 | |||
5 | // Holds a array of objects which contain an array of objects, etc. |
||
6 | private $fs = null; |
||
7 | |||
8 | // Holds a array of /path/to/file.php and /path/to/dir/ map to an object in $fs above |
||
9 | // a fast more efficient way of determining if a path exists, and access to that node |
||
10 | private $fs_map = array(); |
||
11 | |||
12 | public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc. |
||
13 | public $errors = array(); |
||
14 | public $method = 'MockFS'; |
||
15 | |||
16 | function __construct() {} |
||
17 | |||
18 | function connect() { |
||
21 | |||
22 | // Copy of core's function, but accepts a path. |
||
23 | function abspath( $path = false ) { |
||
33 | |||
34 | // Mock FS specific functions: |
||
35 | |||
36 | /** |
||
37 | * Sets initial filesystem environment and/or clears the current environment. |
||
38 | * Can also be passed the initial filesystem to be setup which is passed to self::setfs() |
||
39 | */ |
||
40 | function init( $paths = '', $home_dir = '/' ) { |
||
49 | |||
50 | /** |
||
51 | * "Bulk Loads" a filesystem into the internal virtual filesystem |
||
52 | */ |
||
53 | function setfs( $paths ) { |
||
72 | |||
73 | /** |
||
74 | * Locates a filesystem "node" |
||
75 | */ |
||
76 | private function locate_node( $path ) { |
||
79 | |||
80 | /** |
||
81 | * Locates a filesystem node for the parent of the given item |
||
82 | */ |
||
83 | private function locate_parent_node( $path ) { |
||
87 | |||
88 | // Here starteth the WP_Filesystem functions. |
||
89 | |||
90 | function mkdir( $path, /* Optional args are ignored */ $chmod = false, $chown = false, $chgrp = false ) { |
||
109 | |||
110 | function put_contents( $path, $contents = '', $mode = null ) { |
||
120 | |||
121 | function get_contents( $file ) { |
||
126 | |||
127 | function cwd() { |
||
130 | |||
131 | function chdir( $path ) { |
||
138 | |||
139 | function exists( $path ) { |
||
142 | |||
143 | function is_file( $file ) { |
||
146 | |||
147 | function is_dir( $path ) { |
||
152 | |||
153 | function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { |
||
193 | |||
194 | } |
||
195 | |||
228 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.