Completed
Push — master ( c612e7...f3e1c8 )
by Christian
06:23
created

FunctionAvailabilityCheck::prepareList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Util;
22
23
/**
24
 * This class provides methods to check if certain functions have been disabled in PHP or are callable.
25
 */
26
class FunctionAvailabilityCheck
1 ignored issue
show
Coding Style introduced by
FunctionAvailabilityCheck does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
{
28
    /**
29
     * Cache.
30
     *
31
     * @var string[]
32
     */
33
    private static $blackListSuhosin;
34
35
    /**
36
     * Cache.
37
     *
38
     * @var string[]
39
     */
40
    private static $blackListPhpIni;
41
42
43
    /**
44
     * Check if function is defined.
45
     *
46
     * @param string $function  The function to test.
47
     *
48
     * @param string $extension The optional name of an php extension providing said function.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $extension not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
49
     *
50
     * @return bool
51
     */
52
    public static function isFunctionEnabled($function, $extension = null)
53
    {
54
        return
55
            (null === $extension || extension_loaded($extension))
56
            && !static::isFunctionBlacklistedInPhpIni($function)
57
            && !static::isFunctionBlacklistedInSuhosin($function)
58
            && static::isFunctionDefined($function);
59
    }
60
61
    /**
62
     * Check if function is defined.
63
     *
64
     * @param string $function The function to test.
65
     *
66
     * @return bool
67
     */
68
    public static function isFunctionDefined($function)
69
    {
70
        return function_exists($function);
71
    }
72
73
    /**
74
     * Check if function is blacklisted in Suhosin.
75
     *
76
     * @param string $function The function to test.
77
     *
78
     * @return bool
79
     */
80
    public static function isFunctionBlacklistedInSuhosin($function)
81
    {
82
        if (!extension_loaded('suhosin')) {
83
            return false;
84
        }
85
86
        if (!isset(static::$blackListSuhosin)) {
0 ignored issues
show
Bug introduced by
Since $blackListSuhosin is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blackListSuhosin 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
            static::$blackListSuhosin = static::prepareList(ini_get('suhosin.executor.func.blacklist'));
0 ignored issues
show
Bug introduced by
Since $blackListSuhosin is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blackListSuhosin 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...
Bug introduced by
Since prepareList() 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 prepareList() 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...
88
        }
89
90
        return static::isFunctionsMentionedInList($function, static::$blackListSuhosin);
0 ignored issues
show
Bug introduced by
Since $blackListSuhosin is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blackListSuhosin 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
    }
92
93
    /**
94
     * Check if method is blacklisted in Suhosin.
95
     *
96
     * @param string $function The function to test.
97
     *
98
     * @return bool
99
     */
100
    public static function isFunctionBlacklistedInPhpIni($function)
101
    {
102
        if (!isset(static::$blackListPhpIni)) {
0 ignored issues
show
Bug introduced by
Since $blackListPhpIni is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blackListPhpIni 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...
103
            static::$blackListPhpIni = static::prepareList(ini_get('disable_functions'));
0 ignored issues
show
Bug introduced by
Since $blackListPhpIni is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blackListPhpIni 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...
Bug introduced by
Since prepareList() 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 prepareList() 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...
104
        }
105
106
        return static::isFunctionsMentionedInList($function, static::$blackListPhpIni);
0 ignored issues
show
Bug introduced by
Since $blackListPhpIni is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $blackListPhpIni 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...
107
    }
108
109
    /**
110
     * Check if a function is mentioned in the passed (comma separated) list.
111
     *
112
     * @param string   $function The function to test.
113
     *
114
     * @param string[] $list     The function list.
115
     *
116
     * @return bool
117
     */
118
    public static function isFunctionsMentionedInList($function, $list)
119
    {
120
        if (empty($list)) {
121
            return false;
122
        }
123
124
        return (false !== array_search($function, $list));
125
    }
126
127
    /**
128
     * Explode a list.
129
     *
130
     * @param string $list The list.
131
     *
132
     * @return string[]
133
     */
134
    private static function prepareList($list)
135
    {
136
        return array_map('strtolower', array_map('trim', explode(',', $list, -1)));
137
    }
138
}
139