WP_Filesystem_MockFS::dirlist()   C
last analyzed

Complexity

Conditions 14
Paths 30

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 27
nc 30
nop 3
dl 0
loc 47
rs 5.0622
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
class WP_Filesystem_MockFS extends WP_Filesystem_Base
3
{
4
    private $cwd;
5
6
    // Holds a array of objects which contain an array of objects, etc.
7
    private $fs = null;
8
9
    // Holds a array of /path/to/file.php and /path/to/dir/ map to an object in $fs above
10
    // a fast more efficient way of determining if a path exists, and access to that node
11
    private $fs_map = array();
12
13
    public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc.
14
    public $errors = array();
15
    public $method = 'MockFS';
16
17
    function __construct() 
18
    {
19
    }
20
21
    function connect() 
22
    {
23
        return true;
24
    }
25
26
    // Copy of core's function, but accepts a path.
27
    function abspath( $path = false ) 
28
    {
29
        if (! $path ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
30
              $path = ABSPATH;
31
        }
32
        $folder = $this->find_folder($path);
33
34
        // Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
35
        if (! $folder && $this->is_dir('/wp-includes') ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
36
              $folder = '/';
37
        }
38
        return $folder;
39
    }
40
41
    // Mock FS specific functions:
42
43
    /**
44
     * Sets initial filesystem environment and/or clears the current environment.
45
     * Can also be passed the initial filesystem to be setup which is passed to self::setfs()
46
     */
47
    function init( $paths = '', $home_dir = '/' ) 
48
    {
49
        $this->fs = new MockFS_Directory_Node('/');
50
        $this->fs_map = array(
51
         '/' => $this->fs,
52
        );
53
        $this->cache = array(); // Used by find_folder() and friends
54
        $this->cwd = isset($this->fs_map[ $home_dir ]) ? $this->fs_map[ $home_dir ] : '/';
55
        $this->setfs($paths);
56
    }
57
58
    /**
59
     * "Bulk Loads" a filesystem into the internal virtual filesystem
60
     */
61
    function setfs( $paths ) 
62
    {
63
        if (! is_array($paths) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
64
              $paths = explode("\n", $paths);
65
        }
66
67
        $paths = array_filter(array_map('trim', $paths));
68
69
        foreach ( $paths as $path ) {
70
            // Allow for comments
71
            if ('#' == $path[0] ) {
72
                continue;
73
            }
74
75
            // Directories
76
            if ('/' == $path[ strlen($path) -1 ] ) {
77
                $this->mkdir($path);
78
            } else {
79
                // Files (with dummy content for now)
80
                $this->put_contents($path, 'This is a test file');
81
            }
82
        }
83
84
    }
85
86
    /**
87
     * Locates a filesystem "node"
88
     */
89
    private function locate_node( $path ) 
90
    {
91
        return isset($this->fs_map[ $path ]) ? $this->fs_map[ $path ] : false;
92
    }
93
94
    /**
95
     * Locates a filesystem node for the parent of the given item
96
     */
97
    private function locate_parent_node( $path ) 
98
    {
99
        $dirname = str_replace('\\', '/', dirname($path));
100
        return $this->locate_node(trailingslashit($dirname));
101
    }
102
103
    // Here starteth the WP_Filesystem functions.
104
105
    function mkdir( $path, /* Optional args are ignored */ $chmod = false, $chown = false, $chgrp = false ) 
106
    {
107
        $path = trailingslashit($path);
108
109
        $parent_node = $this->locate_parent_node($path);
110
        if (! $parent_node ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
111
            $dirname = str_replace('\\', '/', dirname($path));
112
            $this->mkdir($dirname);
113
            $parent_node = $this->locate_parent_node($path);
114
            if (! $parent_node ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
115
                return false;
116
            }
117
        }
118
119
        $node = new MockFS_Directory_Node($path);
120
121
        $parent_node->children[ $node->name ] = $node;
122
        $this->fs_map[ $path ] = $node;
123
124
        return true;
125
    }
126
127
    function put_contents( $path, $contents = '', $mode = null ) 
128
    {
129
        if (! $this->is_dir(dirname($path)) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
130
              $this->mkdir(dirname($path));
131
        }
132
133
        $parent = $this->locate_parent_node($path);
134
        $new_file = new MockFS_File_Node($path, $contents);
135
136
        $parent->children[ $new_file->name ] = $new_file;
137
        $this->fs_map[ $path ] = $new_file;
138
    }
139
140
    function get_contents( $file ) 
141
    {
142
        if (! $this->is_file($file) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
143
              return false;
144
        }
145
        return $this->fs_map[ $file ]->contents;
146
    }
147
148
    function cwd() 
149
    {
150
        return $this->cwd->path;
151
    }
152
153
    function chdir( $path ) 
154
    {
155
        if (! isset($this->fs_map[ $path ]) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
156
              return false;
157
        }
158
159
        $this->cwd = $this->fs_map[ $path ];
160
        return true;
161
    }
162
163
    function exists( $path ) 
164
    {
165
        return isset($this->fs_map[ $path ]) || isset($this->fs_map[ trailingslashit($path) ]);
166
    }
167
168
    function is_file( $file ) 
169
    {
170
        return isset($this->fs_map[ $file ]) && $this->fs_map[ $file ]->is_file();
171
    }
172
173
    function is_dir( $path ) 
174
    {
175
        $path = trailingslashit($path);
176
177
        return isset($this->fs_map[ $path ]) && $this->fs_map[ $path ]->is_dir();
178
    }
179
180
    function dirlist( $path = '.', $include_hidden = true, $recursive = false ) 
181
    {
182
183
        if (empty($path) || '.' == $path ) {
184
              $path = $this->cwd();
185
        }
186
187
        if (! $this->exists($path) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
188
              return false;
189
        }
190
191
        $limit_file = false;
192
        if ($this->is_file($path) ) {
193
            $limit_file = $this->locate_node($path)->name;
194
            $path = dirname($path) . '/';
195
        }
196
197
        $ret = array();
198
        foreach ( $this->fs_map[ $path ]->children as $entry ) {
199
            if ('.' == $entry->name || '..' == $entry->name ) {
200
                continue;
201
            }
202
203
            if (! $include_hidden && '.' == $entry->name ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
204
                continue;
205
            }
206
207
            if ($limit_file && $entry->name != $limit_file ) {
208
                continue;
209
            }
210
211
            $struc = array();
212
            $struc['name'] = $entry->name;
213
            $struc['type'] = $entry->type;
214
215
            if ('d' == $struc['type'] ) {
216
                if ($recursive ) {
217
                     $struc['files'] = $this->dirlist(trailingslashit($path) . trailingslashit($struc['name']), $include_hidden, $recursive);
218
                } else {
219
                     $struc['files'] = array();
220
                }
221
            }
222
223
            $ret[ $entry->name ] = $struc;
224
        }
225
        return $ret;
226
    }
227
228
}
229
230
class MockFS_Node
231
{
232
    public $name; // The "name" of the entry, does not include a slash (exception, root)
233
    public $type; // The type of the entry 'f' for file, 'd' for Directory
234
    public $path; // The full path to the entry.
235
236
    function __construct( $path ) 
237
    {
238
        $this->path = $path;
239
        $this->name = basename($path);
240
    }
241
242
    function is_file() 
243
    {
244
        return $this->type == 'f';
245
    }
246
247
    function is_dir() 
248
    {
249
        return $this->type == 'd';
250
    }
251
}
252
253
class MockFS_Directory_Node extends MockFS_Node
254
{
255
    public $type = 'd';
256
    public $children = array(); // The child nodes of this directory
257
}
258
259
class MockFS_File_Node extends MockFS_Node
260
{
261
    public $type = 'f';
262
    public $contents = ''; // The contents of the file
263
264
    function __construct( $path, $contents = '' ) 
265
    {
266
        parent::__construct($path);
267
        $this->contents = $contents;
268
    }
269
}