diff --git a/config.xsd b/config.xsd index 474d9af06..0f21145cc 100644 --- a/config.xsd +++ b/config.xsd @@ -369,6 +369,8 @@ + + diff --git a/docs/running_psalm/issues/RedundantCast.md b/docs/running_psalm/issues/RedundantCast.md new file mode 100644 index 000000000..81ef724b1 --- /dev/null +++ b/docs/running_psalm/issues/RedundantCast.md @@ -0,0 +1,10 @@ +# RedundantCast + +Emitted when a cast (string, int, float etc.) is redundant + +```php +collect_initializations && $lhs_var_id === '$this' ) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php index 95f693ced..caff33f5d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php @@ -377,10 +377,9 @@ class NamedFunctionCallHandler ) { if ($first_arg_type->from_docblock) { if (IssueBuffer::accepts( - new \Psalm\Issue\RedundantConditionGivenDocblockType( + new \Psalm\Issue\RedundantCastGivenDocblockType( 'The call to strtolower is unnecessary given the docblock type', - new CodeLocation($statements_analyzer, $function_name), - null + new CodeLocation($statements_analyzer, $function_name) ), $statements_analyzer->getSuppressedIssues() )) { @@ -388,10 +387,9 @@ class NamedFunctionCallHandler } } else { if (IssueBuffer::accepts( - new \Psalm\Issue\RedundantCondition( + new \Psalm\Issue\RedundantCast( 'The call to strtolower is unnecessary', - new CodeLocation($statements_analyzer, $function_name), - null + new CodeLocation($statements_analyzer, $function_name) ), $statements_analyzer->getSuppressedIssues() )) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php index 05fecd581..82450144c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php @@ -9,8 +9,8 @@ use Psalm\CodeLocation; use Psalm\Context; use Psalm\Issue\InvalidCast; use Psalm\Issue\PossiblyInvalidCast; -use Psalm\Issue\RedundantCondition; -use Psalm\Issue\RedundantConditionGivenDocblockType; +use Psalm\Issue\RedundantCast; +use Psalm\Issue\RedundantCastGivenDocblockType; use Psalm\Issue\UnrecognizedExpression; use Psalm\IssueBuffer; use Psalm\Type; @@ -49,16 +49,14 @@ class CastAnalyzer if ($maybe_type) { if ($maybe_type->isInt()) { if ($maybe_type->from_docblock) { - $issue = new RedundantConditionGivenDocblockType( + $issue = new RedundantCastGivenDocblockType( 'Redundant cast to ' . $maybe_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $maybe_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } else { - $issue = new RedundantCondition( + $issue = new RedundantCast( 'Redundant cast to ' . $maybe_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $maybe_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } @@ -103,16 +101,14 @@ class CastAnalyzer if ($maybe_type) { if ($maybe_type->isFloat()) { if ($maybe_type->from_docblock) { - $issue = new RedundantConditionGivenDocblockType( + $issue = new RedundantCastGivenDocblockType( 'Redundant cast to ' . $maybe_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $maybe_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } else { - $issue = new RedundantCondition( + $issue = new RedundantCast( 'Redundant cast to ' . $maybe_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $maybe_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } @@ -145,16 +141,14 @@ class CastAnalyzer if ($maybe_type) { if ($maybe_type->isBool()) { if ($maybe_type->from_docblock) { - $issue = new RedundantConditionGivenDocblockType( + $issue = new RedundantCastGivenDocblockType( 'Redundant cast to ' . $maybe_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $maybe_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } else { - $issue = new RedundantCondition( + $issue = new RedundantCast( 'Redundant cast to ' . $maybe_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $maybe_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } @@ -187,16 +181,14 @@ class CastAnalyzer if ($stmt_expr_type) { if ($stmt_expr_type->isString()) { if ($stmt_expr_type->from_docblock) { - $issue = new RedundantConditionGivenDocblockType( + $issue = new RedundantCastGivenDocblockType( 'Redundant cast to ' . $stmt_expr_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $stmt_expr_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } else { - $issue = new RedundantCondition( + $issue = new RedundantCast( 'Redundant cast to ' . $stmt_expr_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $stmt_expr_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } @@ -258,16 +250,14 @@ class CastAnalyzer if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) { if ($stmt_expr_type->isArray()) { if ($stmt_expr_type->from_docblock) { - $issue = new RedundantConditionGivenDocblockType( + $issue = new RedundantCastGivenDocblockType( 'Redundant cast to ' . $stmt_expr_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $stmt_expr_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } else { - $issue = new RedundantCondition( + $issue = new RedundantCast( 'Redundant cast to ' . $stmt_expr_type->getKey(), - new CodeLocation($statements_analyzer->getSource(), $stmt), - $stmt_expr_type->getKey() + new CodeLocation($statements_analyzer->getSource(), $stmt) ); } diff --git a/src/Psalm/Issue/RedundantCast.php b/src/Psalm/Issue/RedundantCast.php new file mode 100644 index 000000000..f924855e1 --- /dev/null +++ b/src/Psalm/Issue/RedundantCast.php @@ -0,0 +1,8 @@ + $bar */ - function baz(array $bar) : void { foo((array) $bar); }', - 'error_message' => 'RedundantCondition', + function baz(array $bar) : void { + foo((array) $bar); + }', + 'error_message' => 'RedundantCast', ], ]; } diff --git a/tests/PropertyTypeTest.php b/tests/PropertyTypeTest.php index 78b66cf79..719ec08e9 100644 --- a/tests/PropertyTypeTest.php +++ b/tests/PropertyTypeTest.php @@ -2169,6 +2169,16 @@ class PropertyTypeTest extends TestCase } }', ], + 'memoizePropertyAfterSetting' => [ + 'b = "c"; + echo strlen($this->b); + } + }' + ], ]; } diff --git a/tests/TypeReconciliation/RedundantConditionTest.php b/tests/TypeReconciliation/RedundantConditionTest.php index 2c3d14846..e62a6d161 100644 --- a/tests/TypeReconciliation/RedundantConditionTest.php +++ b/tests/TypeReconciliation/RedundantConditionTest.php @@ -69,16 +69,15 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase ' [], - 'error_levels' => [ - 'RedundantConditionGivenDocblockType', - ], ], 'noRedundantConditionAfterDocblockTypeNullCheck' => [ '