Test Failed
Push — master ( 72dea3...a22c3b )
by Justin
19:22 queued 15:40
created

cms_autoloader()   C

Complexity

Conditions 22
Paths 37

Size

Total Lines 78
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
cc 22
eloc 54
c 4
b 3
f 1
nc 37
nop 1
dl 0
loc 78
rs 5.0961

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
    $array = explode("_", strtolower($classname));
128
129
    if (sizeOf($array) == 3) {
130
131
        if ($array[0] == "plugin") {
132
            if (file_exists(ROOT_PATH . "plugins/" . strtolower($array[1]) . "/classes/" . strtolower($array[2]) . ".php")) {
133
                require(ROOT_PATH . "plugins/" . strtolower($array[1]) . "/classes/" . strtolower($array[2]) . ".php");
134
            } else {
135
                echo "Could not load plugin-class " . $classname . "!";
136
            }
137
        } else {
138
            if (file_exists(ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . "php")) {
139
                require ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . ".php";
140
            } else if ($classname == "Smarty") {
141
                require("system/libs/smarty/Smarty.class.php");
142
            } else {
143
                //echo "Could not (plugin) load class " . $classname . "!";
144
            }
145
        }
146
147
    } else if (sizeof($array) == 2) {
148
		if ($array[0] == "validator") {
149
			if (file_exists(ROOT_PATH . "system/core/validator/" . $array[1] . ".php")) {
150
				require(ROOT_PATH . "system/core/validator/" . $array[1] . ".php");
151
			} else {
152
				echo "Could not load validator class " . $classname . "!";
153
			}
154
		} else if ($array[0] == "datatype") {
155
			if (file_exists(ROOT_PATH . "system/core/datatype/" . $array[1] . ".php")) {
156
				require(ROOT_PATH . "system/core/datatype/" . $array[1] . ".php");
157
			} else {
158
				echo "Could not load datatype class " . $classname . "!";
159
			}
160
		} else if (strpos($classname, "Plugin")) {
161
			//dwoo tries several times to load a class - with and without namespace, so we hide this error message
162
		} else {
163
			echo "Could not load class " . $classname . ", unknown prefix '" . $array[0] . "'!";
164
        }
165
	} else if (sizeOf($array) == 1) {
166
167
        if (file_exists(ROOT_PATH . "system/classes/" . strtolower($classname) . ".php")) {
168
            include ROOT_PATH . "system/classes/" . strtolower($classname) . ".php";
169
        } else if (file_exists(ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . "php")) {
170
            require ROOT_PATH . "system/libs/smarty/sysplugins/" . strtolower($classname) . ".php";
171
        } else if (strpos($classname, "Plugin")) {
172
			//dwoo tries several times to load a class - with and without namespace, so we hide this error message
173
		} else {
174
            echo "Could not load class " . $classname . " (array size 1)!";
175
        }
176
177
    }
178
179
}
180
181
182
?>
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...
183