Skip to content

Commit af503e1

Browse files
authored
Create spl file info (#11)
* switch dockerfile to php8.1 * Add makeFileInfo method
1 parent 6775323 commit af503e1

File tree

4 files changed

+101
-32
lines changed

4 files changed

+101
-32
lines changed

docker/php/Dockerfile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:8-cli-alpine
1+
FROM php:8.1-cli-alpine
22

33

44
RUN set -xe && apk update && apk add --no-cache \
@@ -24,11 +24,7 @@ RUN docker-php-ext-install zip soap \
2424
&& echo 'xdebug.mode=coverage' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
2525

2626

27-
<<<<<<< HEAD
28-
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.2.7 \
29-
=======
30-
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.1.8 \
31-
>>>>>>> origin/master
27+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.3.7 \
3228
&& mkdir -p /.composer && chmod -Rf 777 /.composer
3329

3430

src/FileSystemHelper.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public function getTmpDir(): SplFileInfo;
9797
*
9898
* @param SplFileInfo|string $dir
9999
* @param Closure $callback
100+
*
101+
* @throws FileSystemException
100102
*/
101103
public function iterateDirectory(SplFileInfo|string $dir, Closure $callback): void;
104+
105+
/**
106+
* Tries to create SplFileInfo object from the given path.
107+
*
108+
* @param mixed $path
109+
*
110+
* @return SplFileInfo
111+
*
112+
* @throws FileSystemException
113+
*/
114+
public function makeFileInfo(mixed $path): SplFileInfo;
102115
}

src/FileSystemHelperBase.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/FileSystemHelperBaseTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,54 @@ public function testIterateNonDirectoryException(): void
444444
function (): void {}
445445
);
446446
}
447+
448+
/**
449+
* @throws Throwable
450+
*/
451+
public function testMakeFileInfoString(): void
452+
{
453+
$file = $this->getPathToTestFile();
454+
455+
$helper = new FileSystemHelperBase();
456+
$fileInfo = $helper->makeFileInfo($file);
457+
458+
$this->assertInstanceOf(SplFileInfo::class, $fileInfo);
459+
$this->assertSame($file, $fileInfo->getPathname());
460+
}
461+
462+
/**
463+
* @throws Throwable
464+
*/
465+
public function testMakeFileInfoObject(): void
466+
{
467+
$file = new SplFileInfo($this->getPathToTestFile());
468+
469+
$helper = new FileSystemHelperBase();
470+
$fileInfo = $helper->makeFileInfo($file);
471+
472+
$this->assertInstanceOf(SplFileInfo::class, $fileInfo);
473+
$this->assertSame($file->getPathname(), $fileInfo->getPathname());
474+
}
475+
476+
/**
477+
* @throws Throwable
478+
*/
479+
public function testMakeFileInfoEmptyString(): void
480+
{
481+
$helper = new FileSystemHelperBase();
482+
483+
$this->expectException(FileSystemException::class);
484+
$helper->makeFileInfo(' ');
485+
}
486+
487+
/**
488+
* @throws Throwable
489+
*/
490+
public function testMakeFileInfoWrongInput(): void
491+
{
492+
$helper = new FileSystemHelperBase();
493+
494+
$this->expectException(FileSystemException::class);
495+
$helper->makeFileInfo(true);
496+
}
447497
}

0 commit comments

Comments
 (0)