Failed Conditions
Push — types ( 96e63a...5cd110 )
by Jonathan
01:59
created

StaticPHPDriver::getAllClassNames()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.1228

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 51
ccs 25
cts 28
cp 0.8929
rs 7.6666
c 0
b 0
f 0
cc 10
nc 15
nop 0
crap 10.1228

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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