Failed Conditions
Pull Request — master (#1)
by Jonathan
03:48
created

StaticPHPDriver::getAllClassNames()   C

Complexity

Conditions 10
Paths 15

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.1371

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 24
cts 27
cp 0.8889
rs 5.1578
c 0
b 0
f 0
cc 10
eloc 26
nc 15
nop 0
crap 10.1371

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