Completed
Push — master ( 1d2d63...5f432b )
by Jens
09:03
created

AnnotationGenerator::getClassName()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 6
Ratio 26.09 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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