Complex classes like Filesystem 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 Filesystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Filesystem extends Object { |
||
9 | |||
10 | /** |
||
11 | * @config |
||
12 | * @var integer Integer |
||
13 | */ |
||
14 | private static $file_create_mask = 02775; |
||
15 | |||
16 | /** |
||
17 | * @config |
||
18 | * @var integer Integer |
||
19 | */ |
||
20 | private static $folder_create_mask = 02775; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected static $cache_folderModTime; |
||
26 | |||
27 | /** |
||
28 | * @config |
||
29 | * |
||
30 | * Array of file / folder regex expressions to exclude from the |
||
31 | * {@link Filesystem::sync()} |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private static $sync_blacklisted_patterns = array( |
||
36 | "/^\./", |
||
37 | "/^_combinedfiles$/i", |
||
38 | "/^_resampled$/i", |
||
39 | "/^web.config/i", |
||
40 | "/^Thumbs(.)/" |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * Create a folder on the filesystem, recursively. |
||
45 | * Uses {@link Filesystem::$folder_create_mask} to set filesystem permissions. |
||
46 | * Use {@link Folder::findOrMake()} to create a {@link Folder} database |
||
47 | * record automatically. |
||
48 | * |
||
49 | * @param String $folder Absolute folder path |
||
50 | */ |
||
51 | public static function makeFolder($folder) { |
||
55 | |||
56 | /** |
||
57 | * Remove a directory and all subdirectories and files. |
||
58 | * |
||
59 | * @param String $folder Absolute folder path |
||
60 | * @param Boolean $contentsOnly If this is true then the contents of the folder will be removed but not the |
||
61 | * folder itself |
||
62 | */ |
||
63 | public static function removeFolder($folder, $contentsOnly = false) { |
||
64 | |||
65 | // remove a file encountered by a recursive call. |
||
66 | if(is_file($folder) || is_link($folder)) { |
||
67 | unlink($folder); |
||
68 | } else { |
||
69 | $dir = opendir($folder); |
||
70 | while($file = readdir($dir)) { |
||
71 | if(($file == '.' || $file == '..')) continue; |
||
72 | else { |
||
73 | self::removeFolder($folder . '/' . $file); |
||
74 | } |
||
75 | } |
||
76 | closedir($dir); |
||
77 | if(!$contentsOnly) rmdir($folder); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Remove a directory, but only if it is empty. |
||
83 | * |
||
84 | * @param string $folder Absolute folder path |
||
85 | * @param boolean $recursive Remove contained empty folders before attempting to remove this one |
||
86 | * @return boolean True on success, false on failure. |
||
87 | */ |
||
88 | public static function remove_folder_if_empty($folder, $recursive = true) { |
||
89 | if (!is_readable($folder)) return false; |
||
90 | $handle = opendir($folder); |
||
91 | while (false !== ($entry = readdir($handle))) { |
||
92 | if ($entry != "." && $entry != "..") { |
||
93 | // if an empty folder is detected, remove that one first and move on |
||
94 | if($recursive && is_dir($entry) && self::remove_folder_if_empty($entry)) continue; |
||
95 | // if a file was encountered, or a subdirectory was not empty, return false. |
||
96 | return false; |
||
97 | } |
||
98 | } |
||
99 | // if we are still here, the folder is empty. |
||
100 | rmdir($folder); |
||
101 | return true; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Cleanup function to reset all the Filename fields. Visit File/fixfiles to call. |
||
106 | */ |
||
107 | public function fixfiles() { |
||
108 | if(!Permission::check('ADMIN')) return Security::permissionFailure($this); |
||
|
|||
109 | |||
110 | $files = DataObject::get("File"); |
||
111 | foreach($files as $file) { |
||
112 | $file->updateFilesystem(); |
||
113 | echo "<li>", $file->Filename; |
||
114 | $file->write(); |
||
115 | } |
||
116 | echo "<p>Done!"; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Return the most recent modification time of anything in the folder. |
||
121 | * |
||
122 | * @param $folder The folder, relative to the site root |
||
123 | * @param $extensionList An option array of file extensions to limit the search to |
||
124 | * @return String Same as filemtime() format. |
||
125 | */ |
||
126 | public static function folderModTime($folder, $extensionList = null, $recursiveCall = false) { |
||
127 | //$cacheID = $folder . ',' . implode(',', $extensionList); |
||
128 | //if(!$recursiveCall && self::$cache_folderModTime[$cacheID]) return self::$cache_folderModTime[$cacheID]; |
||
129 | |||
130 | $modTime = 0; |
||
131 | if(!Filesystem::isAbsolute($folder)) $folder = Director::baseFolder() . '/' . $folder; |
||
132 | |||
133 | $items = scandir($folder); |
||
134 | foreach($items as $item) { |
||
135 | if($item[0] != '.') { |
||
136 | // Recurse into folders |
||
137 | if(is_dir("$folder/$item")) { |
||
138 | $modTime = max($modTime, self::folderModTime("$folder/$item", $extensionList, true)); |
||
139 | |||
140 | // Check files |
||
141 | } else { |
||
142 | if($extensionList) $extension = strtolower(substr($item,strrpos($item,'.')+1)); |
||
143 | if(!$extensionList || in_array($extension, $extensionList)) { |
||
144 | $modTime = max($modTime, filemtime("$folder/$item")); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | //if(!$recursiveCall) self::$cache_folderModTime[$cacheID] = $modTime; |
||
151 | return $modTime; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns true if the given filename is an absolute file reference. |
||
156 | * Works on Linux and Windows. |
||
157 | * |
||
158 | * @param String $filename Absolute or relative filename, with or without path. |
||
159 | * @return Boolean |
||
160 | */ |
||
161 | public static function isAbsolute($filename) { |
||
165 | |||
166 | /** |
||
167 | * This function ensures the file table is correct with the files in the assets folder. |
||
168 | * |
||
169 | * If a Folder record ID is given, all of that folder's children will be synchronised. |
||
170 | * If the given Folder ID isn't found, or not specified at all, then everything will |
||
171 | * be synchronised from the root folder (singleton Folder). |
||
172 | * |
||
173 | * See {@link File->updateFilesystem()} to sync properties of a single database record |
||
174 | * back to the equivalent filesystem record. |
||
175 | * |
||
176 | * @param int $folderID Folder ID to sync along with all it's children |
||
177 | * @param Boolean $syncLinkTracking Determines if the link tracking data should also |
||
178 | * be updated via {@link SiteTree->syncLinkTracking()}. Setting this to FALSE |
||
179 | * means that broken links inside page content are not noticed, at least until the next |
||
180 | * call to {@link SiteTree->write()} on this page. |
||
181 | * @return string Localized status message |
||
182 | */ |
||
183 | public static function sync($folderID = null, $syncLinkTracking = true) { |
||
241 | |||
242 | } |
||
243 | |||
244 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: