Autoloader::autoload()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 8
cp 0.5
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 6
1
<?php
2
/**
3
 * This file is part of PHPOffice Common
4
 *
5
 * PHPOffice Common is free software distributed under the terms of the GNU Lesser
6
 * General Public License version 3 as published by the Free Software Foundation.
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code. For the full list of
10
 * contributors, visit https://github.com/PHPOffice/Common/contributors.
11
 *
12
 * @link        https://github.com/PHPOffice/Common
13
 * @copyright   2009-2016 PHPOffice Common contributors
14
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15
 */
16
17
namespace PhpOffice\Common;
18
19
/**
20
 * Autoloader
21
 */
22
class Autoloader
23
{
24
    /** @const string */
25
    const NAMESPACE_PREFIX = 'PhpOffice\\Common\\';
26
27
    /**
28
     * Register
29
     *
30
     * @return void
31
     */
32 1
    public static function register()
33
    {
34 1
        spl_autoload_register(array(new self, 'autoload'));
35 1
    }
36
37
    /**
38
     * Autoload
39
     *
40
     * @param string $class
41
     */
42 1
    public static function autoload($class)
43
    {
44 1
        $prefixLength = strlen(self::NAMESPACE_PREFIX);
45 1
        if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) {
46
            $file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
47
            $file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
48
            if (file_exists($file)) {
49
                /** @noinspection PhpIncludeInspection Dynamic includes */
50
                require_once $file;
51
            }
52
        }
53 1
    }
54
}
55