Skip to content

Commit

Permalink
Merge pull request #7188 from vimeo/muglug-use-invalidscalarargument-…
Browse files Browse the repository at this point in the history
…less
  • Loading branch information
weirdan committed Jan 3, 2022
2 parents 966b6ae + 1e115da commit ab60d31
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 25 deletions.
6 changes: 5 additions & 1 deletion docs/running_psalm/issues/InvalidScalarArgument.md
@@ -1,6 +1,10 @@
# InvalidScalarArgument

Emitted when a scalar value is passed to a method that expected another scalar type
Emitted when a scalar value is passed to a method that expected another scalar type.

This is only emitted in situations where Psalm can be sure that PHP tries to coerce one scalar type to another.

In all other cases `InvalidArgument` is emitted.

```php
<?php
Expand Down
Expand Up @@ -983,7 +983,7 @@ public static function verifyType(
),
$statements_analyzer->getSuppressedIssues()
);
} else {
} elseif ($cased_method_id !== 'echo' && $cased_method_id !== 'print') {
IssueBuffer::maybeAdd(
new ArgumentTypeCoercion(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
Expand Down
11 changes: 8 additions & 3 deletions src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Expand Up @@ -27,6 +27,7 @@
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyLowercaseString;
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString;
use Psalm\Type\Atomic\TNonEmptyScalar;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNonFalsyString;
use Psalm\Type\Atomic\TNonspecificLiteralInt;
Expand Down Expand Up @@ -297,7 +298,7 @@ public static function isContainedBy(
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_mixed = true;
$atomic_comparison_result->scalar_type_match_found = true;
$atomic_comparison_result->scalar_type_match_found = !$container_type_part->from_docblock;
}

return false;
Expand Down Expand Up @@ -619,7 +620,8 @@ public static function isContainedBy(
if ($input_type_part instanceof TNumeric) {
if ($container_type_part->isNumericType()) {
if ($atomic_comparison_result) {
$atomic_comparison_result->scalar_type_match_found = true;
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->scalar_type_match_found = !$container_type_part->from_docblock;
}
}
}
Expand All @@ -630,7 +632,10 @@ public static function isContainedBy(
&& !$container_type_part instanceof TLiteralFloat
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->scalar_type_match_found = true;
$atomic_comparison_result->type_coerced
= $atomic_comparison_result->type_coerced_from_scalar
= ($input_type_part instanceof TScalar || $input_type_part instanceof TNonEmptyScalar);
$atomic_comparison_result->scalar_type_match_found = !$container_type_part->from_docblock;
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Psalm/Internal/Type/Comparator/TypeComparisonResult.php
Expand Up @@ -7,7 +7,12 @@

class TypeComparisonResult
{
/** @var ?bool */
/**
* This is used to trigger `InvalidScalarArgument` in situations where we know PHP
* will try to coerce one scalar type to another.
*
* @var ?bool
*/
public $scalar_type_match_found;

/** @var ?bool */
Expand Down
6 changes: 3 additions & 3 deletions tests/AnnotationTest.php
Expand Up @@ -1702,7 +1702,7 @@ function foo(&...$s) : void {}
$a = 1;
foo($a);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'spreadOperatorByRefAnnotationBadCall2' => [
'<?php
Expand All @@ -1711,7 +1711,7 @@ function foo(&...$s) : void {}
$b = 2;
foo($b);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'spreadOperatorByRefAnnotationBadCall3' => [
'<?php
Expand All @@ -1720,7 +1720,7 @@ function foo(&...$s) : void {}
$c = 3;
foo($c);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'identifyReturnType' => [
'<?php
Expand Down
10 changes: 5 additions & 5 deletions tests/ArgTest.php
Expand Up @@ -160,9 +160,9 @@ function takesInt(int $i) : void {}
function foo($b) : void {}
foo(null);',
],
'allowArrayIntScalarForArrayStringWithScalarIgnored' => [
'allowArrayIntScalarForArrayStringWithArgumentTypeCoercionIgnored' => [
'<?php
/** @param array<int|string> $arr */
/** @param array<array-key> $arr */
function foo(array $arr) : void {
}
Expand All @@ -171,10 +171,10 @@ function bar() : array {
return [];
}
/** @psalm-suppress InvalidScalarArgument */
/** @psalm-suppress ArgumentTypeCoercion */
foo(bar());',
],
'allowArrayScalarForArrayStringWithScalarIgnored' => [
'allowArrayScalarForArrayStringWithArgumentTypeCoercionIgnored' => [
'<?php declare(strict_types=1);
/** @param array<string> $arr */
function foo(array $arr) : void {}
Expand All @@ -184,7 +184,7 @@ function bar() : array {
return [];
}
/** @psalm-suppress InvalidScalarArgument */
/** @psalm-suppress ArgumentTypeCoercion */
foo(bar());',
],
'unpackObjectlikeListArgs' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/ArrayAssignmentTest.php
Expand Up @@ -1887,7 +1887,7 @@ function takesArray(array $arr) : void {}
$a[] = 2;
takesArray($a);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'listUsedAsArrayWrongListType' => [
'<?php
Expand All @@ -1899,7 +1899,7 @@ function takesArray(array $arr) : void {}
$a[] = 2;
takesArray($a);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'nonEmptyAssignmentToListElementChangeType' => [
'<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/CallableTest.php
Expand Up @@ -1390,7 +1390,7 @@ function takesCallableReturningString(callable $c) : void {
function foo(string $c) : void {
takesCallableReturningString([$c, "bar"]);
}',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'inexistantCallableinCallableString' => [
'<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/DocblockInheritanceTest.php
Expand Up @@ -173,7 +173,7 @@ public function boo(array $arr) : void {}
}
(new X())->boo([1, 2]);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/FunctionCallTest.php
Expand Up @@ -1941,7 +1941,7 @@ function a($b): int
}
a(["a" => "hello"]);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'objectLikeKeyChecksAgainstDifferentTKeyedArray' => [
'<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/IncludeTest.php
Expand Up @@ -722,7 +722,7 @@ function fooFoo($bar): void {
'files_to_check' => [
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
],
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'namespacedRequireFunction' => [
'files' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/MagicMethodAnnotationTest.php
Expand Up @@ -813,7 +813,7 @@ class Child extends ParentClass {}
$child = new Child();
$child->setString("five");',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'unionAnnotationInvalidArg' => [
'<?php
Expand All @@ -829,7 +829,7 @@ class Child extends ParentClass {}
$child = new Child();
$b = $child->setBool("hello", 5);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'validAnnotationWithInvalidVariadicCall' => [
'<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/Template/ClassTemplateExtendsTest.php
Expand Up @@ -4626,7 +4626,7 @@ public function getID()
class AppUser extends User {}
$au = new AppUser("string");',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'extendsTwiceDifferentNameBrokenChain' => [
'<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/Template/ClassTemplateTest.php
Expand Up @@ -3822,7 +3822,7 @@ function takesIntCollection(Collection $c): void {}
takesStringCollection($collection);
takesIntCollection($collection);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'argumentExpectsFleshOutTIndexedAccess' => [
'<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/Template/FunctionTemplateTest.php
Expand Up @@ -1811,7 +1811,7 @@ function(callable $s) {
function takesReturnTCallable(callable $s) {}
takesReturnTCallable($b);',
'error_message' => 'InvalidScalarArgument',
'error_message' => 'InvalidArgument',
],
'multipleArgConstraintWithMoreRestrictiveFirstArg' => [
'<?php
Expand Down

0 comments on commit ab60d31

Please sign in to comment.