1 | <?php |
||
19 | class FileExplorer extends \RecursiveFilterIterator implements \Iterator |
||
20 | { |
||
21 | /** |
||
22 | * A bitwise flag to have FileExplorer ignore all files unless its been explicitly included; all other files will be |
||
23 | * ignored. |
||
24 | */ |
||
25 | const INCLUDE_ONLY_FILES = 0x1; |
||
26 | |||
27 | /** |
||
28 | * A bitwise flag to have FileExplorer search files starting with a period as well. |
||
29 | */ |
||
30 | const ALLOW_DOT_FILES = 0x2; |
||
31 | |||
32 | /** |
||
33 | * A list of common version control folders to ignore. |
||
34 | * |
||
35 | * The following folders should be ignored explicitly by the end user. Their usage isn't as popular so adding more |
||
36 | * conditions to loop through will only slow down FileExplorer. |
||
37 | * |
||
38 | * - 'CVS' |
||
39 | * - '_darcs' |
||
40 | * - '.arch-params' |
||
41 | * - '.monotone' |
||
42 | * - '.bzr' |
||
43 | * |
||
44 | * @var string[] |
||
45 | */ |
||
46 | public static $vcsPatterns = ['.git', '.hg', '.svn', '_svn']; |
||
47 | |||
48 | /** |
||
49 | * A list of phrases to exclude from the search. |
||
50 | * |
||
51 | * @var string[] |
||
52 | */ |
||
53 | private $excludes; |
||
54 | |||
55 | /** |
||
56 | * A list of phrases to explicitly include in the search. |
||
57 | * |
||
58 | * @var string[] |
||
59 | */ |
||
60 | private $includes; |
||
61 | |||
62 | /** |
||
63 | * The bitwise sum of the flags applied to this FileExplorer instance. |
||
64 | * |
||
65 | * @var int|null |
||
66 | */ |
||
67 | private $flags; |
||
68 | |||
69 | /** |
||
70 | * FileExplorer constructor. |
||
71 | * |
||
72 | * @param \RecursiveIterator $iterator |
||
73 | * @param string[] $excludes |
||
74 | * @param string[] $includes |
||
75 | * @param int|null $flags |
||
76 | */ |
||
77 | 41 | public function __construct(\RecursiveIterator $iterator, array $excludes = array(), array $includes = array(), $flags = null) |
|
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function __toString() |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 41 | public function accept() |
|
103 | |||
104 | /** |
||
105 | * Get the current File object. |
||
106 | * |
||
107 | * @return File |
||
108 | */ |
||
109 | 41 | public function current() |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function getChildren() |
||
133 | |||
134 | /** |
||
135 | * Get an Iterator with all of the files that have met the search requirements. |
||
136 | * |
||
137 | * @return \RecursiveIteratorIterator |
||
138 | */ |
||
139 | 41 | public function getExplorer() |
|
143 | |||
144 | /** |
||
145 | * Check whether or not a relative file path matches the definition given to this FileExplorer instance. |
||
146 | * |
||
147 | * @param string $filePath |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | 41 | public function matchesPattern($filePath) |
|
170 | |||
171 | /** |
||
172 | * Create an instance of FileExplorer from a directory path as a string. |
||
173 | * |
||
174 | * @param string $folder The path to the folder we're scanning |
||
175 | * @param string[] $excludes |
||
176 | * @param string[] $includes |
||
177 | * @param int|null $flags |
||
178 | * |
||
179 | * @return FileExplorer |
||
180 | */ |
||
181 | 41 | public static function create($folder, $excludes = array(), $includes = array(), $flags = null) |
|
188 | |||
189 | /** |
||
190 | * Search a given string for an array of possible elements. |
||
191 | * |
||
192 | * @param string $haystack |
||
193 | * @param string[] $needle |
||
194 | * @param int $offset |
||
195 | * |
||
196 | * @return bool True if an element from the given array was found in the string |
||
197 | */ |
||
198 | 41 | private static function strpos_array($haystack, $needle, $offset = 0) |
|
220 | |||
221 | /** |
||
222 | * Strip the current working directory from an absolute path. |
||
223 | * |
||
224 | * @param string $path An absolute path |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 41 | private static function getRelativePath($path) |
|
232 | |||
233 | /** |
||
234 | * A vfsStream friendly way of getting the realpath() of something. |
||
235 | * |
||
236 | * @param string $path |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 41 | private static function realpath($path) |
|
249 | } |
||
250 |
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: