These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TechDivision\Import\Adapter\PhpFilesystemAdapter |
||
5 | * |
||
6 | * NOTICE OF LICENSE |
||
7 | * |
||
8 | * This source file is subject to the Open Software License (OSL 3.0) |
||
9 | * that is available through the world-wide-web at this URL: |
||
10 | * http://opensource.org/licenses/osl-3.0.php |
||
11 | * |
||
12 | * PHP version 5 |
||
13 | * |
||
14 | * @author Tim Wagner <[email protected]> |
||
15 | * @copyright 2016 TechDivision GmbH <[email protected]> |
||
16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
17 | * @link https://github.com/techdivision/import |
||
18 | * @link http://www.techdivision.com |
||
19 | */ |
||
20 | |||
21 | namespace TechDivision\Import\Adapter; |
||
22 | |||
23 | /** |
||
24 | * Adapter for a PHP filesystem implementation. |
||
25 | * |
||
26 | * @author Tim Wagner <[email protected]> |
||
27 | * @copyright 2016 TechDivision GmbH <[email protected]> |
||
28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
29 | * @link https://github.com/techdivision/import |
||
30 | * @link http://www.techdivision.com |
||
31 | */ |
||
32 | class PhpFilesystemAdapter implements PhpFilesystemAdapterInterface |
||
0 ignored issues
–
show
|
|||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Creates a new directroy. |
||
37 | * |
||
38 | * @param string $pathname The directory path |
||
39 | * @param integer $mode The mode is 0700 by default, which means the widest possible access |
||
40 | * |
||
41 | * @return boolean TRUE on success, else FALSE |
||
42 | * @link http://php.net/mkdir |
||
43 | */ |
||
44 | public function mkdir($pathname, $mode = 0755) |
||
45 | { |
||
46 | return mkdir($pathname, $mode, true); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Query whether or not the passed filename exists. |
||
51 | * |
||
52 | * @param string $filename The filename to query |
||
53 | * |
||
54 | * @return boolean TRUE if the passed filename exists, else FALSE |
||
55 | * @link http://php.net/is_file |
||
56 | */ |
||
57 | public function isFile($filename) |
||
58 | { |
||
59 | return is_file($filename); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Tells whether the filename is a directory. |
||
64 | * |
||
65 | * @param string $filename Path to the file |
||
66 | * |
||
67 | * @return TRUE if the filename exists and is a directory, else FALSE |
||
68 | * @link http://php.net/is_dir |
||
69 | */ |
||
70 | public function isDir($filename) |
||
71 | { |
||
72 | return is_dir($filename); |
||
0 ignored issues
–
show
The return type of
return is_dir($filename); (boolean ) is incompatible with the return type declared by the interface TechDivision\Import\Adap...AdapterInterface::isDir of type TechDivision\Import\Adapter\TRUE .
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function
Loading history...
|
|||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Creates an empty file with the passed filename. |
||
77 | * |
||
78 | * @param string $filename The name of the file to create |
||
79 | * |
||
80 | * @return boolean TRUE if the file can be created, else FALSE |
||
81 | */ |
||
82 | public function touch($filename) |
||
83 | { |
||
84 | return touch($filename); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Renames a file or directory. |
||
89 | * |
||
90 | * @param string $oldname The old name |
||
91 | * @param string $newname The new name |
||
92 | * |
||
93 | * @return boolean TRUE on success, else FALSE |
||
94 | * @link http://php.net/rename |
||
95 | */ |
||
96 | public function rename($oldname, $newname) |
||
97 | { |
||
98 | return rename($oldname, $newname); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Writes the passed data to file with the passed name. |
||
103 | * |
||
104 | * @param string $filename The name of the file to write the data to |
||
105 | * @param string $data The data to write to the file |
||
106 | * |
||
107 | * @return number The number of bytes written to the file |
||
108 | * @link http://php.net/file_put_contents |
||
109 | */ |
||
110 | public function write($filename, $data) |
||
111 | { |
||
112 | return file_put_contents($filename, $data); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Delete the file with the passed name. |
||
117 | * |
||
118 | * @param string $filename The name of the file to be deleted |
||
119 | * |
||
120 | * @return boolean TRUE on success, else FALSE |
||
121 | */ |
||
122 | public function delete($filename) |
||
123 | { |
||
124 | return unlink($filename); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Copy's a file from source to destination. |
||
129 | * |
||
130 | * @param string $src The source file |
||
131 | * @param string $dest The destination file |
||
132 | * |
||
133 | * @return boolean TRUE on success, else FALSE |
||
134 | * @link http://php.net/copy |
||
135 | */ |
||
136 | public function copy($src, $dest) |
||
137 | { |
||
138 | return copy($src, $dest); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * List the filenames of a directory. |
||
143 | * |
||
144 | * @param string $directory The directory to list |
||
145 | * @param boolean $recursive Whether to list recursively |
||
146 | * |
||
147 | * @return array A list of filenames |
||
148 | */ |
||
149 | public function listContents($directory = '', $recursive = false) |
||
150 | { |
||
151 | |||
152 | // clear the filecache |
||
153 | clearstatcache(); |
||
154 | |||
155 | // parse the directory |
||
156 | $files = $this->glob($pattern = sprintf('%s/*', $directory), 0); |
||
157 | |||
158 | // parse all subdirectories, if recursive parsing is wanted |
||
159 | if ($recursive !== false) { |
||
160 | // load the directories |
||
161 | $dirs = $this->glob(dirname($pattern). DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_BRACE); |
||
162 | // iterate over the subdirectories for its files |
||
163 | foreach ($dirs as $dir) { |
||
164 | $files = array_merge($files, $this->listContents($dir . DIRECTORY_SEPARATOR . basename($pattern), $recursive)); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // return the array with the files matching the glob pattern |
||
169 | return $files; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Removes the passed directory recursively. |
||
174 | * |
||
175 | * @param string $src Name of the directory to remove |
||
176 | * @param boolean $recursive TRUE if the directory has to be deleted recursive, else FALSE |
||
177 | * |
||
178 | * @return void |
||
179 | * @throws \Exception Is thrown, if the directory can not be removed |
||
180 | */ |
||
181 | public function removeDir($src, $recursive = false) |
||
182 | { |
||
183 | |||
184 | // open the directory |
||
185 | $dir = opendir($src); |
||
186 | |||
187 | // remove files/folders recursively |
||
188 | while (false !== ($file = readdir($dir))) { |
||
189 | if (($file != '.') && ($file != '..')) { |
||
190 | $full = $src . '/' . $file; |
||
191 | if ($this->isDir($full)) { |
||
192 | $recursive ?? $this->removeDir($full, $recursive); |
||
193 | } else { |
||
194 | if (!$this->delete($full)) { |
||
195 | throw new \Exception(sprintf('Can\'t remove file %s', $full)); |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | // close handle and remove directory itself |
||
202 | closedir($dir); |
||
203 | if (!rmdir($src)) { |
||
204 | throw new \Exception(sprintf('Can\'t remove directory %s', $src)); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Find and return pathnames matching a pattern |
||
210 | * |
||
211 | * @param string $pattern No tilde expansion or parameter substitution is done. |
||
212 | * @param int $flags Flags that changes the behaviour |
||
213 | * |
||
214 | * @return array Containing the matched files/directories, an empty array if no file matched or FALSE on error |
||
215 | * @link https://www.php.net/glob |
||
216 | */ |
||
217 | public function glob(string $pattern, int $flags = 0) |
||
218 | { |
||
219 | |||
220 | // clear the filecache |
||
221 | clearstatcache(); |
||
222 | |||
223 | // invoke the glob and return the array with the found files |
||
224 | return glob($pattern, $flags); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Return's the size of the file with the passed name. |
||
229 | * |
||
230 | * @param string $filename The name of the file to return the size for |
||
231 | * |
||
232 | * @return int The size of the file in bytes |
||
233 | * @throws \Exception Is thrown, if the size can not be calculated |
||
234 | * @link https://php.net/filesize |
||
235 | */ |
||
236 | public function size($filename) |
||
237 | { |
||
238 | |||
239 | // calculate the size of the file and return it |
||
240 | if (is_int($size = filesize($filename))) { |
||
241 | return $size; |
||
242 | } |
||
243 | |||
244 | // throw an exception if the size can not be calculated |
||
245 | throw new \Exception(sprintf('Can\'t calculate size of file "%s"', $filename)); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Read's and return's the content of the file with the passed name as array. |
||
250 | * |
||
251 | * @param string $filename The name of the file to return its content for |
||
252 | * |
||
253 | * @return array The content of the file as array |
||
254 | * @throws \Exception Is thrown, if the file is not accessible |
||
255 | * @link https://php.net/file |
||
256 | */ |
||
257 | public function read($filename) |
||
258 | { |
||
259 | |||
260 | // read the content of the file and return it |
||
261 | if ($content = file($filename)) { |
||
262 | return $content; |
||
263 | } |
||
264 | |||
265 | // throw an exception if the content of the file is not accessible |
||
266 | throw new \Exception(sprintf('Can\'t read the content of file "%s"', $filename)); |
||
267 | } |
||
268 | } |
||
269 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.