Completed
Push — master ( 89c0fb...943241 )
by Paul
01:57
created

LocalFileSystem::fileExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of PHPUnit Generator.
5
 *
6
 * (c) Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PHPUnitGenerator\FileSystem;
13
14
use PHPUnitGenerator\Exception\DirNotFoundException;
15
use PHPUnitGenerator\Exception\FileNotFoundException;
16
use PHPUnitGenerator\FileSystem\FileSystemInterface\FileSystemInterface;
17
18
/**
19
 * Class LocalFileSystem
20
 *
21
 *      Allow to use the local storage to manage files and directories
22
 *
23
 * @package PHPUnitGenerator\FileSystem
24
 */
25
class LocalFileSystem implements FileSystemInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getFiles(string $dir): array
31
    {
32
        $fileList = [];
33
34
        foreach ($this->getFilesIterator($dir) as $file) {
35
            /**
36
             * @var \SplFileInfo $file
37
             */
38
            if ($this->fileExists($file->__toString())) {
0 ignored issues
show
Bug introduced by
$file->__toString() of type void is incompatible with the type string expected by parameter $file of PHPUnitGenerator\FileSys...ileSystem::fileExists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            if ($this->fileExists(/** @scrutinizer ignore-type */ $file->__toString())) {
Loading history...
Bug introduced by
Are you sure the usage of $file->__toString() targeting SplFileInfo::__toString() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
                $fileList[] = $file->__toString();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $fileList[] is correct as $file->__toString() targeting SplFileInfo::__toString() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
            }
41
        }
42
43
        return $fileList;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function filterFiles(array $files, string $includeRegex = null, string $excludeRegex = null): array
50
    {
51
        foreach ($files as $key => $file) {
52
            if (($includeRegex !== null && preg_match($includeRegex, $file) <= 0)
53
                || ($excludeRegex !== null && preg_match($excludeRegex, $file) > 0)
54
            ) {
55
                unset($files[$key]);
56
            }
57
        }
58
59
        return $files;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function mkDir(string $dir)
66
    {
67
        if (! $this->dirExists($dir)) {
68
            mkdir($dir, 0755, true);
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function pathExists(string $path): bool
76
    {
77
        return file_exists($path);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function fileExists(string $file): bool
84
    {
85
        return $this->pathExists($file) && is_file($file);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function dirExists(string $file): bool
92
    {
93
        return $this->pathExists($file) && is_dir($file);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function write(string $file, string $content)
100
    {
101
        file_put_contents($file, $content);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function read(string $file): string
108
    {
109
        if (! $this->fileExists($file)) {
110
            throw new FileNotFoundException(sprintf(FileNotFoundException::TEXT, $file));
111
        }
112
        return file_get_contents($file);
113
    }
114
115
    /**
116
     * Get the file iterator for a directory
117
     *
118
     * @param string $dir
119
     *
120
     * @return \IteratorIterator
121
     *
122
     * @throws DirNotFoundException If the directory does not exists
123
     */
124
    protected function getFilesIterator(string $dir)
125
    {
126
        if (! $this->dirExists($dir)) {
127
            throw new DirNotFoundException(sprintf(DirNotFoundException::TEXT, $dir));
128
        }
129
130
        $directory = new \RecursiveDirectoryIterator($dir);
131
        $iterator = new \RecursiveIteratorIterator($directory);
132
        return new \IteratorIterator($iterator);
133
    }
134
}
135