1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall; |
4
|
|
|
|
5
|
|
|
use PHPSA\Context; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
use PhpParser\Node\Stmt; |
8
|
|
|
use PHPSA\Analyzer\Pass; |
9
|
|
|
use PhpParser\Node\Expr\Array_; |
10
|
|
|
use PhpParser\Node\Expr\FuncCall; |
11
|
|
|
use PhpParser\Node\Scalar\String_; |
12
|
|
|
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait; |
13
|
|
|
|
14
|
|
|
class FunctionStringFormater extends AbstractFunctionCallAnalyzer |
15
|
|
|
{ |
16
|
|
|
use DefaultMetadataPassTrait; |
17
|
|
|
|
18
|
|
|
const DESCRIPTION = 'Format string has same number of placeholders as parameters are passed into and forbid invalid type formats.'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array different sleep functions |
22
|
|
|
*/ |
23
|
|
|
protected $map = [ |
24
|
|
|
'printf' => 'printf', |
25
|
|
|
'sprintf' => 'sprintf' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param FuncCall $funcCall |
30
|
|
|
* @param Context $context |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
3 |
|
public function pass(FuncCall $funcCall, Context $context) |
34
|
|
|
{ |
35
|
3 |
|
$functionName = $this->resolveFunctionName($funcCall, $context); |
36
|
3 |
|
$args = $funcCall->args; |
37
|
|
|
|
38
|
3 |
|
if (! ($args[0]->value instanceof String_)) { |
39
|
2 |
|
$context->notice( |
40
|
2 |
|
'function_argument_invalid', |
41
|
2 |
|
sprintf('First parameter of %s must be string', $functionName), |
42
|
|
|
$funcCall |
43
|
2 |
|
); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
2 |
|
if (($args[0]->value instanceof String_)) { |
47
|
1 |
|
$string = $args[0]->value->value; |
|
|
|
|
48
|
|
|
// get invalid placeholders |
49
|
1 |
|
preg_match_all("/%[^bcdeEfFgGosuxX]/", $string, $placeholders); |
50
|
1 |
|
if (count($placeholders[0]) > 0) { |
51
|
1 |
|
$context->notice( |
52
|
1 |
|
'function_format_type_invalid', |
53
|
1 |
|
sprintf('Unexpected type format in %s function string', $functionName), |
54
|
|
|
$funcCall |
55
|
1 |
|
); |
56
|
1 |
|
} else { |
57
|
|
|
// get valid placesholders |
58
|
1 |
|
preg_match_all("/%[bcdeEfFgGosuxX]/", $string, $placeholders); |
59
|
1 |
|
if ($args[1]->value instanceof Array_) { |
60
|
1 |
|
if (count($placeholders[0]) !== count($args[1]->value->items)) { |
|
|
|
|
61
|
1 |
|
$context->notice( |
62
|
1 |
|
'function_array_length_invalid', |
63
|
1 |
|
sprintf('Unexpected length of array passed to %s', $functionName), |
64
|
|
|
$funcCall |
65
|
1 |
|
); |
66
|
1 |
|
} |
67
|
1 |
|
} else { |
68
|
1 |
|
if (count($placeholders[0]) !== (count($args) - 1)) { |
69
|
1 |
|
$context->notice( |
70
|
1 |
|
'function_arguments_length_invalid', |
71
|
1 |
|
sprintf('Unexpected length of arguments passed to %s', $functionName), |
72
|
|
|
$funcCall |
73
|
1 |
|
); |
74
|
1 |
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
2 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
1 |
|
public function getRegister() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
FuncCall::class |
89
|
1 |
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.