From 3be31563d6d00e674e621dacccfe22461209d679 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Fri, 27 Nov 2020 17:02:37 -0500 Subject: [PATCH] Add complex issue error --- config.xsd | 2 ++ .../Internal/Analyzer/StatementsAnalyzer.php | 31 +++++++++++++++++-- src/Psalm/Issue/ComplexFunction.php | 8 +++++ src/Psalm/Issue/ComplexMethod.php | 8 +++++ tests/DocumentationTest.php | 2 ++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/Psalm/Issue/ComplexFunction.php create mode 100644 src/Psalm/Issue/ComplexMethod.php diff --git a/config.xsd b/config.xsd index a14b58675..4cfce7ac0 100644 --- a/config.xsd +++ b/config.xsd @@ -187,6 +187,8 @@ + + diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index 6ceffa0c2..8a7437c84 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -30,6 +30,8 @@ use Psalm\DocComment; use Psalm\Exception\DocblockParseException; use Psalm\FileManipulation; use Psalm\Internal\FileManipulation\FileManipulationBuffer; +use Psalm\Issue\ComplexFunction; +use Psalm\Issue\ComplexMethod; use Psalm\Issue\InvalidDocblock; use Psalm\Issue\MissingDocblockType; use Psalm\Issue\Trace; @@ -699,6 +701,8 @@ class StatementsAnalyzer extends SourceAnalyzer && $codebase->config->limit_method_complexity && $source instanceof FunctionLikeAnalyzer && !$source instanceof ClosureAnalyzer + && $function_storage + && $function_storage->location ) { [$count, $branching, $mean] = $this->data_flow_graph->getEdgeStats(); @@ -706,8 +710,31 @@ class StatementsAnalyzer extends SourceAnalyzer && $mean > $codebase->config->max_avg_path_length && $branching > 1.1 ) { - echo $source->getId() . ' ' . $count . ' ' . round($mean) . ' ' . $branching . "\n"; - // do something else + if ($source instanceof FunctionAnalyzer) { + if (IssueBuffer::accepts( + new ComplexFunction( + 'This function’s complexity is greater than the project limit' + . ' (method graph size = ' . $count .', average path length = ' . round($mean). ')', + $function_storage->location + ), + $this->getSuppressedIssues(), + true + )) { + // fall through + } + } elseif ($source instanceof MethodAnalyzer) { + if (IssueBuffer::accepts( + new ComplexMethod( + 'This method’s complexity is greater than the project limit' + . ' (method graph size = ' . $count .', average path length = ' . round($mean) . ')', + $function_storage->location + ), + $this->getSuppressedIssues(), + true + )) { + // fall through + } + } } } diff --git a/src/Psalm/Issue/ComplexFunction.php b/src/Psalm/Issue/ComplexFunction.php new file mode 100644 index 000000000..592971397 --- /dev/null +++ b/src/Psalm/Issue/ComplexFunction.php @@ -0,0 +1,8 @@ +