blib/lib/Devel/DidYouMean.pm

filter: Statementstmt 90.3% 47 / 52 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 88.7% 63 / 71 CC 10 SCAR 19.4
CC
10
Cov
88%
__ANON__
10.5
BEGIN
1.0
BEGIN
1.0
CRAP 7.0
↑ Summary
1 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Devel::DidYouMean;
5 1 $Devel::DidYouMean::VERSION = '0.05';
6 1 ▶use Text::Levenshtein;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Perl::Builtins;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Intercepts failed function and method calls, suggesting the nearest matching alternative.
10  
11  
12 1 our $DYM_MATCHING_SUBS = [];
13  
14 3 ▶$SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=10 SCAR=23.5
15  
16 1 ▶ no strict qw/refs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 my ($error, $package, $sub_name, $new_error) = @_;
18  
19 3 my $undef_sub = qr/^Undefined subroutine &(.+?) called (at .+?\.)/;
20 3 my $missing_method = qr/^Can't locate object method "(.+?)" via package "(.+?)" (at .+?\.)/;
21  
22 3 ▶ if ($error =~ /$undef_sub/)
Branch: 2 branches
BranchTrueFalseTotal
if ($error =~ /$undef_sub/) { } 3 0 3
elsif ($error =~ /$missing_method/) { } 0 0 0
23 {
24 3 my @sub_path = split /::/, $1;
25 3 $sub_name = pop @sub_path;
26 3 $package = join '::', @sub_path;
27 3 $new_error = $2;
28 }
29 elsif ($error =~ /$missing_method/)
30 {
31 0 $sub_name = $1;
32 0 $package = $2;
33 0 $new_error = $3;
34 }
35 else
36 {
37 0 print "No match\n";
38 0 return undef;
39 }
40  
41 3 my $package_namespace = $package . '::';
42 3 my %valid_subs = ();
43  
44 3 for (keys %$package_namespace)
45 {
46 676 my $absolute_name = $package_namespace . $_;
47 676 ▶ if (defined &{$absolute_name})
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$absolute_name;}) 103 573 676
48 {
49 103 $valid_subs{$_} = Text::Levenshtein::fastdistance($sub_name, $_);
50 }
51 }
52  
53 # if package is main, add in builtin functions
54 3 ▶ if ($package eq 'main')
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "main") 2 1 3
55 {
56 2 for (Perl::Builtins::list)
57 {
58 460 $valid_subs{$_} = Text::Levenshtein::fastdistance($sub_name, $_);
59 }
60 }
61  
62 3 $DYM_MATCHING_SUBS = [];
63 3 my $match_score;
64  
65 # return similarly named functions
66 3 for (sort { $valid_subs{$a} <=> $valid_subs{$b} } keys %valid_subs)
67 {
68 6 ▶ $match_score = $valid_subs{$_} unless $match_score;
Branch: 1 branch
BranchTrueFalseTotal
unless $match_score 3 3 6
69  
70 6 ▶ if ($match_score < $valid_subs{$_})
Branch: 1 branch
BranchTrueFalseTotal
if ($match_score < $valid_subs{$_}) 3 3 6
71 {
72 3 die $error . "Did you mean " . join(', ', @$DYM_MATCHING_SUBS) . "?\n";
73 }
74 3 push @$DYM_MATCHING_SUBS, $_;
75 }
76 1 };
77  
78 1 1;
79  
80 __END__
↑ Summary