Completed
Push — master ( 7ee34d...c3ef57 )
by Oleg
03:16
created

Autoload::getAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php /** MicroAutoloader */
2
3
namespace Micro\Base;
4
5
/**
6
 * Autoload class file.
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/lugnsk/micro
10
 * @copyright Copyright &copy; 2013 Oleg Lunegov
11
 * @license /LICENSE
12
 * @package Micro
13
 * @subpackage Base
14
 * @version 1.0
15
 * @since 1.0
16
 */
17
class Autoload
18
{
19
    /** @var array $aliases Autoload aliases maps */
20
    private static $aliases = [];
21
22
23
    /**
24
     * Setting or installing new alias
25
     *
26
     * @access public
27
     *
28
     * @param string $alias name for new alias
29
     * @param string $realPath path of alias
30
     *
31
     * @return void
32
     * @static
33
     */
34
    public static function setAlias($alias, $realPath)
35
    {
36
        static::$aliases[strtolower($alias)][] = $realPath;
0 ignored issues
show
Bug introduced by
Since $aliases is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $aliases to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
37
    }
38
39
    public static function getAlias($alias)
40
    {
41
        $alias = strtolower($alias);
42
43
        return array_key_exists($alias, static::$aliases) ? static::$aliases[$alias] : [];
0 ignored issues
show
Bug introduced by
Since $aliases is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $aliases to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
44
    }
45
46
    /**
47
     * Loader classes
48
     *
49
     * @access public
50
     *
51
     * @param string $className search class name
52
     *
53
     * @return bool
54
     * @static
55
     */
56
    public static function loader($className)
57
    {
58
        if ($path = static::getClassPath(ltrim($className, '\\'))) {
59
            /** @noinspection PhpIncludeInspection */
60
            require_once $path;
61
62
            return true;
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * Get class path
70
     *
71
     * @access public
72
     *
73
     * @param string $className search class name
74
     * @param string $extension extension of class
75
     *
76
     * @return string
77
     * @static
78
     */
79
    public static function getClassPath($className, $extension = '.php')
80
    {
81
        $prefix = $className = static::CamelCaseToLowerNamespace(str_replace('_', '\\', $className));
0 ignored issues
show
Bug introduced by
Since CamelCaseToLowerNamespace() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of CamelCaseToLowerNamespace() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
82
83
        while (false !== $position = strrpos($prefix, '\\')) {
84
            $prefix = substr($prefix, 0, $position);
85
86
            if (!array_key_exists($prefix, static::$aliases)) {
0 ignored issues
show
Bug introduced by
Since $aliases is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $aliases to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
87
                continue;
88
            }
89
90
            foreach (static::$aliases[$prefix] as $dir) {
0 ignored issues
show
Bug introduced by
Since $aliases is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $aliases to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
91
                $path = $dir . '\\' . substr($className, mb_strlen($prefix) + 1);
92
                $absolutePath = str_replace('\\', DIRECTORY_SEPARATOR, $path) . $extension;
93
94
                if (is_readable($absolutePath)) {
95
                    return $absolutePath;
96
                }
97
            }
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * Convert first symbols of namespace to lowercase
105
     *
106
     * @access protected
107
     *
108
     * @param string $path
109
     *
110
     * @return string
111
     * @static
112
     */
113
    private static function CamelCaseToLowerNamespace($path)
114
    {
115
        $classNameArr = array_map(function ($val) {
116
            return lcfirst($val);
117
        }, explode('\\', $path));
118
119
        $classNameArr[] = ucfirst(array_pop($classNameArr));
120
121
        return implode('\\', $classNameArr);
122
    }
123
}
124