Test Failed
Push — alt-path-loader ( dd5973 )
by Guido
07:14
created

helpers.php ➔ mappingsFrom()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nc 21
nop 2
dl 0
loc 52
rs 5.9999
c 0
b 0
f 0

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
namespace LaravelDoctrine\Fluent;
4
5
use Doctrine\ORM\Mapping\MappingException;
6
use FilesystemIterator;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use RecursiveRegexIterator;
10
use ReflectionClass;
11
use RegexIterator;
12
13
/**
14
 * Returns an array of Mapping objects found on the given paths.
15
 *
16
 * @param  string[] $paths
17
 * @param  string   $fileExtension
18
 * @return Mapping[]
19
 *
20
 * @throws MappingException
21
 */
22
function mappingsFrom(array $paths, $fileExtension = '.php') {
23
    $includedFiles = [];
24
25
    foreach ($paths as $path) {
26
        if (!is_dir($path)) {
27
            throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
28
        }
29
30
        $iterator = new RegexIterator(
31
            new RecursiveIteratorIterator(
32
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
33
                RecursiveIteratorIterator::LEAVES_ONLY
34
            ),
35
            '/^.+'.preg_quote($fileExtension, '/').'$/i',
36
            RecursiveRegexIterator::GET_MATCH
37
        );
38
39
        foreach ($iterator as $file) {
40
            $sourceFile = $file[0];
41
42
            if (!preg_match('(^phar:)i', $sourceFile)) {
43
                $sourceFile = realpath($sourceFile);
44
            }
45
46
            require_once $sourceFile;
47
48
            $includedFiles[$sourceFile] = true;
49
        }
50
    }
51
52
    $mappings = [];
53
    $declared = get_declared_classes();
54
    foreach ($declared as $className) {
55
        $rc = new ReflectionClass($className);
56
        $sourceFile = $rc->getFileName();
57
        if ($sourceFile === false || !array_key_exists($sourceFile, $includedFiles)) {
58
            continue;
59
        }
60
61
        if ($rc->isAbstract() || $rc->isInterface()) {
62
            continue;
63
        }
64
65
        if (!$rc->implementsInterface(Mapping::class)) {
66
            continue;
67
        }
68
69
        $mappings[] = $rc->newInstanceWithoutConstructor();
70
    }
71
72
    return $mappings;
73
}
74