| 1 | package Claude::Agent::Code::Review::Filter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | sub filter { | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 12 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | my $issues = $args{issues} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"issues"} // []
MC/DC0%0/1
$args{"issues"} // []
$args{"issues"}
Truth table50%1/2
A
A= $args{"issues"}
| |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | my $filters = $args{filters} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"filters"} // []
MC/DC100%1/1
$args{"filters"} // []
$args{"filters"}
Truth table100%2/2
A
A= $args{"filters"}
| |||||||||||||||||||||||||||||
| 14 | 12 | my @filtered; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 12 | for my $issue (@$issues) { | |||||||||||||||||||||||||||
| 17 | 13 | ▶ | next if $class->_is_false_positive($issue, $filters); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 3 | push @filtered, $issue; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 12 | return @filtered; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 13 | ▶ | sub _is_false_positive { | ||||||||||||||||||||||||||
|
Subroutine
_is_false_positive: called CC=46 SCAR=45.3
| |||||||||||||||||||||||||||||
| 25 | 13 | my ($class, $issue, $custom_filters) = @_; | |||||||||||||||||||||||||||
| 26 | 13 | ▶ | $custom_filters //= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$custom_filters //= []
MC/DC0%0/1
$custom_filters //= []
$custom_filters
Truth table50%1/2
A
A= $custom_filters
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 13 | my $file = $issue->file; | |||||||||||||||||||||||||||
| 29 | 13 | my $line = $issue->line; | |||||||||||||||||||||||||||
| 30 | 13 | ▶ | my $desc = $issue->description // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$issue->description // ""
MC/DC0%0/1
$issue->description // ""
$issue->description
Truth table50%1/2
A
A= $issue->description
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 13 | ▶ | return 0 unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 12 | my $file_path = path($file); | |||||||||||||||||||||||||||
| 35 | 12 | my $safe_path = eval { $file_path->realpath }; | |||||||||||||||||||||||||||
| 36 | 12 | ▶ | return 0 unless $safe_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 12 | my $base_dir = path('.')->realpath; | |||||||||||||||||||||||||||
| 39 | 12 | ▶ | return 0 unless $base_dir->subsumes($safe_path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 12 | ▶ | my $content = eval { $safe_path->slurp_utf8 } // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$safe_path->slurp_utf8
}
} // ""
MC/DC0%0/1
eval {
do {
$safe_path->slurp_utf8
}
} // ""
eval {
do {
$safe_path->slurp_utf8
}
}
Truth table50%1/2
A
A= eval {
do {
$safe_path->slurp_utf8
}
}
| |||||||||||||||||||||||||||||
| 42 | 12 | my @lines = split /\n/, $content; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 12 | my $line_idx = $line - 1; | |||||||||||||||||||||||||||
| 45 | 12 | ▶ | return 0 if $line_idx < 0 || $line_idx >= scalar(@lines); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line_idx < 0 or $line_idx >= scalar @lines
MC/DC50%1/2
$line_idx < 0 or $line_idx >= scalar @lines
$line_idx < 0
$line_idx >= scalar @lines
Truth table66%2/3
A or B
A= $line_idx < 0B= $line_idx >= scalar @lines
| |||||||||||||||||||||||||||||
| 46 | 11 | my $start = $line_idx - 5; | |||||||||||||||||||||||||||
| 47 | 11 | ▶ | $start = 0 if $start < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 11 | my $end = $line_idx + 5; | |||||||||||||||||||||||||||
| 49 | 11 | ▶ | $end = $#lines if $end > $#lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 11 | my $context = join("\n", @lines[$start..$end]); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 11 | for my $filter (@$custom_filters) { | |||||||||||||||||||||||||||
| 54 | 2 | ▶ | return 1 if $filter->($issue, $context); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | if ($desc =~ /heredoc|alarm|Windows|MSWin32|ReDoS|cross-platform/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | my $wide_start = $line_idx - 15; | |||||||||||||||||||||||||||
| 59 | 1 | ▶ | $wide_start = 0 if $wide_start < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | my $wide_end = $line_idx + 10; | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | $wide_end = $#lines if $wide_end > $#lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | my $wide_context = join("\n", @lines[$wide_start..$wide_end]); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | if ($wide_context =~ /Note:|TODO:|FIXME:|limitation|basic|doesn't handle|Windows:|MSWin32|fallback|rely on/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | return 1; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | ▶ | if ($desc =~ /unused.*import|import.*unused/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | if ($desc =~ /['"](\w+)['"]|\b(\w+)\s+is\s+imported/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | my $symbol = $1 // $2; | ||||||||||||||||||||||||||
|
Condition33%1/3
$1 // $2
MC/DC0%0/2
$1 // $2
$1
$2
Truth table33%1/3
A // B
A= $1B= $2
| |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | if ($symbol && $content =~ /\b\Q$symbol\E\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$symbol and $content =~ /\b\Q$symbol\E\b/u
MC/DC0%0/2
$symbol and $content =~ /\b\Q$symbol\E\b/u
$symbol
$content =~ /\b\Q$symbol\E\b/u
Truth table33%1/3
A and B
A= $symbolB= $content =~ /\b\Q$symbol\E\b/u
| |||||||||||||||||||||||||||||
| 73 | 1 | return 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | if ($desc =~ /no critic.*obsolete|directive.*unnecessary/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | if ($context =~ /##\s*no\s*critic/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | return 1; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 7 | ▶ | if ($desc =~ /does.*not.*end.*with.*1|RequireEndWithOne/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if ($content =~ /\n1;\s*\n?\s*$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | return 1; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | if ($desc =~ /path.*validation|path.*traversal/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if ($context =~ /realpath|subsumes|base_dir/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 1 | return 1; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 5 | ▶ | if ($desc =~ /missing.*error|no.*error.*handling/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if ($context =~ /eval\s*\{|or\s+die|\/\/|warn\s+/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | return 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | if ($desc =~ /silent.*fail|silently/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | if ($context =~ /warn\s+["']|warn\s+\$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 1 | return 1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | if ($desc =~ /validation.*(?:regex|pattern|string)|(?:regex|pattern).*validation/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($context =~ /die\s+["']Invalid|Must be/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | return 1; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | if ($desc =~ /off-by-one|indexing|1-based|0-based/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($context =~ /\$start\s*=\s*0\s+if|\$end\s*=.*if|< 0|> \$#/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | return 1; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | if ($desc =~ /inconsistent.*(?:pattern|style|handling)|(?:pattern|style).*inconsistent/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | return 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | ▶ | if ($desc =~ /TOCTOU|time.of.check|race.condition.*file/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | if ($context =~ /Note:|TOCTOU|handles.*gracefully|handles.*missing/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1 | return 1; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | if ($desc =~ /false.positive.*(?:filter|broad|aggressive)|(?:filter|filtering).*(?:broad|aggressive|too)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | return 1; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | if ($desc =~ /(?:comment|documentation).*(?:unnecessary|redundant|obvious)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | return 1; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | if ($desc =~ /no.critic.*(?:directive|issue|apply|not.*needed)|directive.*no.critic/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | return 1; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | if ($desc =~ /position.*(?:fragile|incorrect)|(?:fragile|incorrect).*position|length\(\).*position/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($context =~ /length\(\$\w+\)\s*<\s*length\(\$\w+\)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | return 1; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | ▶ | if ($desc =~ /regex.*(?:edge|corner|doesn't capture|doesn't handle)|(?:edge|corner).*case/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ($context =~ /\# .*(?:handle|capture|parse)|TODO|basic/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | return 1; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | return 0; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | ▶ | sub count_filtered { | ||||||||||||||||||||||||||
|
Subroutine
count_filtered: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | ▶ | my $original = scalar @{$args{original} // []}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"original"} // []
MC/DC0%0/1
$args{"original"} // []
$args{"original"}
Truth table50%1/2
A
A= $args{"original"}
| |||||||||||||||||||||||||||||
| 161 | 1 | ▶ | my $filtered = scalar @{$args{filtered} // []}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"filtered"} // []
MC/DC0%0/1
$args{"filtered"} // []
$args{"filtered"}
Truth table50%1/2
A
A= $args{"filtered"}
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | return ($filtered, $original - $filtered); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | 1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||