1 | <?php |
||
21 | class FileExplorer extends \RecursiveFilterIterator implements \Iterator |
||
22 | { |
||
23 | /** |
||
24 | * A bitwise flag to have FileExplorer ignore all files unless its been explicitly included; all other files will be |
||
25 | * ignored. |
||
26 | */ |
||
27 | const INCLUDE_ONLY_FILES = 0x1; |
||
28 | |||
29 | /** |
||
30 | * A bitwise flag to have FileExplorer search files starting with a period as well. |
||
31 | */ |
||
32 | const ALLOW_DOT_FILES = 0x2; |
||
33 | |||
34 | /** |
||
35 | * A bitwise flag to have FileExplorer ignore any directories. |
||
36 | */ |
||
37 | const IGNORE_DIRECTORIES = 0x4; |
||
38 | |||
39 | /** |
||
40 | * A list of common version control folders to ignore. |
||
41 | * |
||
42 | * The following folders should be ignored explicitly by the end user. Their usage isn't as popular so adding more |
||
43 | * conditions to loop through will only slow down FileExplorer. |
||
44 | * |
||
45 | * - 'CVS' |
||
46 | * - '_darcs' |
||
47 | * - '.arch-params' |
||
48 | * - '.monotone' |
||
49 | * - '.bzr' |
||
50 | * |
||
51 | * @var string[] |
||
52 | */ |
||
53 | public static $vcsPatterns = ['.git', '.hg', '.svn', '_svn']; |
||
54 | |||
55 | /** |
||
56 | * A custom callable that will be used in the `accept()` method. If null, the default matcher will be used. |
||
57 | * |
||
58 | * @var callable[] |
||
59 | */ |
||
60 | private $matchers; |
||
61 | |||
62 | /** |
||
63 | * A list of phrases to exclude from the search. |
||
64 | * |
||
65 | * @var string[] |
||
66 | */ |
||
67 | private $excludes; |
||
68 | |||
69 | /** |
||
70 | * A list of phrases to explicitly include in the search. |
||
71 | * |
||
72 | * @var string[] |
||
73 | */ |
||
74 | private $includes; |
||
75 | |||
76 | /** |
||
77 | * The bitwise sum of the flags applied to this FileExplorer instance. |
||
78 | * |
||
79 | * @var int|null |
||
80 | */ |
||
81 | private $flags; |
||
82 | |||
83 | /** |
||
84 | * FileExplorer constructor. |
||
85 | * |
||
86 | * @param \RecursiveIterator $iterator |
||
87 | * @param string[] $includes |
||
88 | * @param string[] $excludes |
||
89 | * @param int|null $flags |
||
90 | */ |
||
91 | 65 | public function __construct(\RecursiveIterator $iterator, array $includes = [], array $excludes = [], $flags = null) |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function __toString() |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 65 | public function accept() |
|
132 | |||
133 | /** |
||
134 | * Get the current File object. |
||
135 | * |
||
136 | * @return File|Folder |
||
137 | */ |
||
138 | 65 | public function current() |
|
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | 1 | public function getChildren() |
|
168 | |||
169 | /** |
||
170 | * Get an Iterator with all of the files (and *only* files) that have met the search requirements. |
||
171 | * |
||
172 | * @return \RecursiveIteratorIterator |
||
173 | */ |
||
174 | 63 | public function getFileIterator() |
|
178 | |||
179 | /** |
||
180 | * Check whether or not a relative file path matches the definition given to this FileExplorer instance. |
||
181 | * |
||
182 | * @param string $filePath |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 65 | public function matchesPattern($filePath) |
|
209 | |||
210 | /** |
||
211 | * Add a custom matcher that will be executed before the default matcher that uses file names and paths. |
||
212 | * |
||
213 | * @param callable $callable |
||
214 | */ |
||
215 | 1 | public function addMatcher(callable $callable) |
|
219 | |||
220 | /** |
||
221 | * Create an instance of FileExplorer from a directory path as a string. |
||
222 | * |
||
223 | * @deprecated Use `FileExplorer::createFromDefinition()` instead. |
||
224 | * |
||
225 | * @param string $folder The path to the folder we're scanning |
||
226 | * @param string[] $includes |
||
227 | * @param string[] $excludes |
||
228 | * @param int|null $flags |
||
229 | * |
||
230 | * @return FileExplorer |
||
231 | */ |
||
232 | public static function create($folder, $includes = [], $excludes = [], $flags = null) |
||
239 | |||
240 | /** |
||
241 | * @param FileExplorerDefinition $definition |
||
242 | * |
||
243 | * @return FileExplorer |
||
244 | */ |
||
245 | 52 | public static function createFromDefinition(FileExplorerDefinition $definition) |
|
249 | |||
250 | /** |
||
251 | * Search a given string for an array of possible elements. |
||
252 | * |
||
253 | * @param string $haystack |
||
254 | * @param string[] $needle |
||
255 | * @param int $offset |
||
256 | * |
||
257 | * @return bool True if an element from the given array was found in the string |
||
258 | */ |
||
259 | 65 | private static function strpos_array($haystack, $needle, $offset = 0) |
|
281 | } |
||
282 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: