Completed
Pull Request — experimental/sf (#3456)
by Kiyotaka
87:43 queued 81:34
created

AnnotationDriver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 14.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75.56%

Importance

Changes 0
Metric Value
dl 13
loc 87
ccs 34
cts 45
cp 0.7556
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setTraitProxiesDirectory() 0 4 1
C getAllClassNames() 13 74 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Doctrine\ORM\Mapping\Driver;
15
16
use Doctrine\Common\Persistence\Mapping\MappingException;
17
18
class AnnotationDriver extends \Doctrine\ORM\Mapping\Driver\AnnotationDriver
19
{
20
    protected $trait_proxies_directory;
21
22 1326
    public function setTraitProxiesDirectory($dir)
23
    {
24 1326
        $this->trait_proxies_directory = $dir;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 13
    public function getAllClassNames()
31
    {
32 13
        if ($this->classNames !== null) {
33 4
            return $this->classNames;
34
        }
35
36 13
        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...
37
            throw MappingException::pathRequired();
38
        }
39
40 13
        $classes = [];
41 13
        $includedFiles = [];
42
43 13
        foreach ($this->paths as $path) {
44 13
            if (!is_dir($path)) {
45
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
46
            }
47
48 13
            $iterator = new \RegexIterator(
49 13
                new \RecursiveIteratorIterator(
50 13
                    new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
51 13
                    \RecursiveIteratorIterator::LEAVES_ONLY
52
                ),
53 13
                '/^.+'.preg_quote($this->fileExtension).'$/i',
54 13
                \RecursiveRegexIterator::GET_MATCH
55
            );
56
57 13
            foreach ($iterator as $file) {
58 13
                $sourceFile = $file[0];
59
60 13
                if (!preg_match('(^phar:)i', $sourceFile)) {
61 13
                    $sourceFile = realpath($sourceFile);
62
                }
63
64 13 View Code Duplication
                foreach ($this->excludePaths as $excludePath) {
65
                    $exclude = str_replace('\\', '/', realpath($excludePath));
66
                    $current = str_replace('\\', '/', $sourceFile);
67
68
                    if (strpos($current, $exclude) !== false) {
69
                        continue 2;
70
                    }
71
                }
72 13 View Code Duplication
                if ('\\' === DIRECTORY_SEPARATOR) {
73
                    $path = str_replace('\\', '/', $path);
74
                    $this->trait_proxies_directory = str_replace('\\', '/', $this->trait_proxies_directory);
75
                    $sourceFile = str_replace('\\', '/', $sourceFile);
76
                }
77 13
                $proxyFile = str_replace($path, $this->trait_proxies_directory, $sourceFile);
78 13
                if (file_exists($proxyFile)) {
79
                    require_once $proxyFile;
80
81
                    $sourceFile = $proxyFile;
82
                } else {
83 13
                    require_once $sourceFile;
84
                }
85
86 13
                $includedFiles[] = realpath($sourceFile);
87
            }
88
        }
89
90 13
        $declared = get_declared_classes();
91
92 13
        foreach ($declared as $className) {
93 13
            $rc = new \ReflectionClass($className);
94 13
            $sourceFile = $rc->getFileName();
95 13
            if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
96 13
                $classes[] = $className;
97
            }
98
        }
99
100 13
        $this->classNames = $classes;
101
102 13
        return $classes;
103
    }
104
}
105