Passed
Push — master ( 1f470b...e71961 )
by Justin
03:43
created

cms_autoloader()   D

Complexity

Conditions 27
Paths 77

Size

Total Lines 103
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
c 4
b 3
f 1
dl 0
loc 103
rs 4.509
cc 27
eloc 67
nc 77
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
class ClassLoader {
20
21
    public static $classlist = array();
22
23
    public static $loadedClasses = 0;
24
25
    /**
26
     * initialize classloader (called only once / request)
27
     */
28
    public static function init () {
29
30
        //register autoloader
31
        spl_autoload_register('cms_autoloader');
32
33
        if (!file_exists(ROOT_PATH . "cache")) {
34
            mkdir(ROOT_PATH . "cache");
35
        }
36
37
        if (!file_exists(ROOT_PATH . "cache/classloader/classlist.php")) {
38
            self::rebuildCache();
39
        }
40
41
        require(ROOT_PATH . "cache/classloader/classlist.php");
42
        self::$classlist = $classlist;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $classlist seems to be never defined.
Loading history...
43
44
    }
45
46
    public static function rebuildCache () {
47
48
        require_once(ROOT_PATH . "system/core/classes/packages.php");
49
50
        if (file_exists(ROOT_PATH . "cache/classloader/classlist.php")) {
51
            @unlink(ROOT_PATH . "cache/classloader/classlist.php");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

51
            /** @scrutinizer ignore-unhandled */ @unlink(ROOT_PATH . "cache/classloader/classlist.php");

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
52
        }
53
54
        if (!file_exists(ROOT_PATH . "cache/classloader")) {
55
            mkdir(ROOT_PATH . "cache/classloader");
56
        }
57
58
        $packages = Packages::listPackages();
59
60
        $classlist = array();
61
62
        foreach ($packages as $path) {
63
            $path = ROOT_PATH . "system/packages/" . $path . "/";
64
65
            if (file_exists($path . "classloader.xml")) {
66
                $xml = simplexml_load_file($path . "classloader.xml");
67
68
                foreach ($xml->xpath("//class") as $classname) {
69
                    $classlist[(String) $classname] = $path . "classes/" . strtolower((String) $classname) . ".php";
70
                }
71
            }
72
        }
73
74
        $handle = fopen(ROOT_PATH . "cache/classloader/classlist.php", "w");
75
76
        fwrite($handle, "<" . "?" . "php\r\n\r\n");
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        fwrite(/** @scrutinizer ignore-type */ $handle, "<" . "?" . "php\r\n\r\n");
Loading history...
77
78
        fwrite($handle, "$" . "classlist = array(\r\n");
79
80
        foreach ($classlist as $classname=>$classpath) {
81
            fwrite($handle, "\t'" . $classname . "' => \"" . $classpath . "\",\r\n");
82
        }
83
84
        fwrite($handle, ");\r\n\r\n");
85
86
        fwrite($handle, "?" . ">");
87
88
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
89
90
    }
91
92
}
93
94
/**
95
 * autoload function
96
 */
97
function cms_autoloader ($classname) {
98
99
    ClassLoader::$loadedClasses++;
100
101
    if (isset(Classloader::$classlist[$classname])) {
102
        require(Classloader::$classlist[$classname]);
103
        return null;
104
    }
105
106
    if (file_exists(ROOT_PATH . "system/core/classes/" . strtolower($classname) . ".php")) {
107
        require(ROOT_PATH . "system/core/classes/" . strtolower($classname) . ".php");
108
        return null;
109
    } else if (file_exists(ROOT_PATH . "system/core/exception/" . strtolower($classname) . ".php")) {
110
		require(ROOT_PATH . "system/core/exception/" . strtolower($classname) . ".php");
111
		return null;
112
	} else if (file_exists(ROOT_PATH . "system/core/driver/" . strtolower($classname) . ".php")) {
113
		require(ROOT_PATH . "system/core/driver/" . strtolower($classname) . ".php");
114
		return null;
115
	}
116
117
	//check, if class belongs to dwoo template engine
118
    if (PHPUtils::startsWith($classname, "Dwoo")) {
119
        if (class_exists("DwooAutoloader", true)) {
120
            DwooAutoloader::loadClass($classname);
121
            return;
122
        } else {
123
			echo "Could not load Dwoo template engine class " . $classname . "!";
124
        }
125
    }
126
127
    //check, if we have to use namespace classloading
128
	if (PHPUtils::containsStr($classname, "\\")) {
129
    	//we have to use namespace classloading
130
		if (PHPUtils::startsWith($classname, "\\")) {
131
			//use normal class loading
132
			$classname = substr($classname, 1);
133
		} else {
134
			$array = explode("_", $classname);
135
136
			if ($array[0] === "Plugin") {
137
				//load plugin class
138
				$path = PLUGIN_PATH . $array[1] . "/" . str_replace("\\", "/", $array[2]) . ".php";
139
140
				if (file_exists($path)) {
141
					require($path);
142
				} else {
143
					$expected_str = (DEBUG_MODE ? " (expected path: " . $path . ")" : "");
144
					echo "Could not load plugin-class with namespace " . $classname . $expected_str . "!";
145
				}
146
			}
147
148
			return;
149
		}
150
	}
151
152
    $array = explode("_", strtolower($classname));
153
154
    if (sizeOf($array) == 3) {
155
156
        if ($array[0] == "plugin") {
157
            if (file_exists(ROOT_PATH . "plugins/" . strtolower($array[1]) . "/classes/" . strtolower($array[2]) . ".php")) {
158
                require(ROOT_PATH . "plugins/" . strtolower($array[1]) . "/classes/" . strtolower($array[2]) . ".php");
159
            } else {
160
                echo "Could not load plugin-class " . $classname . "!";
161
            }
162
        } else {
163
            if (file_exists(ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . "php")) {
164
                require ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . ".php";
165
            } else if ($classname == "Smarty") {
166
                require("system/libs/smarty/Smarty.class.php");
167
            } else {
168
                //echo "Could not (plugin) load class " . $classname . "!";
169
            }
170
        }
171
172
    } else if (sizeof($array) == 2) {
173
		if ($array[0] == "validator") {
174
			if (file_exists(ROOT_PATH . "system/core/validator/" . $array[1] . ".php")) {
175
				require(ROOT_PATH . "system/core/validator/" . $array[1] . ".php");
176
			} else {
177
				echo "Could not load validator class " . $classname . "!";
178
			}
179
		} else if ($array[0] == "datatype") {
180
			if (file_exists(ROOT_PATH . "system/core/datatype/" . $array[1] . ".php")) {
181
				require(ROOT_PATH . "system/core/datatype/" . $array[1] . ".php");
182
			} else {
183
				echo "Could not load datatype class " . $classname . "!";
184
			}
185
		} else if (strpos($classname, "Plugin")) {
186
			//dwoo tries several times to load a class - with and without namespace, so we hide this error message
187
		} else {
188
			echo "Could not load class " . $classname . ", unknown prefix '" . $array[0] . "'!";
189
        }
190
	} else if (sizeOf($array) == 1) {
191
192
        if (file_exists(ROOT_PATH . "system/classes/" . strtolower($classname) . ".php")) {
193
            include ROOT_PATH . "system/classes/" . strtolower($classname) . ".php";
194
        } else if (file_exists(ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . "php")) {
195
            require ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . ".php";
196
        } else if (strpos($classname, "Plugin") !== FALSE) {
197
			//dwoo tries several times to load a class - with and without namespace, so we hide this error message
198
		} else {
199
            echo "Could not load class '" . $classname . "'' (array size 1)!";
200
        }
201
202
    }
203
204
}
205
206
207
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
208