Failed Conditions
Pull Request — master (#1)
by Jonathan
13:22 queued 10:46
created

DefaultFileLocator::getAllClassNames()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

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