Failed Conditions
Pull Request — master (#2)
by Jonathan
03:29
created

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