Test Failed
Pull Request — 1.1 (#49)
by Guido
17:25 queued 04:21
created

helpers.php ➔ mappingsFrom()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nc 21
nop 2
dl 0
loc 53
rs 6.2926
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
 *
19
 * @throws MappingException
20
 *
21
 * @return Mapping[]
22
 */
23
function mappingsFrom(array $paths, $fileExtension = '.php')
24
{
25
    $includedFiles = [];
26
27
    foreach ($paths as $path) {
28
        if (!is_dir($path)) {
29
            throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
30
        }
31
32
        $iterator = new RegexIterator(
33
            new RecursiveIteratorIterator(
34
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
35
                RecursiveIteratorIterator::LEAVES_ONLY
36
            ),
37
            '/^.+'.preg_quote($fileExtension, '/').'$/i',
38
            RecursiveRegexIterator::GET_MATCH
39
        );
40
41
        foreach ($iterator as $file) {
42
            $sourceFile = $file[0];
43
44
            if (!preg_match('(^phar:)i', $sourceFile)) {
45
                $sourceFile = realpath($sourceFile);
46
            }
47
48
            require_once $sourceFile;
49
50
            $includedFiles[$sourceFile] = true;
51
        }
52
    }
53
54
    $mappings = [];
55
    $declared = get_declared_classes();
56
    foreach ($declared as $className) {
57
        $rc = new ReflectionClass($className);
58
        $sourceFile = $rc->getFileName();
59
        if ($sourceFile === false || !array_key_exists($sourceFile, $includedFiles)) {
60
            continue;
61
        }
62
63
        if ($rc->isAbstract() || $rc->isInterface()) {
64
            continue;
65
        }
66
67
        if (!$rc->implementsInterface(Mapping::class)) {
68
            continue;
69
        }
70
71
        $mappings[] = $rc->newInstanceWithoutConstructor();
72
    }
73
74
    return $mappings;
75
}
76