Completed
Pull Request — master (#50)
by Jonathan
04:13 queued 01:53
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 assert;
12
use function is_dir;
13
use function is_file;
14
use function is_string;
15
use function str_replace;
16
17
/**
18
 * Locates the file that contains the metadata information for a given class name.
19
 *
20
 * This behavior is independent of the actual content of the file. It just detects
21
 * the file which is responsible for the given class name.
22
 */
23
class DefaultFileLocator implements FileLocator
24
{
25
    /**
26
     * The paths where to look for mapping files.
27
     *
28
     * @var string[]
29
     */
30
    protected $paths = [];
31
32
    /**
33
     * The file extension of mapping documents.
34
     *
35
     * @var string|null
36
     */
37
    protected $fileExtension;
38
39
    /**
40
     * Initializes a new FileDriver that looks in the given path(s) for mapping
41
     * documents and operates in the specified operating mode.
42
     *
43
     * @param string|string[] $paths         One or multiple paths where mapping documents can be found.
44
     * @param string|null     $fileExtension The file extension of mapping documents, usually prefixed with a dot.
45
     */
46 11
    public function __construct($paths, ?string $fileExtension = null)
47
    {
48 11
        $this->addPaths((array) $paths);
49 11
        $this->fileExtension = $fileExtension;
50 11
    }
51
52
    /**
53
     * Appends lookup paths to metadata driver.
54
     *
55
     * @param string[] $paths
56
     */
57 11
    public function addPaths(array $paths) : void
58
    {
59 11
        $this->paths = array_unique(array_merge($this->paths, $paths));
60 11
    }
61
62
    /**
63
     * Retrieves the defined metadata lookup paths.
64
     *
65
     * @return string[]
66
     */
67 2
    public function getPaths() : array
68
    {
69 2
        return $this->paths;
70
    }
71
72
    /**
73
     * Gets the file extension used to look for mapping files under.
74
     */
75 1
    public function getFileExtension() : ?string
76
    {
77 1
        return $this->fileExtension;
78
    }
79
80
    /**
81
     * Sets the file extension used to look for mapping files under.
82
     *
83
     * @param string|null $fileExtension The file extension to set.
84
     *
85
     * @return void
86
     */
87 1
    public function setFileExtension(?string $fileExtension)
88
    {
89 1
        $this->fileExtension = $fileExtension;
90 1
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 3
    public function findMappingFile(string $className) : string
96
    {
97 3
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
98
99
        // Check whether file exists
100 3
        foreach ($this->paths as $path) {
101 3
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
102 2
                return $path . DIRECTORY_SEPARATOR . $fileName;
103
            }
104
        }
105
106 1
        throw MappingException::mappingFileNotFound($className, $fileName);
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 2
    public function getAllClassNames(?string $globalBasename = null) : array
113
    {
114 2
        $classes = [];
115
116 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...
117 2
            foreach ($this->paths as $path) {
118 2
                if (! is_dir($path)) {
119
                    throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
120
                }
121
122 2
                $iterator = new RecursiveIteratorIterator(
123 2
                    new RecursiveDirectoryIterator($path),
124 2
                    RecursiveIteratorIterator::LEAVES_ONLY
125
                );
126
127 2
                foreach ($iterator as $file) {
128 2
                    $fileName = $file->getBasename($this->fileExtension);
129
130 2
                    if ($fileName === $file->getBasename() || $fileName === $globalBasename) {
131 2
                        continue;
132
                    }
133
134
                    // NOTE: All files found here means classes are not transient!
135
136 1
                    $class = str_replace('.', '\\', $fileName);
137 1
                    assert(is_string($class));
138
139 1
                    $classes[] = $class;
140
                }
141
            }
142
        }
143
144 2
        return $classes;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 2
    public function fileExists(string $className) : bool
151
    {
152 2
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
153
154
        // Check whether file exists
155 2
        foreach ((array) $this->paths as $path) {
156 2
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
157 2
                return true;
158
            }
159
        }
160
161 2
        return false;
162
    }
163
}
164