Failed Conditions
Push — master ( 3a6c46...2fbe69 )
by Luís
36s queued 11s
created

StaticPHPDriver::isTransient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\Persistence\Mapping\Driver;
4
5
use Doctrine\Persistence\Mapping\ClassMetadata;
6
use Doctrine\Persistence\Mapping\MappingException;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use ReflectionClass;
10
use function array_merge;
11
use function array_unique;
12
use function get_declared_classes;
13
use function in_array;
14
use function is_dir;
15
use function method_exists;
16
use function realpath;
17
18
/**
19
 * The StaticPHPDriver calls a static loadMetadata() method on your entity
20
 * classes where you can manually populate the ClassMetadata instance.
21
 */
22
class StaticPHPDriver implements MappingDriver
23
{
24
    /**
25
     * Paths of entity directories.
26
     *
27
     * @var string[]
28
     */
29
    private $paths = [];
30
31
    /**
32
     * Map of all class names.
33
     *
34
     * @var string[]
35
     */
36
    private $classNames;
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
     *
69
     * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
70
     */
71 1
    public function getAllClassNames()
72
    {
73 1
        if ($this->classNames !== null) {
74
            return $this->classNames;
75
        }
76
77 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...
78
            throw MappingException::pathRequired();
79
        }
80
81 1
        $classes       = [];
82 1
        $includedFiles = [];
83
84 1
        foreach ($this->paths as $path) {
85 1
            if (! is_dir($path)) {
86
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
87
            }
88
89 1
            $iterator = new RecursiveIteratorIterator(
90 1
                new RecursiveDirectoryIterator($path),
91 1
                RecursiveIteratorIterator::LEAVES_ONLY
92
            );
93
94 1
            foreach ($iterator as $file) {
95 1
                if ($file->getBasename('.php') === $file->getBasename()) {
96 1
                    continue;
97
                }
98
99 1
                $sourceFile = realpath($file->getPathName());
100 1
                require_once $sourceFile;
101 1
                $includedFiles[] = $sourceFile;
102
            }
103
        }
104
105 1
        $declared = get_declared_classes();
106
107 1
        foreach ($declared as $className) {
108 1
            $rc         = new ReflectionClass($className);
109 1
            $sourceFile = $rc->getFileName();
110 1
            if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
111 1
                continue;
112
            }
113
114 1
            $classes[] = $className;
115
        }
116
117 1
        $this->classNames = $classes;
118
119 1
        return $classes;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1
    public function isTransient($className)
126
    {
127 1
        return ! method_exists($className, 'loadMetadata');
128
    }
129
}
130