From 0d32203f9a94fa523bf58bb3192b20c7288aa3e3 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Wed, 3 Aug 2022 19:09:56 +0200 Subject: [PATCH 1/2] add ", but" for InvalidArgument error message where a type is provided --- .../Expression/Call/ArgumentAnalyzer.php | 14 +++++++------- .../Call/ArrayFunctionArgumentsAnalyzer.php | 12 +++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index 84b777c7e20..32d6b4c7e3e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -181,7 +181,7 @@ public static function checkArgumentMatches( IssueBuffer::maybeAdd( new InvalidLiteralArgument( 'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id - . ' expects a non-literal value, ' . $arg_value_type->getId() . ' provided', + . ' expects a non-literal value, but ' . $arg_value_type->getId() . ' provided', new CodeLocation($statements_analyzer->getSource(), $arg->value), $cased_method_id ), @@ -976,7 +976,7 @@ public static function verifyType( IssueBuffer::maybeAdd( new MixedArgumentTypeCoercion( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() . - ', parent type ' . $input_type->getId() . ' provided', + ', but parent type ' . $input_type->getId() . ' provided', $arg_location, $cased_method_id, $origin_location @@ -987,7 +987,7 @@ public static function verifyType( IssueBuffer::maybeAdd( new ArgumentTypeCoercion( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() . - ', parent type ' . $input_type->getId() . ' provided', + ', but parent type ' . $input_type->getId() . ' provided', $arg_location, $cased_method_id ), @@ -1000,7 +1000,7 @@ public static function verifyType( IssueBuffer::maybeAdd( new ImplicitToStringCast( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . - $param_type->getId() . ', ' . $input_type->getId() . ' provided with a __toString method', + $param_type->getId() . ', but ' . $input_type->getId() . ' provided with a __toString method', $arg_location ), $statements_analyzer->getSuppressedIssues() @@ -1022,7 +1022,7 @@ public static function verifyType( IssueBuffer::maybeAdd( new InvalidScalarArgument( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . - $param_type->getId() . ', ' . $type . ' provided', + $param_type->getId() . ', but ' . $type . ' provided', $arg_location, $cased_method_id ), @@ -1033,7 +1033,7 @@ public static function verifyType( IssueBuffer::maybeAdd( new PossiblyInvalidArgument( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() . - ', possibly different type ' . $type . ' provided', + ', but possibly different type ' . $type . ' provided', $arg_location, $cased_method_id ), @@ -1043,7 +1043,7 @@ public static function verifyType( IssueBuffer::maybeAdd( new InvalidArgument( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() . - ', ' . $type . ' provided', + ', but ' . $type . ' provided', $arg_location, $cased_method_id ), diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php index 654e9bd829b..05c5aa91951 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php @@ -893,7 +893,8 @@ private static function checkClosureTypeArgs( IssueBuffer::maybeAdd( new MixedArgumentTypeCoercion( 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' . - $closure_param_type->getId() . ', parent type ' . $input_type->getId() . ' provided', + $closure_param_type->getId() . + ', but parent type ' . $input_type->getId() . ' provided', new CodeLocation($statements_analyzer->getSource(), $closure_arg), $method_id ), @@ -903,7 +904,8 @@ private static function checkClosureTypeArgs( IssueBuffer::maybeAdd( new ArgumentTypeCoercion( 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' . - $closure_param_type->getId() . ', parent type ' . $input_type->getId() . ' provided', + $closure_param_type->getId() . + ', but parent type ' . $input_type->getId() . ' provided', new CodeLocation($statements_analyzer->getSource(), $closure_arg), $method_id ), @@ -923,7 +925,7 @@ private static function checkClosureTypeArgs( IssueBuffer::maybeAdd( new InvalidScalarArgument( 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' . - $closure_param_type->getId() . ', ' . $input_type->getId() . ' provided', + $closure_param_type->getId() . ', but ' . $input_type->getId() . ' provided', new CodeLocation($statements_analyzer->getSource(), $closure_arg), $method_id ), @@ -933,7 +935,7 @@ private static function checkClosureTypeArgs( IssueBuffer::maybeAdd( new PossiblyInvalidArgument( 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' - . $closure_param_type->getId() . ', possibly different type ' + . $closure_param_type->getId() . ', but possibly different type ' . $input_type->getId() . ' provided', new CodeLocation($statements_analyzer->getSource(), $closure_arg), $method_id @@ -943,7 +945,7 @@ private static function checkClosureTypeArgs( } elseif (IssueBuffer::accepts( new InvalidArgument( 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' . - $closure_param_type->getId() . ', ' . $input_type->getId() . ' provided', + $closure_param_type->getId() . ', but ' . $input_type->getId() . ' provided', new CodeLocation($statements_analyzer->getSource(), $closure_arg), $method_id ), From d2be169ce50938809ccd7203eb99c9d8d4f56690 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Wed, 3 Aug 2022 19:56:38 +0200 Subject: [PATCH 2/2] update tests --- tests/ClosureTest.php | 2 +- tests/ReturnTypeTest.php | 2 +- tests/Template/ClassTemplateTest.php | 4 ++-- tests/TypeReconciliation/TypeTest.php | 4 ++-- tests/UnusedVariableTest.php | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ClosureTest.php b/tests/ClosureTest.php index def99f9955c..cea843a3725 100644 --- a/tests/ClosureTest.php +++ b/tests/ClosureTest.php @@ -1149,7 +1149,7 @@ function takesB(B $_b) : void {} takesA($getAButReallyB()); takesB($getAButReallyB());', - 'error_message' => 'ArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:28 - Argument 1 of takesB expects B, parent type A provided', + 'error_message' => 'ArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:28 - Argument 1 of takesB expects B, but parent type A provided', ], 'closureByRefUseToMixed' => [ ' 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:54 - Argument 1 expects T:fn-map as mixed, int provided', + 'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:54 - Argument 1 expects T:fn-map as mixed, but int provided', ], 'cannotInferReturnClosureWithDifferentReturnTypes' => [ ' 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:20:34 - Argument 1 of type expects string, callable(State):(T:AlmostFooMap as mixed)&Foo provided', + 'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:20:34 - Argument 1 of type expects string, but callable(State):(T:AlmostFooMap as mixed)&Foo provided', ], 'templateWithNoReturn' => [ ' 5, "name" => "Mario", "height" => 3.5]); $mario->ame = "Luigi";', - 'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:47:29 - Argument 1 of CharacterRow::__set expects "height"|"id"|"name", "ame" provided', + 'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:47:29 - Argument 1 of CharacterRow::__set expects "height"|"id"|"name", but "ame" provided', ], 'specialiseTypeBeforeReturning' => [ ' 'ArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:14:32 - Argument 1 of takesB expects B,' - . ' parent type A&static provided', + . ' but parent type A&static provided', ], 'intersectionTypeInterfaceCheckAfterInstanceof' => [ ' 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:32 - Argument 1 of takesI expects I, A&static provided', + 'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:32 - Argument 1 of takesI expects I, but A&static provided', ], ]; } diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 048025186ca..618f871d651 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -3451,7 +3451,7 @@ function takesArray($a) : void { $arr = [$a]; takesArrayOfString($arr); }', - 'error_message' => 'MixedArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:44 - Argument 1 of takesArrayOfString expects array, parent type array{mixed} provided. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:10:41' + 'error_message' => 'MixedArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:44 - Argument 1 of takesArrayOfString expects array, but parent type array{mixed} provided. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:10:41' ], 'warnAboutUnusedVariableInTryReassignedInCatch' => [ '