Failed Conditions
Pull Request — 1.3.x (#71)
by Grégoire
02:19
created

StaticPHPDriver::getAllClassNames()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.1228

Importance

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

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