|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node\Expr\FuncCall; |
|
6
|
|
|
use PHPSA\Context; |
|
7
|
|
|
use PHPSA\Analyzer\Pass\Metadata; |
|
8
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
9
|
|
|
|
|
10
|
|
|
class DeprecatedFunctions extends AbstractFunctionCallAnalyzer |
|
11
|
|
|
{ |
|
12
|
|
|
const DESCRIPTION = 'Checks for use of deprecated functions and gives alternatives if available.'; |
|
13
|
|
|
|
|
14
|
|
|
protected $mysql = false; |
|
15
|
|
|
|
|
16
|
|
|
protected $mcrypt = false; |
|
17
|
|
|
|
|
18
|
|
|
protected $map = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param array $config The config values for the analyzer |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function __construct(array $config) |
|
24
|
|
|
{ |
|
25
|
1 |
|
if ($config["check_5_3"] == true) { |
|
26
|
|
|
$check53 = [ |
|
27
|
1 |
|
'define_syslog_variables' => ['5.3','_'], |
|
28
|
1 |
|
'set_magic_quotes_runtime' => ['5.3','_'], |
|
29
|
1 |
|
'set_socket_blocking' => ['5.3','_'], |
|
30
|
1 |
|
'ereg' => ['5.3','preg_match()'], |
|
31
|
1 |
|
'eregi' => ['5.3','preg_match()'], |
|
32
|
1 |
|
'ereg_replace' => ['5.3','preg_replace()'], |
|
33
|
1 |
|
'eregi_replace' => ['5.3','preg_replace()'], |
|
34
|
1 |
|
'split' => ['5.3','explode()'], |
|
35
|
1 |
|
'spliti' => ['5.3','preg_split()'], |
|
36
|
1 |
|
'sql_regcase' => ['5.3','preg_match()'], |
|
37
|
1 |
|
'session_is_registered' => ['5.3','$_SESSION'], |
|
38
|
1 |
|
'session_unregister' => ['5.3','$_SESSION'], |
|
39
|
1 |
|
'session_register' => ['5.3','$_SESSION'], |
|
40
|
1 |
|
]; |
|
41
|
1 |
|
$this->map = array_merge($this->map, $check53); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
if ($config["check_5_5"] == true) { |
|
45
|
|
|
$check55 = [ |
|
|
|
|
|
|
46
|
1 |
|
'datefmt_set_timezone_id' => ['5.5','IntlDateFormatter::setTimeZone()'], |
|
47
|
1 |
|
]; |
|
48
|
1 |
|
$this->map = array_merge($this->map, $check53); |
|
|
|
|
|
|
49
|
1 |
|
$this->mysql = true; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
if ($config["check_7_1"] == true) { |
|
53
|
1 |
|
$this->mcrypt = true; |
|
54
|
1 |
|
} |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
5 |
|
public function pass(FuncCall $funcCall, Context $context) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$functionName = $this->resolveFunctionName($funcCall, $context); |
|
60
|
5 |
|
if ($functionName) { |
|
61
|
5 |
|
if (isset($this->map[$functionName])) { |
|
62
|
1 |
|
$context->notice( |
|
63
|
1 |
|
'deprecated.function', |
|
64
|
1 |
|
sprintf('%s() is deprecated since PHP %s. Use %s instead.', $functionName, $this->map[$functionName][0], $this->map[$functionName][1]), |
|
65
|
|
|
$funcCall |
|
66
|
1 |
|
); |
|
67
|
5 |
|
} elseif (substr($functionName, 0, 6) === 'mysql_' && $this->mysql) { |
|
68
|
1 |
|
$context->notice( |
|
69
|
1 |
|
'deprecated.function', |
|
70
|
1 |
|
sprintf('The MySQL Extension is deprecated since PHP 5.5. Use PDO instead.'), |
|
71
|
|
|
$funcCall |
|
72
|
1 |
|
); |
|
73
|
5 |
|
} elseif (substr($functionName, 0, 7) === 'mcrypt_' && $this->mcrypt) { |
|
74
|
1 |
|
$context->notice( |
|
75
|
1 |
|
'deprecated.function', |
|
76
|
1 |
|
sprintf('The Mcrypt Extension is deprecated since PHP 7.1. Use paragonie/halite instead.'), |
|
77
|
|
|
$funcCall |
|
78
|
1 |
|
); |
|
79
|
1 |
|
} |
|
80
|
5 |
|
} |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Metadata |
|
85
|
|
|
*/ |
|
86
|
43 |
|
public static function getMetadata() |
|
87
|
|
|
{ |
|
88
|
43 |
|
$treebuilder = new TreeBuilder(); |
|
89
|
43 |
|
$config = $treebuilder->root("deprecated_functions") |
|
|
|
|
|
|
90
|
43 |
|
->info(self::DESCRIPTION) |
|
91
|
43 |
|
->canBeDisabled() |
|
92
|
43 |
|
->children() |
|
93
|
43 |
|
->booleanNode("check_5_3")->defaultTrue()->end() |
|
94
|
43 |
|
->booleanNode("check_5_5")->defaultTrue()->end() |
|
95
|
43 |
|
->booleanNode("check_7_1")->defaultTrue()->end() |
|
96
|
43 |
|
->end(); |
|
97
|
|
|
|
|
98
|
43 |
|
return new Metadata("deprecated_functions", $config, self::DESCRIPTION); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.