Completed
Pull Request — master (#258)
by Enrico
08:55
created

DeprecatedIniOptions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 115
ccs 76
cts 76
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 53 5
C pass() 0 22 7
A getMetadata() 0 15 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
7
8
use PhpParser\Node\Expr\FuncCall;
9
use PHPSA\Context;
10
use PHPSA\Analyzer\Pass\Metadata;
11
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12
13
class DeprecatedIniOptions extends AbstractFunctionCallAnalyzer
14
{
15
    const DESCRIPTION = 'Checks for use of deprecated php.ini options and gives alternatives if available.';
16
17
    protected $functions = [
18
        'ini_set' => 'ini_set',
19
        'ini_get' => 'ini_get',
20
        'ini_alter' => 'ini_alter',
21
        'ini_restore' => 'ini_restore'
22
    ];
23
24
    /**
25
     * Contains the ini options that are checked
26
     */
27
    protected $deprecatedOptions = [];
28
29
    /**
30
     * @param array $config The config values for the analyzer
31
     */
32 1
    public function __construct(array $config)
33
    {
34 1
        if ($config["check_5_3"] == true) {
35
            $check53 = [
36 1
                'define_syslog_variables' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
37 1
                'magic_quotes_gpc' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
38 1
                'magic_quotes_runtime' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
39 1
                'magic_quotes_sybase' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
40 1
                'register_globals' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
41 1
                'register_long_arrays' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
42 1
                'safe_mode' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
43 1
                'safe_mode_gid' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
44 1
                'safe_mode_include_dir' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
45 1
                'safe_mode_exec_dir' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
46 1
                'safe_mode_allowed_env_vars' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)',
47
                'safe_mode_protected_env_vars' => 'is a deprecated option since PHP 5.3.0 (removed in PHP 5.4.0)'
48 1
            ];
49 1
            $this->deprecatedOptions = array_merge($this->deprecatedOptions, $check53);
50 1
        }
51
52 1
        if ($config["check_5_4"] == true) {
53
            $check54 = [
54 1
                'allow_call_time_pass_reference' => 'is a deprecated option since PHP 5.4.0',
55 1
                'highlight.bg' => 'is a deprecated option since PHP 5.4.0',
56 1
                'zend.ze1_compatibility_mode' => 'is a deprecated option since PHP 5.4.0',
57 1
                'session.bug_compat_42' => 'is a deprecated option since PHP 5.4.0',
58 1
                'session.bug_compat_warn' => 'is a deprecated option since PHP 5.4.0',
59 1
                'y2k_compliance' => 'is a deprecated option since PHP 5.4.0',
60 1
                'xsl.security_prefs' => 'is a deprecated option since PHP 5.4.0 (removed in PHP 7.0.0). Use XsltProcessor->setSecurityPrefs() instead',
61 1
            ];
62 1
            $this->deprecatedOptions = array_merge($this->deprecatedOptions, $check54);
63 1
        }
64
65 1
        if ($config["check_5_6"] == true) {
66
            $check56 = [
67 1
                'iconv.input_encoding' => 'is a deprecated option since PHP 5.6.0. Use \'default_charset\' instead',
68 1
                'iconv.output_encoding' => 'is a deprecated option since PHP 5.6.0. Use \'default_charset\' instead',
69 1
                'iconv.internal_encoding' => 'is a deprecated option since PHP 5.6.0. Use \'default_charset\' instead',
70 1
                'mbstring.http_input' => 'is a deprecated option since PHP 5.6.0. Use \'default_charset\' instead',
71 1
                'mbstring.http_output' => 'is a deprecated option since PHP 5.6.0. Use \'default_charset\' instead',
72 1
                'mbstring.internal_encoding' => 'is a deprecated option since PHP 5.6.0. Use \'default_charset\' instead',
73 1
            ];
74 1
            $this->deprecatedOptions = array_merge($this->deprecatedOptions, $check56);
75 1
        }
76
        
77 1
        if ($config["check_7_0"] == true) {
78
            $check70 = [
79 1
                'asp_tags' => 'is a deprecated option since PHP 7.0.0',
80 1
                'always_populate_raw_post_data' => 'is a deprecated option since PHP 7.0.0',
81 1
            ];
82 1
            $this->deprecatedOptions = array_merge($this->deprecatedOptions, $check70);
83 1
        }
84 1
    }
85
86 4
    public function pass(FuncCall $funcCall, Context $context)
87
    {
88 4
        $functionName = $this->resolveFunctionName($funcCall, $context);
89 4
        if ($functionName && isset($this->functions[$functionName])) {
90 1
            if ($funcCall->args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $funcCall->args of type PhpParser\Node\Arg[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
91 1
                $compiledOptionName = $context->getExpressionCompiler()->compile($funcCall->args[0]);
92 1
                if ($compiledOptionName->isString() && $compiledOptionName->isCorrectValue()) {
93 1
                    if (isset($this->deprecatedOptions[$compiledOptionName->getValue()])) {
94 1
                        $context->notice(
95 1
                            'deprecated.option',
96 1
                            sprintf(
97 1
                                'Ini option %s %s.',
98 1
                                $compiledOptionName->getValue(),
99 1
                                $this->deprecatedOptions[$compiledOptionName->getValue()]
100 1
                            ),
101
                            $funcCall
102 1
                        );
103 1
                    }
104 1
                }
105 1
            }
106 1
        }
107 4
    }
108
109
    /**
110
     * @return Metadata
111
     */
112 43
    public static function getMetadata()
113
    {
114 43
        $treebuilder = new TreeBuilder();
115 43
        $config = $treebuilder->root("deprecated_ini_options")
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method canBeDisabled() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
116 43
            ->info(self::DESCRIPTION)
117 43
            ->canBeDisabled()
118 43
            ->children()
119 43
                ->booleanNode("check_5_3")->defaultTrue()->end()
120 43
                ->booleanNode("check_5_4")->defaultTrue()->end()
121 43
                ->booleanNode("check_5_6")->defaultTrue()->end()
122 43
                ->booleanNode("check_7_0")->defaultTrue()->end()
123 43
            ->end();
124
125 43
        return new Metadata("deprecated_ini_options", $config, self::DESCRIPTION);
126
    }
127
}
128