/
PrintfParametersRule.php
146 lines (121 loc) · 3.53 KB
/
PrintfParametersRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function array_filter;
use function count;
use function in_array;
use function max;
use function sprintf;
use function strlen;
use function strtolower;
use const PREG_SET_ORDER;
/**
* @implements Rule<Node\Expr\FuncCall>
*/
class PrintfParametersRule implements Rule
{
public function __construct(private PhpVersion $phpVersion)
{
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!($node->name instanceof Node\Name)) {
return [];
}
$functionsArgumentPositions = [
'printf' => 0,
'sprintf' => 0,
'sscanf' => 1,
'fscanf' => 1,
];
$minimumNumberOfArguments = [
'printf' => 1,
'sprintf' => 1,
'sscanf' => 3,
'fscanf' => 3,
];
$name = strtolower((string) $node->name);
if (!isset($functionsArgumentPositions[$name])) {
return [];
}
$formatArgumentPosition = $functionsArgumentPositions[$name];
$args = $node->getArgs();
foreach ($args as $arg) {
if ($arg->unpack) {
return [];
}
}
$argsCount = count($args);
if ($argsCount < $minimumNumberOfArguments[$name]) {
return []; // caught by CallToFunctionParametersRule
}
$formatArgType = $scope->getType($args[$formatArgumentPosition]->value);
$placeHoldersCount = null;
foreach ($formatArgType->getConstantStrings() as $formatString) {
$format = $formatString->getValue();
$tempPlaceHoldersCount = $this->getPlaceholdersCount($name, $format);
if ($placeHoldersCount === null) {
$placeHoldersCount = $tempPlaceHoldersCount;
} elseif ($tempPlaceHoldersCount > $placeHoldersCount) {
$placeHoldersCount = $tempPlaceHoldersCount;
}
}
if ($placeHoldersCount === null) {
return [];
}
$argsCount -= $formatArgumentPosition;
if ($argsCount !== $placeHoldersCount + 1) {
return [
RuleErrorBuilder::message(sprintf(
sprintf(
'%s, %s.',
$placeHoldersCount === 1 ? 'Call to %s contains %d placeholder' : 'Call to %s contains %d placeholders',
$argsCount - 1 === 1 ? '%d value given' : '%d values given',
),
$name,
$placeHoldersCount,
$argsCount - 1,
))->build(),
];
}
return [];
}
private function getPlaceholdersCount(string $functionName, string $format): int
{
$specifiers = in_array($functionName, ['sprintf', 'printf'], true) ? '[bcdeEfFgGosuxX%s]' : '(?:[cdDeEfinosuxX%s]|\[[^\]]+\])';
$addSpecifier = '';
if ($this->phpVersion->supportsHhPrintfSpecifier()) {
$addSpecifier .= 'hH';
}
$specifiers = sprintf($specifiers, $addSpecifier);
$pattern = '~(?<before>%*)%(?:(?<position>\d+)\$)?[-+]?(?:[ 0]|(?:\'[^%]))?-?\d*(?:\.\d*)?' . $specifiers . '~';
$matches = Strings::matchAll($format, $pattern, PREG_SET_ORDER);
if (count($matches) === 0) {
return 0;
}
$placeholders = array_filter($matches, static fn (array $match): bool => strlen($match['before']) % 2 === 0);
if (count($placeholders) === 0) {
return 0;
}
$maxPositionedNumber = 0;
$maxOrdinaryNumber = 0;
foreach ($placeholders as $placeholder) {
if (isset($placeholder['position']) && $placeholder['position'] !== '') {
$maxPositionedNumber = max((int) $placeholder['position'], $maxPositionedNumber);
} else {
$maxOrdinaryNumber++;
}
}
return max($maxPositionedNumber, $maxOrdinaryNumber);
}
}