Failed Conditions
Pull Request — master (#2)
by Jonathan
02:45
created

StaticPHPDriver   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 107
rs 10
c 0
b 0
f 0
ccs 36
cts 39
cp 0.9231

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransient() 0 3 1
A __construct() 0 3 1
A addPaths() 0 3 1
A loadMetadataForClass() 0 3 1
C getAllClassNames() 0 49 10
1
<?php
2
namespace Doctrine\Common\Persistence\Mapping\Driver;
3
4
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
5
use Doctrine\Common\Persistence\Mapping\MappingException;
6
use function array_merge;
7
use function array_unique;
8
use function get_declared_classes;
9
use function in_array;
10
use function is_dir;
11
use function method_exists;
12
use function realpath;
13
14
/**
15
 * The StaticPHPDriver calls a static loadMetadata() method on your entity
16
 * classes where you can manually populate the ClassMetadata instance.
17
 *
18
 * @link   www.doctrine-project.org
19
 */
20
class StaticPHPDriver implements MappingDriver
21
{
22
    /**
23
     * Paths of entity directories.
24
     *
25
     * @var string[]
26
     */
27
    private $paths = [];
28
29
    /**
30
     * Map of all class names.
31
     *
32
     * @var string[]
33
     */
34
    private $classNames;
35
36
    /**
37
     *
38
     *
39
     * @param string[]|string $paths
40
     */
41 2
    public function __construct($paths)
42
    {
43 2
        $this->addPaths((array) $paths);
44 2
    }
45
46
    /**
47
     * Adds paths.
48
     *
49
     * @param string[] $paths
50
     *
51
     * @return void
52
     */
53 2
    public function addPaths(array $paths)
54
    {
55 2
        $this->paths = array_unique(array_merge($this->paths, $paths));
56 2
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function loadMetadataForClass($className, ClassMetadata $metadata)
62
    {
63 1
        $className::loadMetadata($metadata);
64 1
    }
65
66
    /**
67
     * {@inheritDoc}
68
     * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
69
     */
70 1
    public function getAllClassNames()
71
    {
72 1
        if ($this->classNames !== null) {
73
            return $this->classNames;
74
        }
75
76 1
        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...
77
            throw MappingException::pathRequired();
78
        }
79
80 1
        $classes       = [];
81 1
        $includedFiles = [];
82
83 1
        foreach ($this->paths as $path) {
84 1
            if (! is_dir($path)) {
85
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
86
            }
87
88 1
            $iterator = new \RecursiveIteratorIterator(
89 1
                new \RecursiveDirectoryIterator($path),
90 1
                \RecursiveIteratorIterator::LEAVES_ONLY
91
            );
92
93 1
            foreach ($iterator as $file) {
94 1
                if ($file->getBasename('.php') === $file->getBasename()) {
95 1
                    continue;
96
                }
97
98 1
                $sourceFile = realpath($file->getPathName());
99 1
                require_once $sourceFile;
100 1
                $includedFiles[] = $sourceFile;
101
            }
102
        }
103
104 1
        $declared = get_declared_classes();
105
106 1
        foreach ($declared as $className) {
107 1
            $rc         = new \ReflectionClass($className);
108 1
            $sourceFile = $rc->getFileName();
109 1
            if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
110 1
                continue;
111
            }
112
113 1
            $classes[] = $className;
114
        }
115
116 1
        $this->classNames = $classes;
117
118 1
        return $classes;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1
    public function isTransient($className)
125
    {
126 1
        return ! method_exists($className, 'loadMetadata');
127
    }
128
}
129