1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add complex issue error

This commit is contained in:
Matt Brown 2020-11-27 17:02:37 -05:00 committed by Daniil Gentili
parent 0b292a55c1
commit 3be31563d6
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 49 additions and 2 deletions

View File

@ -187,6 +187,8 @@
<xs:element name="ArgumentTypeCoercion" type="ArgumentIssueHandlerType" minOccurs="0" />
<xs:element name="AssignmentToVoid" type="IssueHandlerType" minOccurs="0" />
<xs:element name="CircularReference" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ComplexFunction" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ComplexMethod" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ConflictingReferenceConstraint" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ConstructorSignatureMismatch" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ContinueOutsideLoop" type="IssueHandlerType" minOccurs="0" />

View File

@ -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 functions 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 methods complexity is greater than the project limit'
. ' (method graph size = ' . $count .', average path length = ' . round($mean) . ')',
$function_storage->location
),
$this->getSuppressedIssues(),
true
)) {
// fall through
}
}
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Psalm\Issue;
class ComplexFunction extends CodeIssue
{
public const ERROR_LEVEL = -1;
public const SHORTCODE = 259;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Psalm\Issue;
class ComplexMethod extends CodeIssue
{
public const ERROR_LEVEL = -1;
public const SHORTCODE = 260;
}

View File

@ -132,6 +132,8 @@ class DocumentationTest extends TestCase
$code_blocks['PluginIssue'] = true;
$code_blocks['TaintedInput'] = true;
$code_blocks['TaintedCustom'] = true;
$code_blocks['ComplexFunction'] = true;
$code_blocks['ComplexMethod'] = true;
$documented_issues = array_keys($code_blocks);
sort($documented_issues);