CodeSearch/RegexBuilder.pm

filter: Statementstmt 100% 118 / 118 Branchbran 100% 46 / 46 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 100% 192 / 192 CC 29 SCAR 18.1
CC
29
Cov
100%
make_regex
12.0
check_sub_matches
7.0
check_lasts
7.0
CRAP 6.1
« CodeSearch/Highlighter.pm ↑ Summary CodeSearch/Replacer.pm »
1 package File::CodeSearch::RegexBuilder;
2  
3 # Created on: 2009-08-07 18:41:21
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use List::MoreUtils qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 our $VERSION = version->new('0.7.6');
17  
18 5 has regex => (
19 is => 'rw',
20 );
21 5 has re => (
22 is => 'ro',
23 isa => 'ArrayRef',
24 );
25 5 has whole => (
26 is => 'ro',
27 isa => 'Bool',
28 );
29 5 has all => (
30 is => 'ro',
31 isa => 'Bool',
32 );
33 5 has words => (
34 is => 'ro',
35 isa => 'Bool',
36 );
37 5 has ignore_case => (
38 is => 'ro',
39 isa => 'Bool',
40 );
41 has files => (
42 is => 'rw',
43 isa => 'HashRef',
44 35 ▶ default => sub{{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
45 5 );
46 5 has current_file => (
47 is => 'rw',
48 );
49 5 has current_count => (
50 is => 'rw',
51 isa => 'Int',
52 default => 0,
53 );
54 has sub_matches => (
55 is => 'rw',
56 isa => 'ArrayRef[Str]',
57 34 ▶ default => sub{[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
58 5 );
59 5 has sub_match => (
60 is => 'rw',
61 isa => 'Bool',
62 );
63 has sub_not_matches => (
64 is => 'rw',
65 isa => 'ArrayRef[Str]',
66 34 ▶ default => sub{[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
67 5 );
68 5 has sub_not_match => (
69 is => 'rw',
70 isa => 'Bool',
71 );
72 5 has last => (
73 is => 'rw',
74 isa => 'ArrayRef[Str]',
75 );
76 has lasts => (
77 is => 'rw',
78 isa => 'HashRef[Str]',
79 35 ▶ default => sub{{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
80 5 );
81 5 has smart => (
82 is => 'rw',
83 isa => 'Bool',
84 );
85  
86 4562 ▶sub make_regex {
Subroutine make_regex: called CC=12 SCAR=24.8
Pod documented
87 4562 my ($self) = @_;
88 4562 ▶ return $self->regex if ref $self->regex eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->regex eq "Regexp" 4529 33 4562
89  
90 33 my $re;
91 33 my $words = $self->re;
92  
93 33 my $start = shift @{ $words };
94 33 ▶ return $self->regex(qr//) if !defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 1 32 33
95  
96 32 ▶ if (!any {$start eq $_} qw/n b ss/) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $start eq $_; } , "n", "b", "ss")) 29 3 32
97 29 unshift @{ $words }, $start;
98 29 undef $start;
99 }
100  
101 32 ▶ if ($self->whole) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->whole) 5 27 32
102 5 @{$words} = map { "\\b$_\\b" } @{$words};
103 }
104  
105 32 ▶ if ($self->all) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->all) { } 4 28 32
elsif ($self->words) { } 2 26 28
106 4 ▶ if (@{ $words } == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$words;} == 2) { } 3 1 4
107 3 $re = "$words->[0].*$words->[1]|$words->[1].*$words->[0]";
108 }
109 else {
110 1 $re = join ' ', @$words;
111 }
112 }
113 elsif ( $self->words ) {
114 2 $re = join '.*', @{ $words };
115 }
116 else {
117 26 $re = join ' ', @{ $words };
118 }
119  
120 32 ▶ if ($self->ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_case) 1 31 32
121 1 $re = "(?i:$re)";
122 }
123  
124 $re =
125 32 ▶ !defined $start ? $re
Branch: 3 branches
BranchTrueFalseTotal
!defined($start) ? : 29 3 32
$start eq 'n' ? : 1 2 3
$start eq 'b' ? : 1 1 2
126 : $start eq 'n' ? "function(?:&?\\s+|\\s+&?\\s*)$re|$re\\s+=\\s+function"
127 : $start eq 'b' ? "sub\\s+$re"
128 : "class\\s+$re";
129  
130 32 return $self->regex(qr/$re/);
131 }
132  
133 4530 ▶sub match {
Subroutine match: called CC=2 SCAR=6.9
Pod documented
134 4530 my ($self, $line) = @_;
135 4530 my $re = $self->make_regex;
136  
137 4530 $self->check_sub_matches($line);
138 4530 $self->check_lasts($line);
139  
140 4530 my ($match) = $line =~ /($re)/;
141  
142 4530 ▶ if (defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match) 3323 1207 4530
143 3323 $self->current_count( $self->current_count + 1 );
144 }
145  
146 4530 return $match;
147 }
148  
149 4535 ▶sub check_sub_matches {
Subroutine check_sub_matches: called CC=7 SCAR=19.5
Pod documented
150 4535 my ($self, $line) = @_;
151 4535 my $matches = $self->sub_matches;
152 4535 my $match = 0;
153 4535 my $not_matches = $self->sub_not_matches;
154 4535 my $not_match = 0;
155  
156 4535 ▶ return if $self->sub_match;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sub_match 1 4534 4535
157 4534 ▶ return if $self->sub_not_match;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sub_not_match 1 4533 4534
158  
159 4533 for my $match_re (@$matches) {
160 3 $match = $line =~ /$match_re/;
161 3 ▶ last if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 1 2 3
162 }
163  
164 4533 $self->sub_match($match);
165  
166 4533 for my $not_match_re (@$not_matches) {
167 3 $not_match = $line =~ /$not_match_re/;
168 3 ▶ last if $not_match;
Branch: 1 branch
BranchTrueFalseTotal
if $not_match 1 2 3
169 }
170  
171 4533 $self->sub_not_match($not_match);
172  
173 4533 return;
174 }
175  
176 4530 ▶sub check_lasts {
Subroutine check_lasts: called CC=7 SCAR=19.5
Pod documented
177 4530 my ($self, $line) = @_;
178  
179 4530 ▶ if ($self->last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last) 12 4518 4530
180 12 for my $last (@{ $self->last }) {
181 18 ▶ my ($match) =
Branch: 3 branches
BranchTrueFalseTotal
$last eq 'function' ? : 3 15 18
$last eq 'class' ? : 6 9 15
$last eq 'sub' ? : 6 3 9
182 $last eq 'function' ? $line =~ /function \s+ (?: & \s*)? ([\w-]+)/xms
183 : $last eq 'class' ? $line =~ /class \s+ ([\w-]+)/xms
184 : $last eq 'sub' ? $line =~ /sub \s+ ([\w-]+)/xms
185 : $line =~ /$last \s+ ([\w-]+)/xms;
186 18 ▶ $self->lasts->{$last} = $match if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 6 12 18
187 }
188 }
189  
190 4530 return;
191 }
192  
193 5 ▶sub get_last_found {
Subroutine get_last_found: called CC=3 SCAR=11.0
Pod documented
194 5 my ($self) = @_;
195 5 my $out = '';
196  
197 5 ▶ return '' if ! %{$self->lasts};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$self->lasts;} 1 4 5
198  
199 4 for my $last (sort keys %{$self->lasts} ) {
200 6 $out .= "$last " . $self->lasts->{$last} . "\n";
201 }
202  
203 4 return $out;
204 }
205  
206 28 ▶sub reset_file {
Subroutine reset_file: called CC=3 SCAR=11.0
Pod documented
207 28 my ($self, $file) = @_;
208 28 ▶ if ( $self->current_count() && $self->current_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_count and $self->current_file) 22 6 28
Condition100%3/3
$self->current_count and $self->current_file
!ll&&!rl&&r
3322
MC/DC100%2/2
$self->current_count and $self->current_file
$self->current_count $self->current_file
Truth table100%3/3
A and B
A= $self->current_countB= $self->current_file
ABresult
0X0
100
111
209 22 $self->files->{$self->current_file} = $self->current_count;
210 }
211  
212 28 $self->sub_match(0);
213 28 $self->sub_not_match(0);
214 28 $self->current_count(0);
215 28 $self->current_file($file);
216 28 $self->lasts({});
217  
218 28 return;
219 }
220  
221  
222 5 1;
223  
224 __END__
« CodeSearch/Highlighter.pm ↑ Summary CodeSearch/Replacer.pm »