diff --git a/examples/plugins/FunctionCasingChecker.php b/examples/plugins/FunctionCasingChecker.php index e00d8d2227b..3d8eaa02fb8 100644 --- a/examples/plugins/FunctionCasingChecker.php +++ b/examples/plugins/FunctionCasingChecker.php @@ -1,10 +1,9 @@ cased_name !== (string)$expr->name) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new IncorrectFunctionCasing( 'Function is incorrectly cased, expecting ' . $function_storage->cased_name, new CodeLocation($statements_source, $expr->name) ), $statements_source->getSuppressedIssues() - )) { - // fall through - } + ); } } catch (Exception $e) { // can throw if storage is missing @@ -88,15 +85,13 @@ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $function_name_parts = explode('\\', $function_storage->cased_name); if (end($function_name_parts) !== end($expr->name->parts)) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new IncorrectFunctionCasing( 'Function is incorrectly cased, expecting ' . $function_storage->cased_name, new CodeLocation($statements_source, $expr->name) ), $statements_source->getSuppressedIssues() - )) { - // fall through - } + ); } } catch (Exception $e) { // can throw if storage is missing diff --git a/examples/plugins/PreventFloatAssignmentChecker.php b/examples/plugins/PreventFloatAssignmentChecker.php index b15ba82c262..412adb253af 100644 --- a/examples/plugins/PreventFloatAssignmentChecker.php +++ b/examples/plugins/PreventFloatAssignmentChecker.php @@ -1,8 +1,8 @@ getExpr(); @@ -26,15 +26,13 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve && ($expr_type = $statements_source->getNodeTypeProvider()->getType($expr->expr)) && $expr_type->hasFloat() ) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new NoFloatAssignment( 'Don’t assign to floats', new CodeLocation($statements_source, $expr) ), $statements_source->getSuppressedIssues() - )) { - // fall through - } + ); } return null; diff --git a/examples/plugins/StringChecker.php b/examples/plugins/StringChecker.php index 557b6bb7391..53cdf129949 100644 --- a/examples/plugins/StringChecker.php +++ b/examples/plugins/StringChecker.php @@ -30,17 +30,14 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve && preg_match($class_or_class_method, $expr->value) ) { $absolute_class = preg_split('/[:]/', $expr->value)[0]; - - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidClass( 'Use ::class constants when representing class names', new CodeLocation($statements_source, $expr), $absolute_class ), $statements_source->getSuppressedIssues() - )) { - // fall through - } + ); } } elseif ($expr instanceof PhpParser\Node\Expr\BinaryOp\Concat && $expr->left instanceof PhpParser\Node\Expr\ClassConstFetch diff --git a/examples/plugins/composer-based/echo-checker/EchoChecker.php b/examples/plugins/composer-based/echo-checker/EchoChecker.php index a7255e4fd4a..3a9ee1d1232 100644 --- a/examples/plugins/composer-based/echo-checker/EchoChecker.php +++ b/examples/plugins/composer-based/echo-checker/EchoChecker.php @@ -27,17 +27,14 @@ public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event $expr_type = $statements_source->getNodeTypeProvider()->getType($expr); if (!$expr_type || $expr_type->hasMixed()) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new ArgumentTypeCoercion( 'Echo requires an unescaped string, ' . $expr_type . ' provided', new CodeLocation($statements_source, $expr), 'echo' ), $statements_source->getSuppressedIssues() - )) { - // keep soldiering on - } - + ); continue; } @@ -47,16 +44,14 @@ public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event if ($type instanceof TString && !$type instanceof TLiteralString ) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new ArgumentTypeCoercion( 'Echo requires an unescaped string, ' . $expr_type . ' provided', new CodeLocation($statements_source, $expr), 'echo' ), $statements_source->getSuppressedIssues() - )) { - // keep soldiering on - } + ); } } } diff --git a/src/Psalm/Internal/Analyzer/ClassAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassAnalyzer.php index 1ea2962b09d..7f10f6c9ac3 100644 --- a/src/Psalm/Internal/Analyzer/ClassAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassAnalyzer.php @@ -447,13 +447,11 @@ public function analyze( } elseif ($stmt instanceof PhpParser\Node\Stmt\Property) { foreach ($stmt->props as $prop) { if ($storage->is_enum) { - if (IssueBuffer::accepts(new NoEnumProperties( + IssueBuffer::maybeAdd(new NoEnumProperties( 'Enums cannot have properties', new CodeLocation($this, $prop), $fq_class_name - ))) { - // fall through - } + )); continue; } if ($prop->default) { @@ -2421,48 +2419,44 @@ private function checkEnum(): void $seen_values = []; foreach ($storage->enum_cases as $case_storage) { if ($case_storage->value !== null && $storage->enum_type === null) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidEnumCaseValue( 'Case of a non-backed enum should not have a value', $case_storage->stmt_location, $storage->name ) - )) { - } + ); } elseif ($case_storage->value === null && $storage->enum_type !== null) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidEnumCaseValue( 'Case of a backed enum should have a value', $case_storage->stmt_location, $storage->name ) - )) { - } + ); } elseif ($case_storage->value !== null && $storage->enum_type !== null) { if ((is_int($case_storage->value) && $storage->enum_type === 'string') || (is_string($case_storage->value) && $storage->enum_type === 'int') ) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidEnumCaseValue( 'Enum case value type should be ' . $storage->enum_type, $case_storage->stmt_location, $storage->name ) - )) { - } + ); } } if ($case_storage->value !== null) { if (in_array($case_storage->value, $seen_values, true)) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new DuplicateEnumCaseValue( 'Enum case values should be unique', $case_storage->stmt_location, $storage->name ) - )) { - } + ); } else { $seen_values[] = $case_storage->value; } diff --git a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php index 9a86f1496b7..f3cc7a81b13 100644 --- a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php @@ -583,27 +583,27 @@ public static function checkPropertyVisibility( return $emit_issues ? null : true; case self::VISIBILITY_PRIVATE: - if ($emit_issues && IssueBuffer::accepts( - new InaccessibleProperty( - 'Cannot access private property ' . $property_id . ' from context ' . $context->self, - $code_location - ), - $suppressed_issues - )) { - // fall through + if ($emit_issues) { + IssueBuffer::maybeAdd( + new InaccessibleProperty( + 'Cannot access private property ' . $property_id . ' from context ' . $context->self, + $code_location + ), + $suppressed_issues + ); } return null; case self::VISIBILITY_PROTECTED: if (!$context->self) { - if ($emit_issues && IssueBuffer::accepts( - new InaccessibleProperty( - 'Cannot access protected property ' . $property_id, - $code_location - ), - $suppressed_issues - )) { - // fall through + if ($emit_issues) { + IssueBuffer::maybeAdd( + new InaccessibleProperty( + 'Cannot access protected property ' . $property_id, + $code_location + ), + $suppressed_issues + ); } return null; @@ -614,14 +614,14 @@ public static function checkPropertyVisibility( } if (!$codebase->classExtends($context->self, $appearing_property_class)) { - if ($emit_issues && IssueBuffer::accepts( - new InaccessibleProperty( - 'Cannot access protected property ' . $property_id . ' from context ' . $context->self, - $code_location - ), - $suppressed_issues - )) { - // fall through + if ($emit_issues) { + IssueBuffer::maybeAdd( + new InaccessibleProperty( + 'Cannot access protected property ' . $property_id . ' from context ' . $context->self, + $code_location + ), + $suppressed_issues + ); } return null; diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index 71404ec4912..4d0c29aee16 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -361,13 +361,12 @@ public function analyze( if ($storage->unused_docblock_params) { foreach ($storage->unused_docblock_params as $param_name => $param_location) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidDocblockParamName( 'Incorrect param name $' . $param_name . ' in docblock for ' . $cased_method_id, $param_location ) - )) { - } + ); } } @@ -388,7 +387,7 @@ public function analyze( if ($overridden_storage->allow_named_arg_calls) { IssueBuffer::maybeAdd(new MethodSignatureMismatch( 'Method ' . (string) $method_id . ' should accept named arguments ' - . ' as ' . (string) $overridden_method_id . ' does', + . ' as ' . (string) $overridden_method_id . ' does', $storage->location )); } diff --git a/src/Psalm/Internal/Analyzer/MethodAnalyzer.php b/src/Psalm/Internal/Analyzer/MethodAnalyzer.php index ae725773f81..8bfb54e4184 100644 --- a/src/Psalm/Internal/Analyzer/MethodAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/MethodAnalyzer.php @@ -99,13 +99,13 @@ public static function checkStatic( CodeLocation $code_location, array $suppressed_issues, ?bool &$is_dynamic_this_method = false - ): bool { + ): void { $codebase_methods = $codebase->methods; if ($method_id->fq_class_name === 'Closure' && $method_id->method_name === 'fromcallable' ) { - return true; + return; } $original_method_id = $method_id; @@ -114,7 +114,7 @@ public static function checkStatic( if (!$method_id) { if (InternalCallMapHandler::inCallMap((string) $original_method_id)) { - return true; + return; } throw new LogicException('Declaring method for ' . $original_method_id . ' should not be null'); @@ -134,7 +134,7 @@ public static function checkStatic( ), $suppressed_issues )) { - return false; + return; } } else { $is_dynamic_this_method = true; @@ -149,12 +149,10 @@ public static function checkStatic( ), $suppressed_issues )) { - return false; + return; } } } - - return true; } /** diff --git a/src/Psalm/Internal/Analyzer/MethodComparator.php b/src/Psalm/Internal/Analyzer/MethodComparator.php index 034273858ba..9349545e160 100644 --- a/src/Psalm/Internal/Analyzer/MethodComparator.php +++ b/src/Psalm/Internal/Analyzer/MethodComparator.php @@ -270,15 +270,15 @@ private static function checkForObviousMethodMismatches( ), $suppressed_issues + $implementer_classlike_storage->suppressed_issues ); - } elseif (IssueBuffer::accepts( - new TraitMethodSignatureMismatch( - 'Method ' . $cased_implementer_method_id . ' has different access level than ' + } else { + IssueBuffer::maybeAdd( + new TraitMethodSignatureMismatch( + 'Method ' . $cased_implementer_method_id . ' has different access level than ' . $cased_guide_method_id, - $code_location - ), - $suppressed_issues + $implementer_classlike_storage->suppressed_issues - )) { - // fall through + $code_location + ), + $suppressed_issues + $implementer_classlike_storage->suppressed_issues + ); } } @@ -799,35 +799,31 @@ private static function compareMethodDocblockParams( !$guide_classlike_storage->user_defined, !$guide_classlike_storage->user_defined )) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new MoreSpecificImplementedParamType( 'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id - . ' has the more specific type \'' . - $implementer_method_storage_param_type->getId() . '\', expecting \'' . - $guide_method_storage_param_type->getId() . '\' as defined by ' . - $cased_guide_method_id, + . ' has the more specific type \'' . + $implementer_method_storage_param_type->getId() . '\', expecting \'' . + $guide_method_storage_param_type->getId() . '\' as defined by ' . + $cased_guide_method_id, $implementer_method_storage->params[$i]->location ?: $code_location ), $suppressed_issues + $implementer_classlike_storage->suppressed_issues - )) { - // fall through - } + ); } else { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new ImplementedParamTypeMismatch( 'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id - . ' has wrong type \'' . - $implementer_method_storage_param_type->getId() . '\', expecting \'' . - $guide_method_storage_param_type->getId() . '\' as defined by ' . - $cased_guide_method_id, + . ' has wrong type \'' . + $implementer_method_storage_param_type->getId() . '\', expecting \'' . + $guide_method_storage_param_type->getId() . '\' as defined by ' . + $cased_guide_method_id, $implementer_method_storage->params[$i]->location ?: $code_location ), $suppressed_issues + $implementer_classlike_storage->suppressed_issues - )) { - // fall through - } + ); } } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php index f2fcb18d28b..8f92df08164 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php @@ -196,16 +196,16 @@ public static function analyze( && isset($project_analyzer->getIssuesToFix()['UnnecessaryVarAnnotation']) ) { FileManipulationBuffer::addVarAnnotationToRemove($type_location); - } elseif (IssueBuffer::accepts( - new UnnecessaryVarAnnotation( - 'The @var ' . $comment_type . ' annotation for ' + } else { + IssueBuffer::maybeAdd( + new UnnecessaryVarAnnotation( + 'The @var ' . $comment_type . ' annotation for ' . $var_comment->var_id . ' is unnecessary', - $type_location - ), - $statements_analyzer->getSuppressedIssues(), - true - )) { - // fall through + $type_location + ), + $statements_analyzer->getSuppressedIssues(), + true + ); } } @@ -401,15 +401,13 @@ public static function checkIteratorType( bool &$always_non_empty_array ): ?bool { if ($iterator_type->isNull()) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new NullIterator( 'Cannot iterate over null', new CodeLocation($statements_analyzer->getSource(), $expr) ), $statements_analyzer->getSuppressedIssues() - )) { - } - + ); return false; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php index 6ffd3aad0e5..50ce7bca8cd 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php @@ -200,7 +200,7 @@ public static function analyze( } if ($original_context->check_classes) { - if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( + ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( $statements_analyzer, $fq_catch_class, new CodeLocation($statements_analyzer->getSource(), $catch_type, $context->include_location), @@ -208,9 +208,7 @@ public static function analyze( $context->calling_method_id, $statements_analyzer->getSuppressedIssues(), new ClassLikeNameOptions(true) - ) === false) { - // fall through - } + ); } if (($codebase->classExists($fq_catch_class) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index a38327a271d..a5f830f3f9c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -2652,18 +2652,16 @@ private static function getGetclassInequalityAssertions( return $if_types ? [$if_types] : []; } - if ($var_type - && ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( + if (!$var_type + || ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( $source, $var_type, new CodeLocation($source, $whichclass_expr), null, null, $source->getSuppressedIssues() - ) === false + ) !== false ) { - // fall through - } else { if ($var_name && $var_type) { $if_types[$var_name] = [[new IsClassNotEqual($var_type)]]; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php index bd360c5c7c7..64fe24e2772 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php @@ -108,37 +108,31 @@ public static function updateArrayType( $child_stmts[] = $root_array_expr; $root_array_expr = $root_array_expr->var; - if (ExpressionAnalyzer::analyze( + ExpressionAnalyzer::analyze( $statements_analyzer, $root_array_expr, $context, true - ) === false) { - // fall through - } + ); $codebase = $statements_analyzer->getCodebase(); $root_type = $statements_analyzer->node_data->getType($root_array_expr) ?? Type::getMixed(); if ($root_type->hasMixed()) { - if (ExpressionAnalyzer::analyze( + ExpressionAnalyzer::analyze( $statements_analyzer, $stmt->var, $context, true - ) === false) { - // fall through - } + ); if ($stmt->dim) { - if (ExpressionAnalyzer::analyze( + ExpressionAnalyzer::analyze( $statements_analyzer, $stmt->dim, $context - ) === false) { - // fall through - } + ); } } @@ -272,16 +266,13 @@ public static function updateArrayType( || $root_array_expr instanceof PhpParser\Node\Expr\FuncCall ) { if ($root_type->hasArray()) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidArrayAssignment( 'Assigning to the output of a function has no effect', new CodeLocation($statements_analyzer->getSource(), $root_array_expr) ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // do nothing - } + ); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php index 8c3afc95672..e4752554384 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php @@ -92,7 +92,6 @@ class InstancePropertyAssignmentAnalyzer /** * @param PropertyFetch|PropertyProperty $stmt * @param bool $direct_assignment whether the variable is assigned explicitly - * @return false|null */ public static function analyze( StatementsAnalyzer $statements_analyzer, @@ -102,12 +101,12 @@ public static function analyze( Union $assignment_value_type, Context $context, bool $direct_assignment = true - ): ?bool { + ): void { $codebase = $statements_analyzer->getCodebase(); if ($stmt instanceof PropertyProperty) { if (!$context->self || !$stmt->default) { - return null; + return; } $property_id = $context->self . '::$' . $prop_name; @@ -160,11 +159,11 @@ public static function analyze( } if (!$assigned_properties) { - return null; + return; } if ($assignment_value_type->hasMixed()) { - return null; + return; } $invalid_assignment_value_types = []; @@ -289,7 +288,7 @@ public static function analyze( ), $statements_analyzer->getSuppressedIssues() )) { - return false; + return; } } @@ -311,7 +310,7 @@ public static function analyze( ), $statements_analyzer->getSuppressedIssues() )) { - return false; + return; } } } @@ -332,7 +331,7 @@ public static function analyze( ), $statements_analyzer->getSuppressedIssues() )) { - return false; + return; } } else { if (IssueBuffer::accepts( @@ -349,12 +348,10 @@ public static function analyze( ), $statements_analyzer->getSuppressedIssues() )) { - return false; + return; } } } - - return null; } public static function trackPropertyImpurity( @@ -432,16 +429,14 @@ public static function analyzeStatement( ExpressionAnalyzer::analyze($statements_analyzer, $prop->default, $context); if ($prop_default_type = $statements_analyzer->node_data->getType($prop->default)) { - if (self::analyze( + self::analyze( $statements_analyzer, $prop, $prop->name->name, $prop->default, $prop_default_type, $context - ) === false) { - // fall through - } + ); } } } @@ -698,14 +693,13 @@ private static function analyzeRegularAssignment( $context->assigned_var_ids[$var_id] = (int)$stmt->var->getAttribute('startFilePos'); if ($direct_assignment && isset($context->protected_var_ids[$var_id])) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new LoopInvalidation( 'Variable ' . $var_id . ' has already been assigned in a for/foreach loop', new CodeLocation($statements_analyzer->getSource(), $stmt->var) ), $statements_analyzer->getSuppressedIssues() - )) { - } + ); } } @@ -727,14 +721,13 @@ private static function analyzeRegularAssignment( ); } - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new MixedPropertyAssignment( $lhs_var_id . ' of type mixed cannot be assigned to', new CodeLocation($statements_analyzer->getSource(), $stmt->var) ), $statements_analyzer->getSuppressedIssues() - )) { - } + ); return []; } @@ -750,27 +743,25 @@ private static function analyzeRegularAssignment( } if ($lhs_type->isNull()) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new NullPropertyAssignment( $lhs_var_id . ' of type null cannot be assigned to', new CodeLocation($statements_analyzer->getSource(), $stmt->var) ), $statements_analyzer->getSuppressedIssues() - )) { - } + ); return []; } if ($lhs_type->isNullable() && !$lhs_type->ignore_nullable_issues) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new PossiblyNullPropertyAssignment( $lhs_var_id . ' with possibly null type \'' . $lhs_type . '\' cannot be assigned to', new CodeLocation($statements_analyzer->getSource(), $stmt->var) ), $statements_analyzer->getSuppressedIssues() - )) { - } + ); } $has_regular_setter = false; @@ -833,25 +824,23 @@ private static function analyzeRegularAssignment( $invalid_assignment_type = $invalid_assignment_types[0]; if (!$has_valid_assignment_type) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidPropertyAssignment( $lhs_var_id . ' with non-object type \'' . $invalid_assignment_type . '\' cannot treated as an object', new CodeLocation($statements_analyzer->getSource(), $stmt->var) ), $statements_analyzer->getSuppressedIssues() - )) { - } + ); } else { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new PossiblyInvalidPropertyAssignment( $lhs_var_id . ' with possible non-object type \'' . $invalid_assignment_type . '\' cannot treated as an object', new CodeLocation($statements_analyzer->getSource(), $stmt->var) ), $statements_analyzer->getSuppressedIssues() - )) { - } + ); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index e5c71802001..34f4525687d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -276,16 +276,16 @@ public static function analyze( && isset($statements_analyzer->getProjectAnalyzer()->getIssuesToFix()['UnnecessaryVarAnnotation']) ) { FileManipulationBuffer::addVarAnnotationToRemove($comment_type_location); - } elseif (IssueBuffer::accepts( - new UnnecessaryVarAnnotation( - 'The @var ' . $comment_type . ' annotation for ' + } else { + IssueBuffer::maybeAdd( + new UnnecessaryVarAnnotation( + 'The @var ' . $comment_type . ' annotation for ' . $extended_var_id . ' is unnecessary', - $comment_type_location - ), - $statements_analyzer->getSuppressedIssues(), - true - )) { - // fall through + $comment_type_location + ), + $statements_analyzer->getSuppressedIssues(), + true + ); } } @@ -775,16 +775,16 @@ public static function assignTypeFromVarDocblock( && isset($project_analyzer->getIssuesToFix()['UnnecessaryVarAnnotation']) ) { FileManipulationBuffer::addVarAnnotationToRemove($type_location); - } elseif (IssueBuffer::accepts( - new UnnecessaryVarAnnotation( - 'The @var ' . $var_comment_type . ' annotation for ' + } else { + IssueBuffer::maybeAdd( + new UnnecessaryVarAnnotation( + 'The @var ' . $var_comment_type . ' annotation for ' . $var_comment->var_id . ' is unnecessary', - $type_location - ), - $statements_analyzer->getSuppressedIssues(), - true - )) { - // fall through + $type_location + ), + $statements_analyzer->getSuppressedIssues(), + true + ); } } @@ -1306,29 +1306,25 @@ private static function analyzeDestructuringAssignment( if ($assign_value_type->hasArray()) { if ($assign_value_atomic_type instanceof TFalse && $assign_value_type->ignore_falsable_issues) { // do nothing - } elseif (IssueBuffer::accepts( - new PossiblyInvalidArrayAccess( - 'Cannot access array value on non-array variable ' - . $extended_var_id . ' of type ' . $assign_value_atomic_type->getId(), - new CodeLocation($statements_analyzer->getSource(), $var) - ), - $statements_analyzer->getSuppressedIssues() - ) - ) { - // do nothing + } else { + IssueBuffer::maybeAdd( + new PossiblyInvalidArrayAccess( + 'Cannot access array value on non-array variable ' + . $extended_var_id . ' of type ' . $assign_value_atomic_type->getId(), + new CodeLocation($statements_analyzer->getSource(), $var) + ), + $statements_analyzer->getSuppressedIssues() + ); } } else { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidArrayAccess( 'Cannot access array value on non-array variable ' . $extended_var_id . ' of type ' . $assign_value_atomic_type->getId(), new CodeLocation($statements_analyzer->getSource(), $var) ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // do nothing - } + ); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php index 043da46ac4c..11a1468af31 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php @@ -83,14 +83,14 @@ public static function analyze( if ($left_type && $right_type) { if ($left_type->isNull()) { - if ($statements_source && IssueBuffer::accepts( - new NullOperand( - 'Left operand cannot be null', - new CodeLocation($statements_source, $left) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new NullOperand( + 'Left operand cannot be null', + new CodeLocation($statements_source, $left) + ), + $statements_source->getSuppressedIssues() + ); } $result_type = Type::getMixed(); @@ -98,26 +98,26 @@ public static function analyze( } if ($left_type->isNullable() && !$left_type->ignore_nullable_issues) { - if ($statements_source && IssueBuffer::accepts( - new PossiblyNullOperand( - 'Left operand cannot be nullable, got ' . $left_type, - new CodeLocation($statements_source, $left) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new PossiblyNullOperand( + 'Left operand cannot be nullable, got ' . $left_type, + new CodeLocation($statements_source, $left) + ), + $statements_source->getSuppressedIssues() + ); } } if ($right_type->isNull()) { - if ($statements_source && IssueBuffer::accepts( - new NullOperand( - 'Right operand cannot be null', - new CodeLocation($statements_source, $right) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new NullOperand( + 'Right operand cannot be null', + new CodeLocation($statements_source, $right) + ), + $statements_source->getSuppressedIssues() + ); } $result_type = Type::getMixed(); @@ -125,66 +125,66 @@ public static function analyze( } if ($right_type->isNullable() && !$right_type->ignore_nullable_issues) { - if ($statements_source && IssueBuffer::accepts( - new PossiblyNullOperand( - 'Right operand cannot be nullable, got ' . $right_type, - new CodeLocation($statements_source, $right) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new PossiblyNullOperand( + 'Right operand cannot be nullable, got ' . $right_type, + new CodeLocation($statements_source, $right) + ), + $statements_source->getSuppressedIssues() + ); } } if ($left_type->isFalse()) { - if ($statements_source && IssueBuffer::accepts( - new FalseOperand( - 'Left operand cannot be false', - new CodeLocation($statements_source, $left) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new FalseOperand( + 'Left operand cannot be false', + new CodeLocation($statements_source, $left) + ), + $statements_source->getSuppressedIssues() + ); } return; } if ($left_type->isFalsable() && !$left_type->ignore_falsable_issues) { - if ($statements_source && IssueBuffer::accepts( - new PossiblyFalseOperand( - 'Left operand cannot be falsable, got ' . $left_type, - new CodeLocation($statements_source, $left) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new PossiblyFalseOperand( + 'Left operand cannot be falsable, got ' . $left_type, + new CodeLocation($statements_source, $left) + ), + $statements_source->getSuppressedIssues() + ); } } if ($right_type->isFalse()) { - if ($statements_source && IssueBuffer::accepts( - new FalseOperand( - 'Right operand cannot be false', - new CodeLocation($statements_source, $right) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new FalseOperand( + 'Right operand cannot be false', + new CodeLocation($statements_source, $right) + ), + $statements_source->getSuppressedIssues() + ); } return; } if ($right_type->isFalsable() && !$right_type->ignore_falsable_issues) { - if ($statements_source && IssueBuffer::accepts( - new PossiblyFalseOperand( - 'Right operand cannot be falsable, got ' . $right_type, - new CodeLocation($statements_source, $right) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new PossiblyFalseOperand( + 'Right operand cannot be falsable, got ' . $right_type, + new CodeLocation($statements_source, $right) + ), + $statements_source->getSuppressedIssues() + ); } } @@ -423,24 +423,24 @@ private static function analyzeOperands( } if ($left_type_part instanceof TMixed) { - if ($statements_source && IssueBuffer::accepts( - new MixedOperand( - 'Left operand cannot be mixed', - new CodeLocation($statements_source, $left) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new MixedOperand( + 'Left operand cannot be mixed', + new CodeLocation($statements_source, $left) + ), + $statements_source->getSuppressedIssues() + ); } } else { - if ($statements_source && IssueBuffer::accepts( - new MixedOperand( - 'Right operand cannot be mixed', - new CodeLocation($statements_source, $right) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new MixedOperand( + 'Right operand cannot be mixed', + new CodeLocation($statements_source, $right) + ), + $statements_source->getSuppressedIssues() + ); } } @@ -467,27 +467,27 @@ private static function analyzeOperands( && !$left_type_part->as->isInt() && !$left_type_part->as->isFloat() ) { - if ($statements_source && IssueBuffer::accepts( - new MixedOperand( - 'Left operand cannot be a non-numeric template', - new CodeLocation($statements_source, $left) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new MixedOperand( + 'Left operand cannot be a non-numeric template', + new CodeLocation($statements_source, $left) + ), + $statements_source->getSuppressedIssues() + ); } } elseif ($right_type_part instanceof TTemplateParam && !$right_type_part->as->isInt() && !$right_type_part->as->isFloat() ) { - if ($statements_source && IssueBuffer::accepts( - new MixedOperand( - 'Right operand cannot be a non-numeric template', - new CodeLocation($statements_source, $right) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new MixedOperand( + 'Right operand cannot be a non-numeric template', + new CodeLocation($statements_source, $right) + ), + $statements_source->getSuppressedIssues() + ); } } @@ -654,14 +654,14 @@ private static function analyzeOperands( $result_type ); } else { - if ($statements_source && IssueBuffer::accepts( - new InvalidOperand( - 'Cannot add GMP to non-numeric type', - new CodeLocation($statements_source, $parent) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new InvalidOperand( + 'Cannot add GMP to non-numeric type', + new CodeLocation($statements_source, $parent) + ), + $statements_source->getSuppressedIssues() + ); } } @@ -732,15 +732,15 @@ private static function analyzeOperands( && ($left_type_part->isNumericType() && $right_type_part->isNumericType()) ) { if ($config->strict_binary_operands) { - if ($statements_source && IssueBuffer::accepts( - new InvalidOperand( - 'Cannot process different numeric types together in strict binary operands mode, '. - 'please cast explicitly', - new CodeLocation($statements_source, $parent) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new InvalidOperand( + 'Cannot process different numeric types together in strict binary operands mode, '. + 'please cast explicitly', + new CodeLocation($statements_source, $parent) + ), + $statements_source->getSuppressedIssues() + ); } } @@ -859,15 +859,15 @@ private static function analyzeOperands( || ($left_type_part instanceof TInt && $right_type_part instanceof TFloat) ) { if ($config->strict_binary_operands) { - if ($statements_source && IssueBuffer::accepts( - new InvalidOperand( - 'Cannot process ints and floats in strict binary operands mode, '. - 'please cast explicitly', - new CodeLocation($statements_source, $parent) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new InvalidOperand( + 'Cannot process ints and floats in strict binary operands mode, '. + 'please cast explicitly', + new CodeLocation($statements_source, $parent) + ), + $statements_source->getSuppressedIssues() + ); } } @@ -885,15 +885,15 @@ private static function analyzeOperands( if ($left_type_part->isNumericType() && $right_type_part->isNumericType()) { if ($config->strict_binary_operands) { - if ($statements_source && IssueBuffer::accepts( - new InvalidOperand( - 'Cannot process numeric types together in strict operands mode, '. - 'please cast explicitly', - new CodeLocation($statements_source, $parent) - ), - $statements_source->getSuppressedIssues() - )) { - // fall through + if ($statements_source) { + IssueBuffer::maybeAdd( + new InvalidOperand( + 'Cannot process numeric types together in strict operands mode, '. + 'please cast explicitly', + new CodeLocation($statements_source, $parent) + ), + $statements_source->getSuppressedIssues() + ); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php index 3443219f0eb..aa4dc680713 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php @@ -948,16 +948,16 @@ private static function checkClosureTypeArgs( ), $statements_analyzer->getSuppressedIssues() ); - } elseif (IssueBuffer::accepts( - new InvalidArgument( - 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' . + } else { + IssueBuffer::maybeAdd( + new InvalidArgument( + 'Parameter ' . ($i + 1) . ' of closure passed to function ' . $method_id . ' expects ' . $closure_param_type->getId() . ', but ' . $input_type->getId() . ' provided', - new CodeLocation($statements_analyzer->getSource(), $closure_arg), - $method_id - ), - $statements_analyzer->getSuppressedIssues() - )) { - // fall through + new CodeLocation($statements_analyzer->getSource(), $closure_arg), + $method_id + ), + $statements_analyzer->getSuppressedIssues() + ); } } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php index 89e1ff1b2b7..f2b070e8708 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php @@ -502,15 +502,15 @@ private static function handleNamedFunction( $code_location, $is_maybe_root_function ) === false) { - if ($args && ArgumentsAnalyzer::analyze( - $statements_analyzer, - $args, - null, - null, - true, - $context - ) === false) { - // fall through + if ($args) { + ArgumentsAnalyzer::analyze( + $statements_analyzer, + $args, + null, + null, + true, + $context + ); } return $function_call_info; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php index ac1ff1b8cf6..0855adbd968 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php @@ -546,16 +546,15 @@ private static function getMagicGetterOrSetterProperty( // a `@property` annotation if (($class_storage->sealed_properties || $codebase->config->seal_all_properties) && !isset($class_storage->pseudo_property_set_types['$' . $prop_name]) - && IssueBuffer::accepts( + ) { + IssueBuffer::maybeAdd( new UndefinedThisPropertyAssignment( 'Instance property ' . $property_id . ' is not defined', new CodeLocation($statements_analyzer->getSource(), $stmt), $property_id ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // fall through + ); } // If a `@property` annotation is set, the type of the value passed to the @@ -645,16 +644,15 @@ private static function getMagicGetterOrSetterProperty( // a `@property` annotation if (($class_storage->sealed_properties || $codebase->config->seal_all_properties) && !isset($class_storage->pseudo_property_get_types['$' . $prop_name]) - && IssueBuffer::accepts( + ) { + IssueBuffer::maybeAdd( new UndefinedThisPropertyFetch( 'Instance property ' . $property_id . ' is not defined', new CodeLocation($statements_analyzer->getSource(), $stmt), $property_id ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // fall through + ); } if (isset($class_storage->pseudo_property_get_types['$' . $prop_name])) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallProhibitionAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallProhibitionAnalyzer.php index dd4a19fafbc..cd141e51ba1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallProhibitionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallProhibitionAnalyzer.php @@ -19,7 +19,6 @@ class MethodCallProhibitionAnalyzer { /** * @param string[] $suppressed_issues - * @return false|null */ public static function analyze( Codebase $codebase, @@ -28,13 +27,13 @@ public static function analyze( ?string $caller_identifier, CodeLocation $code_location, array $suppressed_issues - ): ?bool { + ): void { $codebase_methods = $codebase->methods; $method_id = $codebase_methods->getDeclaringMethodId($method_id); if ($method_id === null) { - return null; + return; } $storage = $codebase_methods->getStorage($method_id); @@ -67,6 +66,5 @@ public static function analyze( ); } } - return null; } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php index 45e9dfb811b..9bc4a6ccda1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php @@ -127,17 +127,13 @@ public static function analyze( && $stmt->name instanceof PhpParser\Node\Identifier && ($class_type->isNull() || $class_type->isVoid()) ) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new NullReference( 'Cannot call method ' . $stmt->name->name . ' on null value', new CodeLocation($statements_analyzer->getSource(), $stmt->name) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } if ($class_type diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php index e730978924b..60a436a1e5a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php @@ -211,15 +211,13 @@ public static function analyze( } if ($codebase->interfaceExists($fq_class_name)) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InterfaceInstantiation( 'Interface ' . $fq_class_name . ' cannot be instantiated', new CodeLocation($statements_analyzer->getSource(), $stmt->class) ), $statements_analyzer->getSuppressedIssues() - )) { - } - + ); return true; } } @@ -261,13 +259,11 @@ public static function analyze( ); if ($codebase->classlikes->enumExists($fq_class_name)) { - if (IssueBuffer::accepts(new UndefinedClass( + IssueBuffer::maybeAdd(new UndefinedClass( 'Enums cannot be instantiated', new CodeLocation($statements_analyzer, $stmt), $fq_class_name - ))) { - // fall through - } + )); } } } @@ -836,18 +832,16 @@ private static function analyzeConstructorExpression( } if ($lhs_type_part instanceof TString) { - if ($config->allow_string_standin_for_class - && !$lhs_type_part instanceof TNumericString + if (!$config->allow_string_standin_for_class + || $lhs_type_part instanceof TNumericString ) { - // do nothing - } elseif (IssueBuffer::accepts( - new InvalidStringClass( - 'String cannot be used as a class', - new CodeLocation($statements_analyzer->getSource(), $stmt) - ), - $statements_analyzer->getSuppressedIssues() - )) { - // fall through + IssueBuffer::maybeAdd( + new InvalidStringClass( + 'String cannot be used as a class', + new CodeLocation($statements_analyzer->getSource(), $stmt) + ), + $statements_analyzer->getSuppressedIssues() + ); } } elseif ($lhs_type_part instanceof TMixed || $lhs_type_part instanceof TObject @@ -870,15 +864,15 @@ private static function analyzeConstructorExpression( } elseif ($lhs_type_part instanceof TNamedObject) { $new_type = Type::combineUnionTypes($new_type, new Union([$lhs_type_part])); continue; - } elseif (IssueBuffer::accepts( - new UndefinedClass( - 'Type ' . $lhs_type_part . ' cannot be called as a class', - new CodeLocation($statements_analyzer->getSource(), $stmt), - (string)$lhs_type_part - ), - $statements_analyzer->getSuppressedIssues() - )) { - // fall through + } else { + IssueBuffer::maybeAdd( + new UndefinedClass( + 'Type ' . $lhs_type_part . ' cannot be called as a class', + new CodeLocation($statements_analyzer->getSource(), $stmt), + (string)$lhs_type_part + ), + $statements_analyzer->getSuppressedIssues() + ); } $new_type = Type::combineUnionTypes($new_type, Type::getObject()); diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php index bcbed3e9c4e..e4020067acd 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php @@ -67,17 +67,13 @@ public static function analyze( : null; if (!$class_storage || !$class_storage->parent_class) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new ParentNotFound( 'Cannot call method on parent as this class does not extend another', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } $fq_class_name = $class_storage->parent_class; @@ -95,17 +91,13 @@ public static function analyze( $fq_class_name = $context->self; } } else { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new NonStaticSelfCall( 'Cannot use ' . $stmt->class->parts[0] . ' outside class context', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } if ($context->isPhantomClass($fq_class_name)) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php index 75dd5bcb815..49c44759037 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php @@ -853,19 +853,17 @@ private static function handleNamedCall( || !$codebase->classExtends($context->self, $fq_class_name) ) ) { - if (MethodAnalyzer::checkStatic( + MethodAnalyzer::checkStatic( $method_id, ($stmt->class instanceof PhpParser\Node\Name && strtolower($stmt->class->parts[0]) === 'self') - || $context->self === $fq_class_name, + || $context->self === $fq_class_name, !$statements_analyzer->isStatic(), $codebase, new CodeLocation($statements_analyzer, $stmt), $statements_analyzer->getSuppressedIssues(), $is_dynamic_this_method - ) === false) { - // fall through - } + ); if ($is_dynamic_this_method) { return self::forwardCallToInstanceMethod( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/ExistingAtomicStaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/ExistingAtomicStaticCallAnalyzer.php index 613f2f98715..922b74f6759 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/ExistingAtomicStaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/ExistingAtomicStaticCallAnalyzer.php @@ -72,16 +72,14 @@ public static function analyze( $codebase = $statements_analyzer->getCodebase(); $config = $codebase->config; - if (MethodCallProhibitionAnalyzer::analyze( + MethodCallProhibitionAnalyzer::analyze( $codebase, $context, $method_id, $statements_analyzer->getFullyQualifiedFunctionMethodOrNamespaceName(), new CodeLocation($statements_analyzer->getSource(), $stmt), $statements_analyzer->getSuppressedIssues() - ) === false) { - // fall through - } + ); if ($class_storage->user_defined && $context->self diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php index c6b80ab5c9e..e6892fe58c0 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php @@ -943,8 +943,6 @@ private static function handleRedundantCast( } - if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) { - // fall through - } + IssueBuffer::maybeAdd($issue, $statements_analyzer->getSuppressedIssues()); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/ClassConstAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/ClassConstAnalyzer.php index 5e8db7bae83..e1150a1a772 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/ClassConstAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/ClassConstAnalyzer.php @@ -74,17 +74,13 @@ public static function analyzeFetch( if ($first_part_lc === 'self' || $first_part_lc === 'static') { if (!$context->self) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new NonStaticSelfCall( 'Cannot use ' . $first_part_lc . ' outside class context', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } $fq_class_name = $context->self; @@ -92,17 +88,13 @@ public static function analyzeFetch( $fq_class_name = $statements_analyzer->getParentFQCLN(); if ($fq_class_name === null) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new ParentNotFound( 'Cannot check property fetch on parent as this class does not extend another', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } } else { $fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 5f32e104d9e..44729ac3afc 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -714,29 +714,23 @@ public static function getArrayAccessTypeGivenOffset( if ($non_array_types) { if ($has_array_access) { if ($in_assignment) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new PossiblyInvalidArrayAssignment( 'Cannot access array value on non-array variable ' . $extended_var_id . ' of type ' . $non_array_types[0], new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // do nothing - } + ); } elseif (!$context->inside_isset) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new PossiblyInvalidArrayAccess( 'Cannot access array value on non-array variable ' . $extended_var_id . ' of type ' . $non_array_types[0], new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // do nothing - } + ); } } else { if ($in_assignment) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php index d40eaa7bb7b..fce9809ae9d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php @@ -108,31 +108,23 @@ public static function analyze( } if ($stmt_var_type->isNull()) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new NullPropertyFetch( 'Cannot get property on null variable ' . $stmt_var_id, new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } if ($stmt_var_type->isNever()) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new MixedPropertyFetch( 'Cannot fetch property on empty var ' . $stmt_var_id, new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } if ($stmt_var_type->hasMixed()) { @@ -425,7 +417,7 @@ private static function handleScopedProperty( $class_storage, $in_assignment ); - + $context->vars_in_scope[$var_id] = $stmt_type; $statements_analyzer->node_data->setType($stmt, $stmt_type); diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php index c397918e8b8..d54b199ca56 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php @@ -58,17 +58,13 @@ public static function analyze( $fq_class_name = $statements_analyzer->getParentFQCLN(); if ($fq_class_name === null) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new ParentNotFound( 'Cannot check property fetch on parent as this class does not extend another', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } } else { $fq_class_name = (string)$context->self; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php index 2dfdab9e942..09a58a063a7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php @@ -76,17 +76,13 @@ public static function analyze( if ($stmt->name === 'this') { if ($statements_analyzer->isStatic()) { - if (IssueBuffer::accepts( + return !IssueBuffer::accepts( new InvalidScope( 'Invalid reference to $this in a static context', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - return false; - } - - return true; + ); } if (!isset($context->vars_in_scope['$this'])) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/MagicConstAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/MagicConstAnalyzer.php index 2fdd90b5c0f..b38db459356 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/MagicConstAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/MagicConstAnalyzer.php @@ -58,16 +58,14 @@ public static function analyze( } } elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Namespace_) { $namespace = $statements_analyzer->getNamespace(); - if ($namespace === null - && IssueBuffer::accepts( + if ($namespace === null) { + IssueBuffer::maybeAdd( new UndefinedConstant( 'Cannot get __namespace__ outside a namespace', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - ) - ) { - // fall through + ); } $statements_analyzer->node_data->setType($stmt, Type::getString($namespace)); diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/YieldAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/YieldAnalyzer.php index a9710c3b42f..406350f1411 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/YieldAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/YieldAnalyzer.php @@ -101,15 +101,15 @@ public static function analyze( && isset($project_analyzer->getIssuesToFix()['UnnecessaryVarAnnotation']) ) { FileManipulationBuffer::addVarAnnotationToRemove($type_location); - } elseif (IssueBuffer::accepts( - new UnnecessaryVarAnnotation( - 'The @var annotation for ' . $var_comment->var_id . ' is unnecessary', - $type_location - ), - $statements_analyzer->getSuppressedIssues(), - true - )) { - // fall through + } else { + IssueBuffer::maybeAdd( + new UnnecessaryVarAnnotation( + 'The @var annotation for ' . $var_comment->var_id . ' is unnecessary', + $type_location + ), + $statements_analyzer->getSuppressedIssues(), + true + ); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php index 994759611db..479e7866306 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php @@ -507,16 +507,14 @@ private static function handleExpression( return true; } - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new UnrecognizedExpression( 'Psalm does not understand ' . get_class($stmt) . ' for PHP ' . $codebase->getMajorAnalysisPhpVersion() . '.' . $codebase->getMinorAnalysisPhpVersion(), new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() - )) { - // fall through - } + ); return false; } diff --git a/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php index 348ee488ceb..0d6f6f06c1f 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php @@ -373,16 +373,13 @@ public static function analyze( } if ($local_return_type->isVoid()) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new InvalidReturnStatement( 'No return values are expected for ' . $cased_method_id, new CodeLocation($source, $stmt->expr) ), $statements_analyzer->getSuppressedIssues() - )) { - return; - } - + ); return; } diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index d6a9b6089b2..e20a31be528 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -498,17 +498,14 @@ private static function analyzeStatement( && !($stmt instanceof PhpParser\Node\Stmt\HaltCompiler) ) { if ($codebase->find_unused_variables) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new UnevaluatedCode( 'Expressions after return/throw/continue', new CodeLocation($statements_analyzer->source, $stmt) ), $statements_analyzer->source->getSuppressedIssues() - )) { - return null; - } + ); } - return null; } diff --git a/src/Psalm/Internal/Codebase/ClassLikes.php b/src/Psalm/Internal/Codebase/ClassLikes.php index f9069e1de02..3bd1441471b 100644 --- a/src/Psalm/Internal/Codebase/ClassLikes.php +++ b/src/Psalm/Internal/Codebase/ClassLikes.php @@ -1811,14 +1811,14 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth true ); } - } elseif (IssueBuffer::accepts( - $issue, - $method_storage->suppressed_issues, - $method_storage->stmt_location - && !$declaring_classlike_storage->is_trait - && !$has_variable_calls - )) { - // fall through + } else { + IssueBuffer::maybeAdd( + $issue, + $method_storage->suppressed_issues, + $method_storage->stmt_location + && !$declaring_classlike_storage->is_trait + && !$has_variable_calls + ); } } } elseif (!isset($classlike_storage->declaring_method_ids['__call'])) { @@ -1857,14 +1857,14 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth true ); } - } elseif (IssueBuffer::accepts( - $issue, - $method_storage->suppressed_issues, - $method_storage->stmt_location + } else { + IssueBuffer::maybeAdd( + $issue, + $method_storage->suppressed_issues, + $method_storage->stmt_location && !$declaring_classlike_storage->is_trait && !$has_variable_calls - )) { - // fall through + ); } } } @@ -2138,11 +2138,11 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v true ); } - } elseif (IssueBuffer::accepts( - $issue, - $classlike_storage->suppressed_issues + $property_storage->suppressed_issues - )) { - // fall through + } else { + IssueBuffer::maybeAdd( + $issue, + $classlike_storage->suppressed_issues + $property_storage->suppressed_issues + ); } } } elseif (!isset($classlike_storage->declaring_method_ids['__get'])) { @@ -2169,11 +2169,11 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v true ); } - } elseif (IssueBuffer::accepts( - $issue, - $classlike_storage->suppressed_issues + $property_storage->suppressed_issues - )) { - // fall through + } else { + IssueBuffer::maybeAdd( + $issue, + $classlike_storage->suppressed_issues + $property_storage->suppressed_issues + ); } } } diff --git a/src/Psalm/Internal/Codebase/Populator.php b/src/Psalm/Internal/Codebase/Populator.php index 64f322f8f43..3517da2cbcc 100644 --- a/src/Psalm/Internal/Codebase/Populator.php +++ b/src/Psalm/Internal/Codebase/Populator.php @@ -110,13 +110,13 @@ private function populateClassLikeStorage(ClassLikeStorage $storage, array $depe $fq_classlike_name_lc = strtolower($storage->name); if (isset($dependent_classlikes[$fq_classlike_name_lc])) { - if ($storage->location && IssueBuffer::accepts( - new CircularReference( - 'Circular reference discovered when loading ' . $storage->name, - $storage->location - ) - )) { - // fall through + if ($storage->location) { + IssueBuffer::maybeAdd( + new CircularReference( + 'Circular reference discovered when loading ' . $storage->name, + $storage->location + ) + ); } return; diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php index c2846292a86..293ea52234c 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php @@ -175,16 +175,15 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool || $duplicate_storage->stmt_location->file_path !== $this->file_path || $class_location->getHash() !== $duplicate_storage->stmt_location->getHash() ) { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new DuplicateClass( 'Class ' . $fq_classlike_name . ' has already been defined' - . ($duplicate_storage->location - ? ' in ' . $duplicate_storage->location->file_path - : ''), + . ($duplicate_storage->location + ? ' in ' . $duplicate_storage->location->file_path + : ''), $name_location ) - )) { - } + ); $this->file_storage->has_visitor_issues = true; @@ -1218,13 +1217,11 @@ private function visitClassConstDeclaration( if (isset($storage->constants[$const->name->name]) || isset($storage->enum_cases[$const->name->name]) ) { - if (IssueBuffer::accepts(new DuplicateConstant( + IssueBuffer::maybeAdd(new DuplicateConstant( 'Constant names should be unique', new CodeLocation($this->file_scanner, $const), $fq_classlike_name - ))) { - // fall through - } + )); continue; } @@ -1333,13 +1330,11 @@ private function visitEnumDeclaration( string $fq_classlike_name ): void { if (isset($storage->constants[$stmt->name->name])) { - if (IssueBuffer::accepts(new DuplicateConstant( + IssueBuffer::maybeAdd(new DuplicateConstant( 'Constant names should be unique', new CodeLocation($this->file_scanner, $stmt), $fq_classlike_name - ))) { - // fall through - } + )); return; } @@ -1412,14 +1407,13 @@ private function visitEnumDeclaration( } $storage->enum_cases[$stmt->name->name] = $case; } else { - if (IssueBuffer::accepts( + IssueBuffer::maybeAdd( new DuplicateEnumCase( 'Enum case names should be unique', $case_location, $fq_classlike_name ) - )) { - } + ); } } diff --git a/src/Psalm/IssueBuffer.php b/src/Psalm/IssueBuffer.php index 0a86aaead14..a3df2e8f19d 100644 --- a/src/Psalm/IssueBuffer.php +++ b/src/Psalm/IssueBuffer.php @@ -146,11 +146,7 @@ public static function accepts(CodeIssue $e, array $suppressed_issues = [], bool */ public static function maybeAdd(CodeIssue $e, array $suppressed_issues = [], bool $is_fixable = false): void { - if (self::isSuppressed($e, $suppressed_issues)) { - return; - } - - self::add($e, $is_fixable); + self::accepts($e, $suppressed_issues, $is_fixable); } /** @@ -551,9 +547,7 @@ public static function finish( $codebase = $project_analyzer->getCodebase(); foreach ($codebase->config->config_issues as $issue) { - if (self::accepts($issue)) { - // fall through - } + self::maybeAdd($issue); } $error_count = 0;