@@ -40,7 +40,7 @@ public function __construct(?string $baseFolder = null)
4040 */
4141 public function remove (SplFileInfo |string $ entity ): void
4242 {
43- $ splEntity = $ this ->convertToSplFileInfo ($ entity );
43+ $ splEntity = $ this ->makeFileInfoAndCheckBasePath ($ entity );
4444
4545 if ($ splEntity ->isFile ()) {
4646 $ this ->runPhpFunction (
@@ -69,7 +69,7 @@ public function remove(SplFileInfo|string $entity): void
6969 */
7070 public function removeIfExists (SplFileInfo |string $ entity ): void
7171 {
72- $ splEntity = $ this ->convertToSplFileInfo ($ entity );
72+ $ splEntity = $ this ->makeFileInfoAndCheckBasePath ($ entity );
7373
7474 if ($ splEntity ->isFile () || $ splEntity ->isDir ()) {
7575 $ this ->remove ($ splEntity );
@@ -81,8 +81,8 @@ public function removeIfExists(SplFileInfo|string $entity): void
8181 */
8282 public function copy (SplFileInfo |string $ from , SplFileInfo |string $ to ): SplFileInfo
8383 {
84- $ source = $ this ->convertToSplFileInfo ($ from );
85- $ target = $ this ->convertToSplFileInfo ($ to );
84+ $ source = $ this ->makeFileInfoAndCheckBasePath ($ from );
85+ $ target = $ this ->makeFileInfoAndCheckBasePath ($ to );
8686
8787 if (!$ source ->isDir () && !$ source ->isFile ()) {
8888 throw $ this ->createException (
@@ -99,7 +99,7 @@ public function copy(SplFileInfo|string $from, SplFileInfo|string $to): SplFileI
9999 );
100100 }
101101
102- $ parent = $ this ->convertToSplFileInfo ($ target ->getPath ());
102+ $ parent = $ this ->makeFileInfoAndCheckBasePath ($ target ->getPath ());
103103
104104 if (!$ parent ->isDir ()) {
105105 throw $ this ->createException (
@@ -141,8 +141,8 @@ function (SplFileInfo $file) use ($target): void {
141141 */
142142 public function rename (SplFileInfo |string $ from , SplFileInfo |string $ to ): SplFileInfo
143143 {
144- $ source = $ this ->convertToSplFileInfo ($ from );
145- $ destination = $ this ->convertToSplFileInfo ($ to );
144+ $ source = $ this ->makeFileInfoAndCheckBasePath ($ from );
145+ $ destination = $ this ->makeFileInfoAndCheckBasePath ($ to );
146146
147147 if (!$ source ->isFile () && !$ source ->isDir ()) {
148148 throw $ this ->createException (
@@ -158,7 +158,7 @@ public function rename(SplFileInfo|string $from, SplFileInfo|string $to): SplFil
158158 );
159159 }
160160
161- $ parent = $ this ->convertToSplFileInfo ($ destination ->getPath ());
161+ $ parent = $ this ->makeFileInfoAndCheckBasePath ($ destination ->getPath ());
162162
163163 if (!$ parent ->isDir ()) {
164164 throw $ this ->createException (
@@ -188,7 +188,7 @@ public function rename(SplFileInfo|string $from, SplFileInfo|string $to): SplFil
188188 */
189189 public function mkdir (SplFileInfo |string $ path , int $ mode = 0777 ): SplFileInfo
190190 {
191- $ dir = $ this ->convertToSplFileInfo ($ path );
191+ $ dir = $ this ->makeFileInfoAndCheckBasePath ($ path );
192192
193193 if ($ dir ->isFile () || $ dir ->isDir ()) {
194194 throw $ this ->createException (
@@ -212,7 +212,7 @@ public function mkdir(SplFileInfo|string $path, int $mode = 0777): SplFileInfo
212212 */
213213 public function mkdirIfNotExist (SplFileInfo |string $ path , int $ mode = 0777 ): SplFileInfo
214214 {
215- $ dir = $ this ->convertToSplFileInfo ($ path );
215+ $ dir = $ this ->makeFileInfoAndCheckBasePath ($ path );
216216
217217 if ($ dir ->isDir ()) {
218218 return $ dir ;
@@ -226,7 +226,7 @@ public function mkdirIfNotExist(SplFileInfo|string $path, int $mode = 0777): Spl
226226 */
227227 public function emptyDir (SplFileInfo |string $ path ): void
228228 {
229- $ dir = $ this ->convertToSplFileInfo ($ path );
229+ $ dir = $ this ->makeFileInfoAndCheckBasePath ($ path );
230230
231231 if (!$ dir ->isDir ()) {
232232 throw $ this ->createException (
@@ -254,15 +254,15 @@ public function getTmpDir(): SplFileInfo
254254 );
255255 }
256256
257- return $ this ->convertToSplFileInfo ($ dir );
257+ return $ this ->makeFileInfoAndCheckBasePath ($ dir );
258258 }
259259
260260 /**
261261 * {@inheritDoc}
262262 */
263263 public function iterateDirectory (SplFileInfo |string $ dir , Closure $ callback ): void
264264 {
265- $ splEntity = $ this ->convertToSplFileInfo ($ dir );
265+ $ splEntity = $ this ->makeFileInfoAndCheckBasePath ($ dir );
266266
267267 if (!$ splEntity ->isDir ()) {
268268 throw $ this ->createException (
@@ -287,6 +287,26 @@ public function iterateDirectory(SplFileInfo|string $dir, Closure $callback): vo
287287 }
288288 }
289289
290+ /**
291+ * {@inheritDoc}
292+ */
293+ public function makeFileInfo (mixed $ path ): SplFileInfo
294+ {
295+ if (\is_string ($ path )) {
296+ $ trimmedPath = trim ($ path );
297+ if ($ trimmedPath === '' ) {
298+ throw $ this ->createException ("Can't create SplFileInfo using empty string " );
299+ }
300+ $ fileInfo = new SplFileInfo ($ trimmedPath );
301+ } elseif ($ path instanceof SplFileInfo) {
302+ $ fileInfo = $ path ;
303+ } else {
304+ throw $ this ->createException ("Can't create SplFileInfo from given object type " );
305+ }
306+
307+ return $ fileInfo ;
308+ }
309+
290310 /**
291311 * Creates SplFileInfo object from set data.
292312 *
@@ -296,17 +316,9 @@ public function iterateDirectory(SplFileInfo|string $dir, Closure $callback): vo
296316 *
297317 * @throws FileSystemException
298318 */
299- private function convertToSplFileInfo (SplFileInfo |string $ data ): SplFileInfo
319+ private function makeFileInfoAndCheckBasePath (SplFileInfo |string $ data ): SplFileInfo
300320 {
301- if (\is_string ($ data )) {
302- $ trimmedData = trim ($ data );
303- if ($ trimmedData === '' ) {
304- throw $ this ->createException (
305- "Can't create SplFileInfo using empty string "
306- );
307- }
308- $ data = new SplFileInfo ($ trimmedData );
309- }
321+ $ data = $ this ->makeFileInfo ($ data );
310322
311323 if ($ this ->baseFolder !== null && mb_strpos ($ data ->getPathName (), $ this ->baseFolder ) !== 0 ) {
312324 throw $ this ->createException (
@@ -350,9 +362,7 @@ private function runPhpFunction(string $functionName, ...$params): void
350362 private function createException (string $ message , ...$ params ): FileSystemException
351363 {
352364 $ stringifyParams = array_map (
353- function (SplFileInfo |string $ item ): string {
354- return $ item instanceof SplFileInfo ? $ item ->getPathName () : $ item ;
355- },
365+ fn (SplFileInfo |string $ item ): string => $ item instanceof SplFileInfo ? $ item ->getPathName () : $ item ,
356366 $ params
357367 );
358368
0 commit comments