Completed
Push — master ( f8b6bf...bec27e )
by Mahmoud
03:02
created

LoaderHelper::getClassNamespaceFromFile()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 5.3846
cc 8
eloc 22
nc 8
nop 1
1
<?php
2
3
namespace App\Port\Loader\Helpers;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Facades\File;
7
8
/**
9
 * Class LoaderHelper
10
 *
11
 * @author  Mahmoud Zalt  <[email protected]>
12
 */
13
class LoaderHelper
14
{
15
16
    /**
17
     * Get the containers namespace value from the containers config file
18
     *
19
     * @return  string
20
     */
21
    public function getContainersNamespace()
22
    {
23
        return Config::get('hello.containers.namespace');
24
    }
25
26
    /**
27
     * Get the containers names
28
     *
29
     * @return  array
30
     */
31
    public function getContainersNames()
32
    {
33
        $containersNames = [];
34
35
        foreach ($this->getContainersPaths() as $containersPath) {
36
            $containersNames[] = basename($containersPath);
37
        }
38
39
        return $containersNames;
40
    }
41
42
    /**
43
     * Get the port folders names
44
     *
45
     * @return  array
46
     */
47
    public function getPortFoldersNames()
48
    {
49
        $portFoldersNames = [];
50
51
        foreach ($this->getPortPaths() as $portFoldersPath) {
52
            $portFoldersNames[] = basename($portFoldersPath);
53
        }
54
55
        return $portFoldersNames;
56
    }
57
58
    /**
59
     * get containers directories paths
60
     *
61
     * @return  mixed
62
     */
63
    public function getContainersPaths()
64
    {
65
        $containersPaths = File::directories(app_path('Containers'));
66
67
        // TODO: preparing some code to implement the Ships concept to Porto
68
69
//        // Search for Ships "_SHIP" of Containers
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
//        foreach ($containersPaths as $containersPath) {
71
//
72
//            if (preg_match('/_SHIP/',$containersPath)){
73
//
74
//                // append the Ship directories to the Containers directory, since it will contain containers
75
//                $shipContainers = File::directories($containersPath);
76
//                $containersPaths = array_merge($containersPaths, $shipContainers);
77
//            }
78
//        }
79
80
        return $containersPaths;
81
    }
82
83
    /**
84
     * @return  mixed
85
     */
86
    public function getPortPaths()
87
    {
88
        return File::directories(app_path('Port'));
89
    }
90
91
    /**
92
     * build and return an object of a class from its file path
93
     *
94
     * @param $filePathName
95
     *
96
     * @return  mixed
97
     */
98
    public function getClassObjectFromFile($filePathName)
99
    {
100
        $classString = $this->getClassFullNameFromFile($filePathName);
101
102
        $object = new $classString;
103
104
        return $object;
105
    }
106
107
    /**
108
     * get the full name (name \ namespace) of a class from its file path
109
     * result example: (string) "I\Am\The\Namespace\Of\This\Class"
110
     *
111
     * @param $filePathName
112
     *
113
     * @return  string
114
     */
115
    public function getClassFullNameFromFile($filePathName)
116
    {
117
        return $this->getClassNamespaceFromFile($filePathName) . '\\' . $this->getClassNameFromFile($filePathName);
118
    }
119
120
    /**
121
     * get the class namespace form file path using token
122
     *
123
     * @param $filePathName
124
     *
125
     * @return  null|string
126
     */
127
    protected function getClassNamespaceFromFile($filePathName)
128
    {
129
        $src = file_get_contents($filePathName);
130
131
        $tokens = token_get_all($src);
132
        $count = count($tokens);
133
        $i = 0;
134
        $namespace = '';
135
        $namespace_ok = false;
136
        while ($i < $count) {
137
            $token = $tokens[$i];
138
            if (is_array($token) && $token[0] === T_NAMESPACE) {
139
                // Found namespace declaration
140
                while (++$i < $count) {
141
                    if ($tokens[$i] === ';') {
142
                        $namespace_ok = true;
143
                        $namespace = trim($namespace);
144
                        break;
145
                    }
146
                    $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
147
                }
148
                break;
149
            }
150
            $i++;
151
        }
152
        if (!$namespace_ok) {
153
            return null;
154
        } else {
155
            return $namespace;
156
        }
157
    }
158
159
    /**
160
     * get the class name form file path using token
161
     *
162
     * @param $filePathName
163
     *
164
     * @return  mixed
165
     */
166
    protected function getClassNameFromFile($filePathName)
167
    {
168
        $php_code = file_get_contents($filePathName);
169
170
        $classes = array();
171
        $tokens = token_get_all($php_code);
172
        $count = count($tokens);
173
        for ($i = 2; $i < $count; $i++) {
174
            if ($tokens[$i - 2][0] == T_CLASS
175
                && $tokens[$i - 1][0] == T_WHITESPACE
176
                && $tokens[$i][0] == T_STRING
177
            ) {
178
179
                $class_name = $tokens[$i][1];
180
                $classes[] = $class_name;
181
            }
182
        }
183
184
        return $classes[0];
185
    }
186
187
}
188