Completed
Push — master ( 6c80b5...e40af7 )
by Nikola
03:03
created

Tokenizer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 48
ccs 0
cts 35
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D findClass() 0 45 18
1
<?php
2
3
namespace RunOpenCode\AbstractBuilder\Helper;
4
5
use RunOpenCode\AbstractBuilder\Exception\RuntimeException;
6
7
class Tokenizer
8
{
9
    public static function findClass($path)
10
    {
11
        if (!file_exists($path) || !is_file($path)) {
12
            throw new RuntimeException(sprintf('There is no file on path "%s".', $path));
13
        }
14
15
        if (!is_readable($path)) {
16
            throw new RuntimeException(sprintf('There is a file on path "%s", but it is not readable.', $path));
17
        }
18
19
        $contents = file_get_contents($path);
20
        $namespace = $class = '';
21
        $getting_namespace = $getting_class = false;
22
23
        foreach (token_get_all($contents) as $token) {
24
25
            if (is_array($token) && $token[0] === T_NAMESPACE) {
26
                $getting_namespace = true;
27
            }
28
29
            if (is_array($token) && $token[0] === T_CLASS) {
30
                $getting_class = true;
31
            }
32
33
            if ($getting_namespace === true) {
34
35
                if(is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR], true)) {
36
                    $namespace .= $token[1];
37
                } elseif ($token === ';') {
38
                    $getting_namespace = false;
39
                }
40
            }
41
42
            if ($getting_class === true && is_array($token) && $token[0] === T_STRING) {
43
                $class = $token[1];
44
                break;
45
            }
46
        }
47
48
        if ('' === $namespace.$class) {
49
            throw new RuntimeException(sprintf('There is no class definition in file on path "%s".', $path));
50
        }
51
52
        return $namespace ? $namespace . '\\' . $class : $class;
53
    }
54
}
55