Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

micro/base/Autoload.php (4 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
    /**
40
     * Loader classes
41
     *
42
     * @access public
43
     *
44
     * @param string $className search class name
45
     *
46
     * @return bool
47
     * @static
48
     */
49
    public static function loader($className)
50
    {
51
        if ($path = static::getClassPath(ltrim($className, '\\'))) {
52
            /** @noinspection PhpIncludeInspection */
53
            require_once $path;
54
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * Get class path
63
     *
64
     * @access public
65
     *
66
     * @param string $className search class name
67
     * @param string $extension extension of class
68
     *
69
     * @return string
70
     * @static
71
     */
72
    public static function getClassPath($className, $extension = '.php')
73
    {
74
        $prefix = $className = static::CamelCaseToLowerNamespace(str_replace('_', '\\', $className));
0 ignored issues
show
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...
75
76
        while (false !== $position = strrpos($prefix, '\\')) {
77
            $prefix = substr($prefix, 0, $position);
78
79
            if (!array_key_exists($prefix, static::$aliases)) {
0 ignored issues
show
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...
80
                continue;
81
            }
82
83
            foreach (static::$aliases[$prefix] as $dir) {
0 ignored issues
show
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...
84
                $path         = $dir . '\\' . substr($className, mb_strlen($prefix) + 1);
85
                $absolutePath = str_replace('\\', DIRECTORY_SEPARATOR, $path) . $extension;
86
87
                if (is_readable($absolutePath)) {
88
                    return $absolutePath;
89
                }
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Convert first symbols of namespace to lowercase
98
     *
99
     * @access protected
100
     *
101
     * @param string $path
102
     *
103
     * @return string
104
     * @static
105
     */
106
    private static function CamelCaseToLowerNamespace($path)
107
    {
108
        $classNameArr = array_map(function($val) {
109
            return lcfirst($val);
110
        }, explode('\\', $path));
111
112
        $classNameArr[] = ucfirst(array_pop($classNameArr));
113
114
        return implode('\\', $classNameArr);
115
    }
116
}
117