1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add check for strpos dictionaries

Ref #4070
This commit is contained in:
Brown 2020-09-13 21:42:44 -04:00 committed by Daniil Gentili
parent db55f608be
commit 0b66e8897c
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 35 additions and 11 deletions

View File

@ -1792,7 +1792,7 @@ class AssertionFinder
if ($var_type->from_docblock || $other_type->from_docblock) {
if (IssueBuffer::accepts(
new DocblockTypeContradiction(
$var_type . ' can never contain ' . $other_type,
$var_type . ' can never contain ' . $other_type->getId(),
new CodeLocation($source, $conditional),
$var_type . ' ' . $other_type
),

View File

@ -126,16 +126,34 @@ class ArgumentAnalyzer
&& !$arg->value instanceof PhpParser\Node\Scalar\MagicConst
&& !$arg->value instanceof PhpParser\Node\Expr\ConstFetch
) {
if (IssueBuffer::accepts(
new InvalidLiteralArgument(
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
. ' expects a non-literal value, ' . $arg_value_type->getId() . ' provided',
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
$values = \preg_split('//u', $arg_value_type->getSingleStringLiteral()->value, null, \PREG_SPLIT_NO_EMPTY);
$prev_ord = 0;
$gt_count = 0;
foreach ($values as $value) {
$ord = \mb_ord($value);
if ($ord > $prev_ord) {
$gt_count++;
}
$prev_ord = $ord;
}
if (count($values) < 12 || ($gt_count / count($values)) < 0.8) {
if (IssueBuffer::accepts(
new InvalidLiteralArgument(
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
. ' expects a non-literal value, ' . $arg_value_type->getId() . ' provided',
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}

View File

@ -1341,6 +1341,12 @@ class FunctionCallTest extends TestCase
return $matches[0];
}'
],
'strposAllowDictionary' => [
'<?php
function sayHello(string $format): void {
if (strpos("abcdefghijklmno", $format)) {}
}',
],
];
}