Failed Conditions
Pull Request — experimental/3.1 (#2268)
by chihiro
68:29 queued 15:11
created

AnnotationDriver   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 8.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 83
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setTraitProxiesDirectory() 0 4 1
C getAllClassNames() 7 70 13

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
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Doctrine\ORM\Mapping\Driver;
25
26
use Doctrine\Common\Persistence\Mapping\MappingException;
27
28
/**
29
 * @package Eccube\Doctrine\ORM\Mapping\Driver
30
 */
31
class AnnotationDriver extends \Doctrine\ORM\Mapping\Driver\AnnotationDriver
32
{
33
    protected $trait_proxies_directory;
34
35
    public function setTraitProxiesDirectory($dir)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
36
    {
37
        $this->trait_proxies_directory = $dir;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function getAllClassNames()
44
    {
45
        if ($this->classNames !== null) {
46
            return $this->classNames;
47
        }
48
49
        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...
50
            throw MappingException::pathRequired();
51
        }
52
53
        $classes = [];
54
        $includedFiles = [];
55
56
        foreach ($this->paths as $path) {
57
            if ( ! is_dir($path)) {
58
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
59
            }
60
61
            $iterator = new \RegexIterator(
62
                new \RecursiveIteratorIterator(
63
                    new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
64
                    \RecursiveIteratorIterator::LEAVES_ONLY
65
                ),
66
                '/^.+' . preg_quote($this->fileExtension) . '$/i',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
67
                \RecursiveRegexIterator::GET_MATCH
68
            );
69
70
            foreach ($iterator as $file) {
71
                $sourceFile = $file[0];
72
73
                if ( ! preg_match('(^phar:)i', $sourceFile)) {
74
                    $sourceFile = realpath($sourceFile);
75
                }
76
77
                foreach ($this->excludePaths as $excludePath) {
78
                    $exclude = str_replace('\\', '/', realpath($excludePath));
79
                    $current = str_replace('\\', '/', $sourceFile);
80
81
                    if (strpos($current, $exclude) !== false) {
82
                        continue 2;
83
                    }
84
                }
85
86
                $proxyFile = str_replace($path, $this->trait_proxies_directory, $sourceFile);
87
                if (file_exists($proxyFile)) {
88
                    require_once $proxyFile;
89
90
                    $sourceFile = $proxyFile;
91
                } else {
92
                    require_once $sourceFile;
93
                }
94
95
                $includedFiles[] = $sourceFile;
96
            }
97
        }
98
99
        $declared = get_declared_classes();
100
101 View Code Duplication
        foreach ($declared as $className) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
            $rc = new \ReflectionClass($className);
103
            $sourceFile = $rc->getFileName();
104
            if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
105
                $classes[] = $className;
106
            }
107
        }
108
109
        $this->classNames = $classes;
110
111
        return $classes;
112
    }
113
}