Passed
Push — types ( 30d5cd )
by Jonathan
04:47
created

DefaultFileLocator::getAllClassNames()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0099

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
ccs 16
cts 17
cp 0.9412
rs 8.8333
c 0
b 0
f 0
cc 7
nc 3
nop 1
crap 7.0099
1
<?php
2
3
namespace Doctrine\Persistence\Mapping\Driver;
4
5
use Doctrine\Persistence\Mapping\MappingException;
6
use RecursiveDirectoryIterator;
7
use RecursiveIteratorIterator;
8
use const DIRECTORY_SEPARATOR;
9
use function array_merge;
10
use function array_unique;
11
use function is_dir;
12
use function is_file;
13
use function str_replace;
14
15
/**
16
 * Locates the file that contains the metadata information for a given class name.
17
 *
18
 * This behavior is independent of the actual content of the file. It just detects
19
 * the file which is responsible for the given class name.
20
 */
21
class DefaultFileLocator implements FileLocator
22
{
23
    /**
24
     * The paths where to look for mapping files.
25
     *
26
     * @var string[]
27
     */
28
    protected $paths = [];
29
30
    /**
31
     * The file extension of mapping documents.
32
     *
33
     * @var string|null
34
     */
35
    protected $fileExtension;
36
37
    /**
38
     * Initializes a new FileDriver that looks in the given path(s) for mapping
39
     * documents and operates in the specified operating mode.
40
     *
41
     * @param string|string[] $paths         One or multiple paths where mapping documents can be found.
42
     * @param string|null     $fileExtension The file extension of mapping documents, usually prefixed with a dot.
43
     */
44 11
    public function __construct($paths, ?string $fileExtension = null)
45
    {
46 11
        $this->addPaths((array) $paths);
47 11
        $this->fileExtension = $fileExtension;
48 11
    }
49
50
    /**
51
     * Appends lookup paths to metadata driver.
52
     *
53
     * @param string[] $paths
54
     */
55 11
    public function addPaths(array $paths) : void
56
    {
57 11
        $this->paths = array_unique(array_merge($this->paths, $paths));
58 11
    }
59
60
    /**
61
     * Retrieves the defined metadata lookup paths.
62
     *
63
     * @return string[]
64
     */
65 2
    public function getPaths() : array
66
    {
67 2
        return $this->paths;
68
    }
69
70
    /**
71
     * Gets the file extension used to look for mapping files under.
72
     */
73 1
    public function getFileExtension() : ?string
74
    {
75 1
        return $this->fileExtension;
76
    }
77
78
    /**
79
     * Sets the file extension used to look for mapping files under.
80
     *
81
     * @param string|null $fileExtension The file extension to set.
82
     *
83
     * @return void
84
     */
85 1
    public function setFileExtension(?string $fileExtension)
86
    {
87 1
        $this->fileExtension = $fileExtension;
88 1
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 3
    public function findMappingFile(string $className) : string
94
    {
95 3
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
96
97
        // Check whether file exists
98 3
        foreach ($this->paths as $path) {
99 3
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
100 2
                return $path . DIRECTORY_SEPARATOR . $fileName;
101
            }
102
        }
103
104 1
        throw MappingException::mappingFileNotFound($className, $fileName);
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110 2
    public function getAllClassNames(?string $globalBasename = null) : array
111
    {
112 2
        $classes = [];
113
114 2
        if ($this->paths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paths of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115 2
            foreach ($this->paths as $path) {
116 2
                if (! is_dir($path)) {
117
                    throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
118
                }
119
120 2
                $iterator = new RecursiveIteratorIterator(
121 2
                    new RecursiveDirectoryIterator($path),
122 2
                    RecursiveIteratorIterator::LEAVES_ONLY
123
                );
124
125 2
                foreach ($iterator as $file) {
126 2
                    $fileName = $file->getBasename($this->fileExtension);
127
128 2
                    if ($fileName === $file->getBasename() || $fileName === $globalBasename) {
129 2
                        continue;
130
                    }
131
132
                    // NOTE: All files found here means classes are not transient!
133
134 1
                    $class = str_replace('.', '\\', $fileName);
135 1
                    assert(is_string($class));
0 ignored issues
show
introduced by
Function assert() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function is_string() should not be referenced via a fallback global name, but via a use statement.
Loading history...
136
137 1
                    $classes[] = $class;
138
                }
139
            }
140
        }
141
142 2
        return $classes;
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148 2
    public function fileExists(string $className) : bool
149
    {
150 2
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
151
152
        // Check whether file exists
153 2
        foreach ((array) $this->paths as $path) {
154 2
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
155 2
                return true;
156
            }
157
        }
158
159 2
        return false;
160
    }
161
}
162