Passed
Branch php-scrutinizer (9ddcba)
by Jens
09:45
created

AnnotationGenerator::tokenize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author @jenschude <[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
    protected function getJsonObjects(\RegexIterator $phpFiles)
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
    protected function getCollectionObjects(\RegexIterator $phpFiles)
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
    protected function getRequestObjects(\RegexIterator $phpFiles)
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
            if (T_NAMESPACE === $tokens[$index][0]) {
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