FileNameToClass::getClassFullNameFromFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Api;
4
5
class FileNameToClass
6
{
7
    /**
8
     * get the full name (name \ namespace) of a class from its file path
9
     * result example: (string) "I\Am\The\Namespace\Of\This\Class"
10
     */
11
    public function getClassFullNameFromFile(string $filePathName): string
12
    {
13
        return $this->getClassNamespaceFromFile($filePathName) . '\\' . $this->getClassNameFromFile($filePathName);
14
    }
15
16
    /**
17
     * build and return an object of a class from its file path
18
     *
19
     * @return mixed
20
     */
21
    public function getClassObjectFromFile(string $filePathName)
22
    {
23
        $classString = $this->getClassFullNameFromFile($filePathName);
24
25
        return new $classString();
26
    }
27
28
    /**
29
     * get the class namespace form file path using token
30
     */
31
    public function getClassNamespaceFromFile(string $filePathName): ?string
32
    {
33
        $src = (string) file_get_contents($filePathName);
34
35
        $tokens = token_get_all($src);
36
        $count = count($tokens);
37
        $i = 0;
38
        $namespace = '';
39
        $namespace_ok = false;
40
        while ($i < $count) {
41
            $token = $tokens[$i];
42
            if (is_array($token) && $token[0] === T_NAMESPACE) {
43
                // Found namespace declaration
44
                while (++$i < $count) {
45
                    if ($tokens[$i] === ';') {
46
                        $namespace_ok = true;
47
                        $namespace = trim($namespace);
48
                        break;
49
                    }
50
                    $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
51
                }
52
                break;
53
            }
54
            $i++;
55
        }
56
        if (! $namespace_ok) {
57
            return null;
58
        }
59
        return $namespace;
60
    }
61
62
    /**
63
     * get the class name form file path using token
64
     */
65
    public function getClassNameFromFile(string $filePathName): string
66
    {
67
        $php_code = (string) file_get_contents($filePathName);
68
69
        $classes = [];
70
        $tokens = token_get_all($php_code);
71
        $count = count($tokens);
72
        for ($i = 2; $i < $count; $i++) {
73
            if ($tokens[$i - 2][0] === T_CLASS
74
                && $tokens[$i - 1][0] === T_WHITESPACE
75
                && $tokens[$i][0] === T_STRING
76
            ) {
77
                $class_name = $tokens[$i][1];
78
                $classes[] = $class_name;
79
            }
80
        }
81
        if (isset($classes[0])) {
82
            return $classes[0];
83
        }
84
        return 'Error';
85
    }
86
}
87