Review/Tools.pm

filter: Statementstmt 92.7% 217 / 234 Branchbran 69.3% 68 / 98 Conditioncond 53.8% 21 / 39 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 94.7% 18 / 19 Podpod 100% 1 / 1 totaltotal 79.7% 331 / 415 CC 73 SCAR 27.7
CC
73
Cov
82%
__ANON__
29.2
__ANON__
17.2
__ANON__
16.3
CRAP 16.0
« Review/Filter.pm ↑ Summary Review/Report.pm »
1 package Claude::Agent::Code::Review::Tools;
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 Claude::Agent qw(tool create_sdk_mcp_server);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Glob qw(:bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶sub create_server {
Subroutine create_server: called CC=1 SCAR=0.0
Pod documented
12 7 my ($class) = @_;
13  
14 7 return create_sdk_mcp_server(
15 name => 'code_review',
16 version => '1.0.0',
17 tools => [
18 $class->_get_file_context_tool(),
19 $class->_search_codebase_tool(),
20 $class->_check_tests_tool(),
21 $class->_get_dependencies_tool(),
22 $class->_analyze_complexity_tool(),
23 ],
24 );
25 }
26  
27 7 ▶sub _get_file_context_tool {
Subroutine _get_file_context_tool: called CC=1 SCAR=0.0
28 return tool(
29 'get_file_context',
30 'Get surrounding context for a specific line in a file',
31 {
32 type => 'object',
33 properties => {
34 file => {
35 type => 'string',
36 description => 'File path',
37 },
38 line => {
39 type => 'integer',
40 description => 'Line number',
41 },
42 before => {
43 type => 'integer',
44 description => 'Lines of context before (default: 5)',
45 },
46 after => {
47 type => 'integer',
48 description => 'Lines of context after (default: 5)',
49 },
50 },
51 required => ['file', 'line'],
52 },
53 4 ▶ sub {
Subroutine __ANON__: called CC=11 SCAR=24.5
54 4 my ($args) = @_;
55  
56 4 my $file = $args->{file};
57 4 my $line = $args->{line};
58 4 ▶ my $before = $args->{before} // 5;
Condition100%2/2
$args->{"before"} // 5
l!l
13
MC/DC100%1/1
$args->{"before"} // 5
$args->{"before"}
Truth table100%2/2
A
A= $args->{"before"}
Aresult
11
00
59 4 ▶ my $after = $args->{after} // 5;
Condition100%2/2
$args->{"after"} // 5
l!l
13
MC/DC100%1/1
$args->{"after"} // 5
$args->{"after"}
Truth table100%2/2
A
A= $args->{"after"}
Aresult
11
00
60  
61 4 my $file_path = path($file);
62 4 ▶ unless (-f $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file_path) 2 2 4
63 return {
64 2 content => [{ type => 'text', text => "File not found: $file" }],
65 is_error => 1,
66 };
67 }
68  
69 2 my $fh = eval { $file_path->openr_utf8 };
70 2 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 2 2
71 return {
72 0 content => [{ type => 'text', text => "Cannot open file: $file" }],
73 is_error => 1,
74 };
75 }
76  
77 2 my $safe_path = eval { $file_path->realpath };
78 2 my $base_dir = path('.')->realpath;
79 2 ▶ unless ($safe_path && $base_dir->subsumes($safe_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($safe_path and $base_dir->subsumes($safe_path)) 0 2 2
Condition33%1/3
$safe_path and $base_dir->subsumes($safe_path)
!ll&&!rl&&r
002
MC/DC0%0/2
$safe_path and $base_dir->subsumes($safe_path)
$safe_path $base_dir->subsumes($safe_path)
Truth table33%1/3
A and B
A= $safe_pathB= $base_dir->subsumes($safe_path)
ABresult
0X0
100
111
80 0 close $fh;
81 return {
82 0 content => [{ type => 'text', text => "Access denied: $file" }],
83 is_error => 1,
84 };
85 }
86  
87 2 my @lines = map { chomp; $_ } <$fh>;
88 2 close $fh;
89 2 my $total = scalar @lines;
90  
91 2 my $start = $line - $before - 1;
92 2 my $end = $line + $after - 1;
93  
94 2 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 2 2
95 2 ▶ $end = $total - 1 if $end >= $total;
Branch: 1 branch
BranchTrueFalseTotal
if $end >= $total 0 2 2
96  
97 2 my @context;
98 2 for my $i ($start .. $end) {
99 16 my $ln = $i + 1;
100 16 ▶ my $marker = ($ln == $line) ? '>>>' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$ln == $line ? : 2 14 16
101 16 push @context, sprintf "%s %4d: %s", $marker, $ln, $lines[$i];
102 }
103  
104 return {
105 2 content => [{
106 type => 'text',
107 text => join("\n", @context),
108 }],
109 };
110 }
111 7 );
112 }
113  
114 7 ▶sub _search_codebase_tool {
Subroutine _search_codebase_tool: called CC=1 SCAR=0.1
115 return tool(
116 'search_codebase',
117 'Search for patterns across the codebase',
118 {
119 type => 'object',
120 properties => {
121 pattern => {
122 type => 'string',
123 description => 'Text or pattern to search for',
124 },
125 literal => {
126 type => 'boolean',
127 description => 'If true, treat pattern as literal text (safer, default). If false, treat as regex.',
128 },
129 file_pattern => {
130 type => 'string',
131 description => 'File glob pattern (e.g., "*.pm", "lib/**/*.pl")',
132 },
133 max_results => {
134 type => 'integer',
135 description => 'Maximum results to return (default: 20)',
136 },
137 },
138 required => ['pattern'],
139 },
140 8 ▶ sub {
Subroutine __ANON__: called CC=24 SCAR=33.7
141 8 my ($args) = @_;
142  
143 8 my $pattern = $args->{pattern};
144 8 ▶ my $literal = $args->{literal} // 1;
Condition100%2/2
$args->{"literal"} // 1
l!l
35
MC/DC100%1/1
$args->{"literal"} // 1
$args->{"literal"}
Truth table100%2/2
A
A= $args->{"literal"}
Aresult
11
00
145 8 ▶ my $file_pattern = $args->{file_pattern} // '**/*.{pm,pl,t}';
Condition100%2/2
$args->{"file_pattern"} // "**/*.{pm,pl,t}"
l!l
17
MC/DC100%1/1
$args->{"file_pattern"} // "**/*.{pm,pl,t}"
$args->{"file_pattern"}
Truth table100%2/2
A
A= $args->{"file_pattern"}
Aresult
11
00
146 8 ▶ my $max_results = $args->{max_results} // 20;
Condition50%1/2
$args->{"max_results"} // 20
l!l
08
MC/DC0%0/1
$args->{"max_results"} // 20
$args->{"max_results"}
Truth table50%1/2
A
A= $args->{"max_results"}
Aresult
11
00
147  
148 8 ▶ if ($file_pattern =~ /\.\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_pattern =~ /\.\./u) 1 7 8
149 return {
150 1 content => [{ type => 'text', text => "Invalid file pattern: '..' not allowed" }],
151 is_error => 1,
152 };
153 }
154  
155 7 my @results;
156 my $re;
157  
158 7 ▶ if ($literal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal) { } 4 3 7
159 4 $re = qr/\Q$pattern\E/i;
160 } else {
161  
162 3 ▶ if (length($pattern) > 500) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pattern > 500) 1 2 3
163 return {
164 1 content => [{ type => 'text', text => "Pattern too long (max 500 chars)" }],
165 is_error => 1,
166 };
167 }
168  
169 2 ▶ if ($pattern =~ /\([^)]*[+*][^)]*\)[+*?]/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\([^)]*[+*][^)]*\)[+*?]/u or $pattern =~ /\(\?[^)]*[+*][^)]*\)[+*?]/u) 1 1 2
Condition66%2/3
$pattern =~ /\([^)]*[+*][^)]*\)[+*?]/u or $pattern =~ /\(\?[^)]*[+*][^)]*\)[+*?]/u
l!l&&r!l&&!r
101
MC/DC50%1/2
$pattern =~ /\([^)]*[+*][^)]*\)[+*?]/u or $pattern =~ /\(\?[^)]*[+*][^)]*\)[+*?]/u
$pattern =~ /\([^)]*[+*][^)]*\)[+*?]/u $pattern =~ /\(\?[^)]*[+*][^)]*\)[+*?]/u
Truth table66%2/3
A or B
A= $pattern =~ /\([^)]*[+*][^)]*\)[+*?]/uB= $pattern =~ /\(\?[^)]*[+*][^)]*\)[+*?]/u
ABresult
1X1
011
000
170 $pattern =~ /\(\?[^)]*[+*][^)]*\)[+*?]/) {
171 return {
172 1 content => [{ type => 'text', text => "Unsafe regex: nested quantifiers not allowed (ReDoS risk)" }],
173 is_error => 1,
174 };
175 }
176  
177 1 eval {
178 1 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne 'MSWin32') { } 1 0 1
179 0 ▶ local $SIG{ALRM} = sub { die "regex compilation timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
180 1 alarm(2);
181 1 $re = qr/$pattern/i;
182 0 alarm(0);
183 } else {
184 0 $re = qr/$pattern/i;
185 }
186 };
187 1 ▶ alarm(0) if $^O ne 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "MSWin32" 1 0 1
188 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
189 return {
190 1 content => [{ type => 'text', text => "Invalid or unsafe regex: $@" }],
191 is_error => 1,
192 };
193 }
194 }
195  
196 4 my $base_dir = path('.')->realpath;
197  
198 4 my @files = bsd_glob($file_pattern, GLOB_BRACE);
199 4 ▶ unless (@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 4 0 4
200 4 my $ext_pattern = '\.(?:pm|pl|t)$';
201 4 ▶ if ($file_pattern =~ /\*\.(\w+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file_pattern =~ /\*\.(\w+)$/u) { } 0 4 4
elsif ($file_pattern =~ /\*\.\{([^}]+)\}$/u) { } 4 0 4
202 0 $ext_pattern = '\.' . quotemeta($1) . '$';
203 } elsif ($file_pattern =~ /\*\.\{([^}]+)\}$/) {
204 4 my $exts = join('|', map { quotemeta($_) } split(/,/, $1));
205 4 $ext_pattern = '\.(?:' . $exts . ')$';
206 }
207 4 my $ext_re = qr/$ext_pattern/;
208 8 ▶ path('.')->visit(sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
209 8 ▶ push @files, $_->stringify if $_->is_file && $_->stringify =~ $ext_re;
Branch: 1 branch
BranchTrueFalseTotal
if $_->is_file and $_->stringify =~ /$ext_re/u 8 0 8
Condition33%1/3
$_->is_file and $_->stringify =~ /$ext_re/u
!ll&&!rl&&r
008
MC/DC0%0/2
$_->is_file and $_->stringify =~ /$ext_re/u
$_->is_file $_->stringify =~ /$ext_re/u
Truth table33%1/3
A and B
A= $_->is_fileB= $_->stringify =~ /$ext_re/u
ABresult
0X0
100
111
210 4 }, { recurse => 1 });
211 }
212  
213 4 FILE: for my $file (@files) {
214 8 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 8 8
215 8 my $path = path($file);
216  
217 8 my $safe_path = eval { $path->realpath };
218 8 ▶ next unless $safe_path && $base_dir->subsumes($safe_path);
Branch: 1 branch
BranchTrueFalseTotal
unless $safe_path and $base_dir->subsumes($safe_path) 0 8 8
Condition33%1/3
$safe_path and $base_dir->subsumes($safe_path)
!ll&&!rl&&r
008
MC/DC0%0/2
$safe_path and $base_dir->subsumes($safe_path)
$safe_path $base_dir->subsumes($safe_path)
Truth table33%1/3
A and B
A= $safe_pathB= $base_dir->subsumes($safe_path)
ABresult
0X0
100
111
219  
220 8 my $line_num = 0;
221 8 for my $line ($safe_path->lines_utf8({ chomp => 1 })) {
222 96 $line_num++;
223 96 ▶ next if length($line) > 10_000;
Branch: 1 branch
BranchTrueFalseTotal
if length $line > 10000 0 96 96
224 96 ▶ if ($line =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$re/u) 1 95 96
225 1 push @results, sprintf "%s:%d: %s", $safe_path, $line_num, $line;
226 1 ▶ last FILE if @results >= $max_results;
Branch: 1 branch
BranchTrueFalseTotal
if @results >= $max_results 0 1 1
227 }
228 }
229 }
230  
231 4 ▶ my $text = @results
Branch: 1 branch
BranchTrueFalseTotal
@results ? : 1 3 4
232 ? join("\n", @results)
233 : "No matches found for pattern: $pattern";
234  
235 return {
236 4 content => [{ type => 'text', text => $text }],
237 };
238 }
239 7 );
240 }
241  
242 7 ▶sub _check_tests_tool {
Subroutine _check_tests_tool: called CC=1 SCAR=0.0
243 return tool(
244 'check_tests',
245 'Check if tests exist for a module or function',
246 {
247 type => 'object',
248 properties => {
249 module => {
250 type => 'string',
251 description => 'Module name (e.g., My::Module)',
252 },
253 function => {
254 type => 'string',
255 description => 'Function name to check (optional)',
256 },
257 },
258 required => ['module'],
259 },
260 3 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=23.3
261 3 my ($args) = @_;
262  
263 3 my $module = $args->{module};
264 3 my $function = $args->{function};
265  
266 3 my $module_path = $module;
267 3 $module_path =~ s/::/-/g;
268 3 $module_path =~ s/([*?\[\]{}])/\\$1/g;
269  
270 3 my @test_patterns = (
271 "t/*$module_path*.t",
272 "t/**/*$module_path*.t",
273 "xt/*$module_path*.t",
274 );
275  
276 3 my @found_tests;
277 3 for my $pattern (@test_patterns) {
278 9 push @found_tests, bsd_glob($pattern, GLOB_BRACE);
279 }
280  
281 3 my $base_dir = path('.')->realpath;
282  
283 3 my @results;
284 3 ▶ if (@found_tests) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_tests) { } 2 1 3
285 2 push @results, "Found test files for $module:";
286 2 for my $test (@found_tests) {
287 2 push @results, " - $test";
288  
289 2 ▶ if ($function && !-l $test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($function and not -l $test) 1 1 2
Condition66%2/3
$function and not -l $test
!ll&&!rl&&r
101
MC/DC50%1/2
$function and not -l $test
$function not -l $test
Truth table66%2/3
A and not(B)
A= $functionB= not -l $test
ABresult
0X0
100
111
290 1 my $test_path = eval { path($test)->realpath };
291 1 ▶ next unless $test_path && $base_dir->subsumes($test_path);
Branch: 1 branch
BranchTrueFalseTotal
unless $test_path and $base_dir->subsumes($test_path) 0 1 1
Condition33%1/3
$test_path and $base_dir->subsumes($test_path)
!ll&&!rl&&r
001
MC/DC0%0/2
$test_path and $base_dir->subsumes($test_path)
$test_path $base_dir->subsumes($test_path)
Truth table33%1/3
A and B
A= $test_pathB= $base_dir->subsumes($test_path)
ABresult
0X0
100
111
292 1 ▶ my $content = eval { $test_path->slurp_utf8 } // '';
Condition50%1/2
eval { do { $test_path->slurp_utf8 } } // ""
l!l
10
MC/DC0%0/1
eval { do { $test_path->slurp_utf8 } } // ""
eval { do { $test_path->slurp_utf8 } }
Truth table50%1/2
A
A= eval { do { $test_path->slurp_utf8 } }
Aresult
11
00
293 1 my $safe_func = quotemeta($function);
294 1 ▶ if ($content =~ /\b$safe_func\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\b$safe_func\b/u) 1 0 1
295 1 push @results, " (mentions '$function')";
296 }
297 }
298 }
299 }
300 else {
301 1 push @results, "No test files found for $module";
302 1 push @results, "Consider creating tests in t/$module_path.t";
303 }
304  
305 return {
306 3 content => [{ type => 'text', text => join("\n", @results) }],
307 };
308 }
309 7 );
310 }
311  
312 7 ▶sub _get_dependencies_tool {
Subroutine _get_dependencies_tool: called CC=1 SCAR=0.1
313 return tool(
314 'get_dependencies',
315 'Get module dependencies for a file',
316 {
317 type => 'object',
318 properties => {
319 file => {
320 type => 'string',
321 description => 'File path to analyze',
322 },
323 },
324 required => ['file'],
325 },
326 3 ▶ sub {
Subroutine __ANON__: called CC=15 SCAR=28.5
327 3 my ($args) = @_;
328  
329 3 my $file = $args->{file};
330 3 my $file_path = path($file);
331  
332 3 ▶ unless (-f $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file_path) 2 1 3
333 return {
334 2 content => [{ type => 'text', text => "File not found: $file" }],
335 is_error => 1,
336 };
337 }
338  
339 1 my $fh = eval { $file_path->openr_utf8 };
340 1 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 1 1
341 return {
342 0 content => [{ type => 'text', text => "Cannot open file: $file" }],
343 is_error => 1,
344 };
345 }
346  
347 1 my $safe_path = eval { $file_path->realpath };
348 1 my $base_dir = path('.')->realpath;
349 1 ▶ unless ($safe_path && $base_dir->subsumes($safe_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($safe_path and $base_dir->subsumes($safe_path)) 0 1 1
Condition33%1/3
$safe_path and $base_dir->subsumes($safe_path)
!ll&&!rl&&r
001
MC/DC0%0/2
$safe_path and $base_dir->subsumes($safe_path)
$safe_path $base_dir->subsumes($safe_path)
Truth table33%1/3
A and B
A= $safe_pathB= $base_dir->subsumes($safe_path)
ABresult
0X0
100
111
350 0 close $fh;
351 return {
352 0 content => [{ type => 'text', text => "Access denied: $file" }],
353 is_error => 1,
354 };
355 }
356  
357 1 my $content = do { local $/; <$fh> };
358 1 close $fh;
359  
360 1 my @use_statements;
361 my @require_statements;
362  
363 1 ▶ while ($content =~ /^\s*use\s+([\w:]+)(?:\s|;)/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*use\s+([\w:]+)(?:\s|;)/gmu) 4 1 5
364 4 ▶ push @use_statements, $1 unless $1 =~ /^(strict|warnings|v?\d|parent|base|lib|constant|if|feature)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 =~ /^(strict|warnings|v?\d|parent|base|lib|constant|if|feature)$/u 2 2 4
365 }
366  
367 1 ▶ while ($content =~ /^\s*require\s+([\w:]+)/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*require\s+([\w:]+)/gmu) 1 1 2
368 1 my $mod = $1;
369 1 ▶ next if $mod =~ /^v?\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $mod =~ /^v?\d/u 0 1 1
370 1 push @require_statements, $mod;
371 }
372  
373 1 my @results;
374 1 push @results, "Dependencies for $file:";
375 1 push @results, "";
376  
377 1 ▶ if (@use_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@use_statements) 1 0 1
378 1 push @results, "use statements:";
379 1 push @results, " - $_" for sort @use_statements;
380 }
381  
382 1 ▶ if (@require_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@require_statements) 1 0 1
383 1 push @results, "";
384 1 push @results, "require statements:";
385 1 push @results, " - $_" for sort @require_statements;
386 }
387  
388 1 ▶ unless (@use_statements || @require_statements) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@use_statements or @require_statements) 0 1 1
Condition33%1/3
@use_statements or @require_statements
l!l&&r!l&&!r
100
MC/DC0%0/2
@use_statements or @require_statements
@use_statements @require_statements
Truth table33%1/3
A or B
A= @use_statementsB= @require_statements
ABresult
1X1
011
000
389 0 push @results, " (no external dependencies found)";
390 }
391  
392 return {
393 1 content => [{ type => 'text', text => join("\n", @results) }],
394 };
395 }
396 7 );
397 }
398  
399 7 ▶sub _analyze_complexity_tool {
Subroutine _analyze_complexity_tool: called CC=1 SCAR=0.1
400 return tool(
401 'analyze_complexity',
402 'Analyze cyclomatic complexity of a subroutine',
403 {
404 type => 'object',
405 properties => {
406 file => {
407 type => 'string',
408 description => 'File path',
409 },
410 function => {
411 type => 'string',
412 description => 'Function/subroutine name',
413 },
414 },
415 required => ['file', 'function'],
416 },
417 6 ▶ sub {
Subroutine __ANON__: called CC=15 SCAR=27.9
418 6 my ($args) = @_;
419  
420 6 my $file = $args->{file};
421 6 my $function = $args->{function};
422 6 my $file_path = path($file);
423  
424 6 ▶ unless (-f $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file_path) 2 4 6
425 return {
426 2 content => [{ type => 'text', text => "File not found: $file" }],
427 is_error => 1,
428 };
429 }
430  
431 4 my $fh = eval { $file_path->openr_utf8 };
432 4 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 4 4
433 return {
434 0 content => [{ type => 'text', text => "Cannot open file: $file" }],
435 is_error => 1,
436 };
437 }
438  
439 4 my $safe_path = eval { $file_path->realpath };
440 4 my $base_dir = path('.')->realpath;
441 4 ▶ unless ($safe_path && $base_dir->subsumes($safe_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($safe_path and $base_dir->subsumes($safe_path)) 0 4 4
Condition33%1/3
$safe_path and $base_dir->subsumes($safe_path)
!ll&&!rl&&r
004
MC/DC0%0/2
$safe_path and $base_dir->subsumes($safe_path)
$safe_path $base_dir->subsumes($safe_path)
Truth table33%1/3
A and B
A= $safe_pathB= $base_dir->subsumes($safe_path)
ABresult
0X0
100
111
442 0 close $fh;
443 return {
444 0 content => [{ type => 'text', text => "Access denied: $file" }],
445 is_error => 1,
446 };
447 }
448  
449 4 my $content = do { local $/; <$fh> };
450 4 close $fh;
451  
452 4 my $safe_func = quotemeta($function);
453 4 my $sub_re = qr/sub\s+$safe_func\s*\{/;
454 4 ▶ unless ($content =~ $sub_re) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content =~ /$sub_re/u) 1 3 4
455 return {
456 1 content => [{ type => 'text', text => "Subroutine '$function' not found in $file" }],
457 };
458 }
459  
460 3 my $sub_body;
461 3 ▶ if ($content =~ /sub\s+$safe_func\s*\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /sub\s+$safe_func\s*\{/u) 3 0 3
462 3 my $pos = $+[0] - 1;
463 3 my $depth = 0;
464 3 my $start = $pos;
465 3 for my $i ($pos .. length($content) - 1) {
466 829 my $char = substr($content, $i, 1);
467 829 ▶ if ($char eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '{') { } 22 807 829
elsif ($char eq '}') { } 22 785 807
468 22 $depth++;
469 } elsif ($char eq '}') {
470 22 $depth--;
471 22 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 3 19 22
472 3 $sub_body = substr($content, $start, $i - $start + 1);
473 3 last;
474 }
475 }
476 }
477 }
478  
479 3 ▶ unless ($sub_body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_body) 0 3 3
480 return {
481 0 content => [{ type => 'text', text => "Could not extract body of '$function'" }],
482 };
483 }
484  
485 3 my $code_only = $sub_body;
486 3 $code_only =~ s/#.*$//gm;
487 3 $code_only =~ s/'(?:[^'\\]|\\.)*'//g;
488 3 $code_only =~ s/"(?:[^"\\]|\\.)*"//g;
489 3 $code_only =~ s/<<['"]?(\w+)['"]?.*?\n\1//gs;
490  
491 3 my $complexity = 1;
492  
493 3 $complexity += () = $code_only =~ /\b(if|elsif|unless|while|until|for|foreach)\b/g;
494 3 $complexity += () = $code_only =~ /\b(and|or)\b/g;
495 3 $complexity += () = $code_only =~ /(\?\s*:)/g;
496 3 $complexity += () = $code_only =~ /(\|\||\&\&)/g;
497  
498 3 my $assessment;
499 3 ▶ if ($complexity <= 5) {
Branch: 3 branches
BranchTrueFalseTotal
if ($complexity <= 5) { } 2 1 3
elsif ($complexity <= 10) { } 1 0 1
elsif ($complexity <= 20) { } 0 0 0
500 2 $assessment = "Low complexity - well structured";
501 }
502 elsif ($complexity <= 10) {
503 1 $assessment = "Moderate complexity - acceptable";
504 }
505 elsif ($complexity <= 20) {
506 0 $assessment = "High complexity - consider refactoring";
507 }
508 else {
509 0 $assessment = "Very high complexity - refactoring recommended";
510 }
511  
512 3 my @results = (
513 "Complexity analysis for $function in $file:",
514 "",
515 " Cyclomatic complexity: $complexity",
516 " Assessment: $assessment",
517 );
518  
519 return {
520 3 content => [{ type => 'text', text => join("\n", @results) }],
521 };
522 }
523 7 );
524 }
525  
526  
527 4 1;
528  
529 __END__
« Review/Filter.pm ↑ Summary Review/Report.pm »