Passed
Push — develop ( 267ce5...6afb61 )
by Jens
14:16
created

AnnotationGenerator   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 134
Duplicated Lines 44.78 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 1
dl 60
loc 134
ccs 0
cts 99
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 1
B analyzeFiles() 0 26 4
B getJsonObjects() 18 18 5
B getCollectionObjects() 18 18 5
B getRequestObjects() 18 18 5
C getClassName() 6 23 7
A tokenize() 0 5 1

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
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Helper\Annotate;
7
8
use Commercetools\Core\Model\Common\Collection;
9
use Commercetools\Core\Model\Common\JsonObject;
10
use Commercetools\Core\Request\AbstractApiRequest;
11
12
class AnnotationGenerator
13
{
14
    /**
15
     * @var \ReflectionClass
16
     */
17
    protected $reflectionClass;
18
    protected $newDocBlock;
19
    protected $fields;
20
    protected $fieldNames;
21
    protected $includes;
22
    protected $uses;
23
24
    public function run($path)
25
    {
26
        $allFiles = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
27
        $phpFiles = new \RegexIterator($allFiles, '/\.php$/');
28
29
        $this->analyzeFiles($phpFiles);
30
    }
31
32
    protected function analyzeFiles(\RegexIterator $phpFiles)
33
    {
34
        $jsonObjects = $this->getJsonObjects($phpFiles);
35
36
        foreach ($jsonObjects as $jsonObject) {
37
            $annotator = new ClassAnnotator($jsonObject);
38
39
            $annotator->generate();
40
        }
41
42
        $collections = $this->getCollectionObjects($phpFiles);
43
44
        foreach ($collections as $collection) {
45
            $annotator = new ClassAnnotator($collection);
46
47
            $annotator->generateCurrentMethod();
48
        }
49
50
        $requests = $this->getRequestObjects($phpFiles);
51
52
        foreach ($requests as $request) {
53
            $annotator = new ClassAnnotator($request);
54
55
            $annotator->generateMapResponseMethod();
56
        }
57
    }
58
59 View Code Duplication
    protected function getJsonObjects(\RegexIterator $phpFiles)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        $jsonObjects = [];
62
        foreach ($phpFiles as $phpFile) {
63
            $class = $this->getClassName($phpFile->getRealPath());
64
            if (strpos($class, 'Core\\Helper') > 0) {
65
                continue;
66
            }
67
68
            if (!empty($class)) {
69
                if (in_array(JsonObject::class, class_parents($class))) {
70
                    $jsonObjects[] = $class;
71
                }
72
            }
73
        }
74
75
        return $jsonObjects;
76
    }
77
78 View Code Duplication
    protected function getCollectionObjects(\RegexIterator $phpFiles)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
    {
80
        $collectionObjects = [];
81
        foreach ($phpFiles as $phpFile) {
82
            $class = $this->getClassName($phpFile->getRealPath());
83
            if (strpos($class, 'Core\\Helper') > 0) {
84
                continue;
85
            }
86
87
            if (!empty($class)) {
88
                if (in_array(Collection::class, class_parents($class))) {
89
                    $collectionObjects[] = $class;
90
                }
91
            }
92
        }
93
94
        return $collectionObjects;
95
    }
96
97 View Code Duplication
    protected function getRequestObjects(\RegexIterator $phpFiles)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
98
    {
99
        $requestObjects = [];
100
        foreach ($phpFiles as $phpFile) {
101
            $class = $this->getClassName($phpFile->getRealPath());
102
            if (strpos($class, 'Core\\Helper') > 0) {
103
                continue;
104
            }
105
106
            if (!empty($class)) {
107
                if (in_array(AbstractApiRequest::class, class_parents($class))) {
108
                    $requestObjects[] = $class;
109
                }
110
            }
111
        }
112
113
        return $requestObjects;
114
    }
115
116
    protected function getClassName($fileName)
117
    {
118
        $tokens = $this->tokenize($fileName);
119
        $namespace = '';
120
        for ($index = 0; isset($tokens[$index]); $index++) {
121
            if (!isset($tokens[$index][0])) {
122
                continue;
123
            }
124 View Code Duplication
            if (T_NAMESPACE === $tokens[$index][0]) {
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...
125
                $index += 2; // Skip namespace keyword and whitespace
126
                while (isset($tokens[$index]) && is_array($tokens[$index])) {
127
                    $namespace .= $tokens[$index++][1];
128
                }
129
            }
130
            if (T_CLASS === $tokens[$index][0]) {
131
                $index += 2; // Skip class keyword and whitespace
132
                $class = $namespace.'\\'.$tokens[$index][1];
133
                return $class;
134
            }
135
        }
136
137
        return null;
138
    }
139
140
    protected function tokenize($fileName)
141
    {
142
        $content = file_get_contents($fileName);
143
        return token_get_all($content);
144
    }
145
}
146