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

AnnotationGenerator::getJsonObjects()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 0
cts 16
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 1
crap 30
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