lib/Perl/Critic/PJCJ/Fixer.pm

filter: Statementstmt 100% 180 / 180 Branchbran 98.5% 136 / 138 (2 uncoverable) Conditioncond 98.2% 53 / 56 (2 uncoverable) MC/DCmcdc 97.1% 32 / 35 (2 uncoverable) Subroutinesub 100% 37 / 37 Podpod 100% 2 / 2 totaltotal 99.1% 440 / 448 (6 uncoverable) CC 94 SCAR 19.0
CC
94
Cov
99%
_replacement
14.0
_fix_include
13.0
_value_preserved
10.0
CRAP 6.7
« utils/perl-hook ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/RequireConsistentQuoting.pm »
1 package Perl::Critic::PJCJ::Fixer v0.3.0;
2  
3 42 ▶use v5.26.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 42 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 42 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 42 ▶use List::Util qw( all any );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 42 ▶use PPI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 42 ▶use Perl::Critic::Policy::ValuesAndExpressions::RequireConsistentQuoting ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 42 my $Max_passes = 10;
14 42 my %Interpolating = map { $_ => 1 } qw(
15 PPI::Token::Quote::Double
16 PPI::Token::Quote::Interpolate
17 PPI::Token::QuoteLike::Command
18 );
19  
20 26 ▶sub _interpolates ($self, $elem) {
Subroutine _interpolates: called CC=4 SCAR=13.9
21 # perlop: qx does not interpolate when its delimiter is ''
22 26 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if ref $elem eq "PPI::Token::QuoteLike::Command" and $elem->content =~ /\Aqx\s*'/u 1 25 26
Condition100%3/3
ref $elem eq "PPI::Token::QuoteLike::Command" and $elem->content =~ /\Aqx\s*'/u
!ll&&!rl&&r
1241
MC/DC100%2/2
ref $elem eq "PPI::Token::QuoteLike::Command" and $elem->content =~ /\Aqx\s*'/u
ref $elem eq "PPI::Token::QuoteLike::Command" $elem->content =~ /\Aqx\s*'/u
Truth table100%3/3
A and B
A= ref $elem eq "PPI::Token::QuoteLike::Command"B= $elem->content =~ /\Aqx\s*'/u
ABresult
0X0
100
111
23 if ref $elem eq "PPI::Token::QuoteLike::Command"
24 && $elem->content =~ /\Aqx\s*'/;
25 25 ▶ $Interpolating{ ref $elem } // 0
Condition100%2/2
$Interpolating{ref $elem} // 0
l!l
241
MC/DC100%1/1
$Interpolating{ref $elem} // 0
$Interpolating{ref $elem}
Truth table100%2/2
A
A= $Interpolating{ref $elem}
Aresult
11
00
26 }
27  
28 59 ▶sub new ($class) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 59 my $policy
30 = Perl::Critic::Policy::ValuesAndExpressions::RequireConsistentQuoting->new;
31 59 bless { policy => $policy }, $class
32 }
33  
34 # Only valid for content free of quote-sensitive escapes; conversion call
35 # sites guard with the policy's has_quote_sensitive_escapes predicate
36 159 ▶sub _decode_double ($self, $raw) { $raw =~ s/\\(.)/$1/gsr }
Subroutine _decode_double: called CC=1 SCAR=0.0
37  
38 175 ▶sub _decode_q ($self, $raw, $start, $end) {
Subroutine _decode_q: called CC=1 SCAR=0.0
39 175 $raw =~ s/\\([\\\Q$start$end\E])/$1/gr
40 }
41  
42 142 ▶sub _decode_delimiters ($self, $raw, $start, $end) {
Subroutine _decode_delimiters: called CC=3 SCAR=11.0
43 142 ▶ $raw =~ s/\\(.)/$1 eq $start || $1 eq $end ? $1 : "\\$1"/gser
Branch: 1 branch
BranchTrueFalseTotal
$1 eq $start || $1 eq $end ? : 28 10 38
Condition100%3/3
$1 eq $start or $1 eq $end
l!l&&r!l&&!r
26210
MC/DC100%2/2
$1 eq $start or $1 eq $end
$1 eq $start $1 eq $end
Truth table100%3/3
A or B
A= $1 eq $startB= $1 eq $end
ABresult
1X1
011
000
44 }
45  
46 58 ▶sub _encode_single ($self, $value) {
Subroutine _encode_single: called CC=1 SCAR=0.0
47 58 "'" . $self->{policy}->escape_single_quoted($value) . "'"
48 }
49  
50 95 ▶sub _encode_double ($self, $value) {
Subroutine _encode_double: called CC=1 SCAR=0.0
51 95 '"' . ($value =~ s/\\/\\\\/gr) . '"'
52 }
53  
54 463 ▶sub _normalised_value ($self, $elem) {
Subroutine _normalised_value: called CC=5 SCAR=16.1
55 463 my $class = ref $elem;
56 463 ▶ return $elem->literal if $class eq "PPI::Token::Quote::Single";
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "PPI::Token::Quote::Single" 113 350 463
57 350 ▶ return $self->_decode_double($elem->string)
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "PPI::Token::Quote::Double" 118 232 350
58 if $class eq "PPI::Token::Quote::Double";
59 232 ▶ return join "\0", $elem->literal if $class eq "PPI::Token::QuoteLike::Words";
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "PPI::Token::QuoteLike::Words" 124 108 232
60  
61 108 my ($start, $end, $raw) = $self->{policy}->parse_quote_token($elem);
62 108 ▶ return $self->_decode_q($raw, $start, $end)
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "PPI::Token::Quote::Literal" 99 9 108
63 if $class eq "PPI::Token::Quote::Literal";
64  
65 # Interpolate; a Command never reaches here because _value_preserved
66 # compares interpolating token pairs via _canonical
67 9 $self->_decode_double($raw)
68 }
69  
70 4 ▶sub _balanced ($self, $content, $start, $end) {
Subroutine _balanced: called CC=5 SCAR=16.1
71 4 my $depth = 0;
72 4 for my $char (split //, $content) {
73 101 ▶ $depth++ if $char eq $start;
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $start 3 98 101
74 101 ▶ if ($char eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $end) 4 97 101
75 4 $depth--;
76 4 ▶ return 0 if $depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth < 0 1 3 4
77 }
78 }
79 3 $depth == 0
80 }
81  
82 101 ▶sub _delimit_content ($self, $content, $start, $end) {
Subroutine _delimit_content: called CC=4 SCAR=13.9
83 101 ▶ return $content if $content !~ /[\Q$start$end\E]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /[\Q$start$end\E]/u 94 7 101
84 7 ▶ return if $content =~ /\\/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\\/u 3 4 7
85 4 ▶ return $content if $self->_balanced($content, $start, $end);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_balanced($content, $start, $end) 3 1 4
86 1 $content =~ s/([\Q$start$end\E])/\\$1/gr
87 }
88  
89 106 ▶sub _operator_replacement ($self, $elem, $op, $start, $end) {
Subroutine _operator_replacement: called CC=6 SCAR=17.9
90 106 my $class = ref $elem;
91 106 my $content;
92  
93 106 ▶ if ($class eq "PPI::Token::Quote::Single") {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "PPI::Token::Quote::Single") 5 101 106
94 5 $content = $elem->literal =~ s/([\\\Q$start$end\E])/\\$1/gr;
95 5 return "$op$start$content$end";
96 }
97  
98 101 ▶ if ($class eq "PPI::Token::Quote::Double") {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'PPI::Token::Quote::Double') { } 14 87 101
99 # An interpolating target keeps the escapes bar the quote; a literal
100 # target (q, qw) takes the fully decoded value
101 14 ▶ $content
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'qq' || $op eq 'qx' ? : 9 5 14
Condition66%2/3
$op eq "qq" or $op eq "qx"
l!l&&r!l&&!r
905
MC/DC50%1/2
$op eq "qq" or $op eq "qx"
$op eq "qq" $op eq "qx"
Truth table66%2/3
A or B
A= $op eq "qq"B= $op eq "qx"
ABresult
1X1
011
000
102 = $op eq "qq" || $op eq "qx"
103 ? $elem->string =~ s/\\"/"/gr
104 : $self->_decode_double($elem->string);
105 } else {
106 87 my ($old_start, $old_end, $raw) = $self->{policy}->parse_quote_token($elem);
107 87 $content = $raw =~ s/\\([\Q$old_start$old_end\E])/$1/gr;
108 }
109  
110 101 $content = $self->_delimit_content($content, $start, $end);
111 101 ▶ defined $content ? "$op$start$content$end" : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 98 3 101
112 }
113  
114 298 ▶sub _replacement ($self, $elem, $fix) {
Subroutine _replacement: called CC=14 SCAR=26.4
115 298 my $class = ref $elem;
116 298 my $type = $fix->{type};
117  
118 return $self->_operator_replacement($elem, $fix->{op}, $fix->{start},
119 $fix->{end})
120 298 ▶ if $type eq "operator";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "operator" 106 192 298
121  
122 192 ▶ if ($class eq "PPI::Token::Quote::Single") {
Branch: 4 branches
BranchTrueFalseTotal
if ($class eq 'PPI::Token::Quote::Single') { } 52 140 192
elsif ($class eq 'PPI::Token::Quote::Double') { } 20 120 140
elsif ($class eq 'PPI::Token::Quote::Literal') { } 76 44 120
elsif ($class eq 'PPI::Token::Quote::Interpolate') { } 43 1 44
123 52 ▶ return $self->_encode_double($elem->literal) if $type eq "double";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "double" 51 1 52
124 } elsif ($class eq "PPI::Token::Quote::Double") {
125 return $self->_encode_single($self->_decode_double($elem->string))
126 if $type eq "single"
127 20 ▶ && !$self->{policy}->has_quote_sensitive_escapes($elem->string);
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" and not $self->{"policy"}->has_quote_sensitive_escapes($elem->string) 18 2 20
Condition100%3/3
$type eq "single" and not $self->{"policy"}->has_quote_sensitive_escapes($elem->string)
!ll&&!rl&&r
1118
MC/DC100%2/2
$type eq "single" and not $self->{"policy"}->has_quote_sensitive_escapes($elem->string)
$type eq "single" not $self->{"policy"}->has_quote_sensitive_escapes($elem->string)
Truth table100%3/3
A and not(B)
A= $type eq "single"B= not $self->{"policy"}->has_quote_sensitive_escapes($elem->string)
ABresult
0X0
100
111
128 } elsif ($class eq "PPI::Token::Quote::Literal") {
129 76 my ($start, $end, $raw) = $self->{policy}->parse_quote_token($elem);
130 76 my $value = $self->_decode_q($raw, $start, $end);
131 76 ▶ return $self->_encode_double($value) if $type eq "double";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "double" 44 32 76
132 32 ▶ return $self->_encode_single($value) if $type eq "single";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" 31 1 32
133 } elsif ($class eq "PPI::Token::Quote::Interpolate") {
134 43 my ($start, $end, $raw) = $self->{policy}->parse_quote_token($elem);
135 43 ▶ return '"' . $self->_decode_delimiters($raw, $start, $end) . '"'
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "double" 32 11 43
136 if $type eq "double";
137 return $self->_encode_single($self->_decode_double($raw))
138 if $type eq "single"
139 11 ▶ && !$self->{policy}->has_quote_sensitive_escapes($raw);
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" and not $self->{"policy"}->has_quote_sensitive_escapes($raw) 9 2 11
Condition100%3/3
$type eq "single" and not $self->{"policy"}->has_quote_sensitive_escapes($raw)
!ll&&!rl&&r
119
MC/DC100%2/2
$type eq "single" and not $self->{"policy"}->has_quote_sensitive_escapes($raw)
$type eq "single" not $self->{"policy"}->has_quote_sensitive_escapes($raw)
Truth table100%3/3
A and not(B)
A= $type eq "single"B= not $self->{"policy"}->has_quote_sensitive_escapes($raw)
ABresult
0X0
100
111
140 }
141  
142 undef
143 7 }
144  
145 110 ▶sub _canonical ($self, $elem) {
Subroutine _canonical: called CC=1 SCAR=0.0
146 110 my ($start, $end, $raw) = $self->{policy}->parse_quote_token($elem);
147 110 $self->_decode_delimiters($raw, $start, $end)
148 }
149  
150 288 ▶sub _value_preserved ($self, $elem, $new_source) {
Subroutine _value_preserved: called CC=10 SCAR=23.1
151 288 my $code = "$new_source;";
152  
153 # uncoverable branch true note:code is never empty so PPI always parses
154 288 ▶ my $doc = PPI::Document->new(\$code) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = "PPI::Document"->new(\$code) or return 0 288 0 288
155 my ($token) = grep {
156 288 ▶ $_->isa("PPI::Token::Quote") || $_->isa("PPI::Token::QuoteLike")
Condition100%3/3
$_->isa('PPI::Token::Quote') || $_->isa('PPI::Token::QuoteLike')
l!l&&r!l&&!r
21375288
MC/DC100%2/2
$_->isa('PPI::Token::Quote') || $_->isa('PPI::Token::QuoteLike')
$_->isa('PPI::Token::Quote') $_->isa('PPI::Token::QuoteLike')
Truth table100%3/3
A || B
A= $_->isa('PPI::Token::Quote')B= $_->isa('PPI::Token::QuoteLike')
ABresult
1X1
011
000
157 } $doc->tokens;
158  
159 # uncoverable branch true
160 # uncoverable condition left note:every replacement contains a quote token
161 # uncoverable condition right note:PPI serialisation is faithful
162 288 ▶ return 0 unless $token && $doc->serialize eq $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $token and $doc->serialize eq $code 0 288 288
Condition100%1/3
$token and $doc->serialize eq $code
!ll&&!rl&&r
00288
MC/DC100%0/2
$token and $doc->serialize eq $code
$token $doc->serialize eq $code
Truth table100%1/3
A and B
A= $tokenB= $doc->serialize eq $code
ABresult
0X0
100
111
163 288 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::QuoteLike::Command") or $token->isa("PPI::Token::QuoteLike::Command")) 13 275 288
Condition100%3/3
$elem->isa("PPI::Token::QuoteLike::Command") or $token->isa("PPI::Token::QuoteLike::Command")
l!l&&r!l&&!r
121275
MC/DC100%2/2
$elem->isa("PPI::Token::QuoteLike::Command") or $token->isa("PPI::Token::QuoteLike::Command")
$elem->isa("PPI::Token::QuoteLike::Command") $token->isa("PPI::Token::QuoteLike::Command")
Truth table100%3/3
A or B
A= $elem->isa("PPI::Token::QuoteLike::Command")B= $token->isa("PPI::Token::QuoteLike::Command")
ABresult
1X1
011
000
164 $elem->isa("PPI::Token::QuoteLike::Command")
165 || $token->isa("PPI::Token::QuoteLike::Command")
166 ) {
167 13 ▶ return 0 if $self->_interpolates($elem) != $self->_interpolates($token);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_interpolates($elem) != $self->_interpolates($token) 2 11 13
168 11 return $self->_canonical($token) eq $self->_canonical($elem);
169 }
170 return $self->_canonical($token) eq $self->_canonical($elem)
171 275 ▶ if $Interpolating{ ref $token } && $Interpolating{ ref $elem };
Branch: 1 branch
BranchTrueFalseTotal
if $Interpolating{ref $token} and $Interpolating{ref $elem} 44 231 275
Condition100%3/3
$Interpolating{ref $token} and $Interpolating{ref $elem}
!ll&&!rl&&r
1369544
MC/DC100%2/2
$Interpolating{ref $token} and $Interpolating{ref $elem}
$Interpolating{ref $token} $Interpolating{ref $elem}
Truth table100%3/3
A and B
A= $Interpolating{ref $token}B= $Interpolating{ref $elem}
ABresult
0X0
100
111
172 231 $self->_normalised_value($token) eq $self->_normalised_value($elem)
173 }
174  
175 298 ▶sub _apply_replacement ($self, $elem, $fix) {
Subroutine _apply_replacement: called CC=3 SCAR=11.0
176 298 my $new = $self->_replacement($elem, $fix);
177 298 ▶ return 0 unless defined $new && $self->_value_preserved($elem, $new);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new and $self->_value_preserved($elem, $new) 13 285 298
Condition100%3/3
defined $new and $self->_value_preserved($elem, $new)
!ll&&!rl&&r
103285
MC/DC100%2/2
defined $new and $self->_value_preserved($elem, $new)
defined $new $self->_value_preserved($elem, $new)
Truth table100%3/3
A and B
A= defined $newB= $self->_value_preserved($elem, $new)
ABresult
0X0
100
111
178 285 $elem->set_content($new);
179 285 1
180 }
181  
182 15 ▶sub _remove_include_parens ($self, $elem) {
Subroutine _remove_include_parens: called CC=8 SCAR=20.8
183 15 my $list = $self->{policy}->statement_level_list($elem);
184 15 ▶ return 0 unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 3 12 15
185  
186 12 my @kids = $list->children;
187 12 ▶ while (@kids && !$kids[0]->significant) { (shift @kids)->delete }
Branch: 1 branch
BranchTrueFalseTotal
if (@kids and not $kids[0]->significant) 5 12 17
Condition100%3/3
@kids and not $kids[0]->significant
!ll&&!rl&&r
2105
MC/DC100%2/2
@kids and not $kids[0]->significant
@kids not $kids[0]->significant
Truth table100%3/3
A and not(B)
A= @kidsB= not $kids[0]->significant
ABresult
0X0
100
111
188 12 ▶ while (@kids && !$kids[-1]->significant) { (pop @kids)->delete }
Branch: 1 branch
BranchTrueFalseTotal
if (@kids and not $kids[-1]->significant) 4 12 16
Condition100%3/3
@kids and not $kids[-1]->significant
!ll&&!rl&&r
2104
MC/DC100%2/2
@kids and not $kids[-1]->significant
@kids not $kids[-1]->significant
Truth table100%3/3
A and not(B)
A= @kidsB= not $kids[-1]->significant
ABresult
0X0
100
111
189 12 ▶ unless (@kids) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kids) 2 10 12
190 2 my $prev = $list->previous_sibling;
191 2 ▶ $prev->delete unless $prev->significant;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->significant 1 1 2
192 }
193 12 $list->start->set_content("");
194 12 $list->finish->set_content("");
195 12 1
196 }
197  
198 66 ▶sub _include_argument_span ($self, $elem) {
Subroutine _include_argument_span: called CC=7 SCAR=19.5
199 # No module means no import arguments; ->arguments dies on a bare "use"
200 66 ▶ return unless $elem->module;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->module 1 65 66
201 65 ▶ my @args = $elem->arguments or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@args) = $elem->arguments or return 64 1 65
202 64 my (@span, $in);
203 64 for my $child ($elem->children) {
204 452 ▶ $in ||= $child == $args[0];
Condition100%3/3
$in ||= $child == $args[0]
l!l&&r!l&&!r
12264266
MC/DC100%2/2
$in ||= $child == $args[0]
$in $child == $args[0]
Truth table100%3/3
A ||= B
A= $inB= $child == $args[0]
ABresult
1X1
011
000
205 452 ▶ push @span, $child if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 186 266 452
206 452 ▶ last if $child == $args[-1];
Branch: 1 branch
BranchTrueFalseTotal
if $child == $args[-1] 64 388 452
207 }
208 @span
209 64 }
210  
211 64 ▶sub _span_has_comment ($self, @span) {
Subroutine _span_has_comment: called CC=1 SCAR=0.0
212 183 ▶ any {
Subroutine __ANON__: called CC=3 SCAR=11.0
213 183 ▶ $_->isa("PPI::Token::Comment")
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("PPI::Token::Comment") or $_->isa("PPI::Node") and $_->find_any("PPI::Token::Comment") 1 182 183
Condition100%6/6
$_->isa('PPI::Token::Comment') || $_->isa('PPI::Node') && $_->find_any('PPI::Token::Comment')
l!l&&r!l&&!r
11181
$_->isa("PPI::Node") and $_->find_any("PPI::Token::Comment")
!ll&&!rl&&r
17651
MC/DC100%3/3
$_->isa('PPI::Token::Comment') || $_->isa('PPI::Node') && $_->find_any('PPI::Token::Comment')
$_->isa('PPI::Token::Comment') $_->isa("PPI::Node") $_->find_any("PPI::Token::Comment")
Truth table100%4/4
A || B and C
A= $_->isa('PPI::Token::Comment')B= $_->isa("PPI::Node")C= $_->find_any("PPI::Token::Comment")
ABCresult
1XX1
0111
00X0
0100
214 || ($_->isa("PPI::Node") && $_->find_any("PPI::Token::Comment"))
215 } @span
216 64 }
217  
218 83 ▶sub _fix_include ($self, $elem, $fix) {
Subroutine _fix_include: called CC=13 SCAR=25.6
219 return $self->_remove_include_parens($elem)
220 83 ▶ if $fix->{type} eq "remove_parens";
Branch: 1 branch
BranchTrueFalseTotal
if $fix->{"type"} eq "remove_parens" 15 68 83
221 68 ▶ return 0 unless $fix->{type} eq "operator" && $fix->{op} eq "qw";
Branch: 1 branch
BranchTrueFalseTotal
unless $fix->{"type"} eq "operator" and $fix->{"op"} eq "qw" 2 66 68
Condition100%3/3
$fix->{"type"} eq "operator" and $fix->{"op"} eq "qw"
!ll&&!rl&&r
1166
MC/DC100%2/2
$fix->{"type"} eq "operator" and $fix->{"op"} eq "qw"
$fix->{"type"} eq "operator" $fix->{"op"} eq "qw"
Truth table100%3/3
A and B
A= $fix->{"type"} eq "operator"B= $fix->{"op"} eq "qw"
ABresult
0X0
100
111
222  
223 66 my @span = $self->_include_argument_span($elem);
224 66 ▶ return 0 unless @span;
Branch: 1 branch
BranchTrueFalseTotal
unless @span 2 64 66
225  
226 64 my @words;
227 64 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_span_has_comment(@span) and $self->{"policy"}->collect_qw_words(\@words, @span) and &all(sub { $self->{"policy"}->qw_word_ok($_); } , @words)) 55 9 64
Condition100%6/6
not $self->_span_has_comment(@span) and $self->{"policy"}->collect_qw_words(\@words, @span) and &all(sub { $self->{"policy"}->qw_word_ok($_); } , @words)
!ll&&!rl&&r
8155
not $self->_span_has_comment(@span) and $self->{"policy"}->collect_qw_words(\@words, @span)
!ll&&!rl&&r
2656
MC/DC100%3/3
not $self->_span_has_comment(@span) and $self->{"policy"}->collect_qw_words(\@words, @span) and &all(sub { $self->{"policy"}->qw_word_ok($_); } , @words)
not $self->_span_has_comment(@span) $self->{"policy"}->collect_qw_words(\@words, @span) &all(sub { $self->{"policy"}->qw_word_ok($_); } , @words)
Truth table100%4/4
not(A) and B and C
A= not $self->_span_has_comment(@span)B= $self->{"policy"}->collect_qw_words(\@words, @span)C= &all(sub { $self->{"policy"}->qw_word_ok($_); } , @words)
ABCresult
0XX0
10X0
1100
1111
228 !$self->_span_has_comment(@span)
229 && $self->{policy}->collect_qw_words(\@words, @span)
230 91 ▶ && all { $self->{policy}->qw_word_ok($_) } @words
Subroutine __ANON__: called CC=1 SCAR=0.0
231 ) {
232 55 $span[0]->insert_before(PPI::Token->new("qw( @words )"));
233 55 $_->delete for @span;
234 55 return 1;
235 }
236  
237 9 ▶ my $qw_tokens = $elem->find("PPI::Token::QuoteLike::Words") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $qw_tokens = $elem->find("PPI::Token::QuoteLike::Words") or return 0 6 3 9
238 6 my $applied = 0;
239 6 for my $token (@$qw_tokens) {
240 8 ▶ next if $token->content =~ /\Aqw\s*\Q$fix->{start}\E/;
Branch: 1 branch
BranchTrueFalseTotal
if $token->content =~ /\Aqw\s*\Q$fix->{"start"}\E/u 1 7 8
241 7 ▶ $applied = 1 if $self->_apply_replacement($token, $fix);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_apply_replacement($token, $fix) 6 1 7
242 }
243 $applied
244 6 }
245  
246 374 ▶sub _apply_fix ($self, $elem, $fix) {
Subroutine _apply_fix: called CC=2 SCAR=6.9
247 374 ▶ $elem->isa("PPI::Statement::Include")
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa('PPI::Statement::Include') ? : 83 291 374
248 ? $self->_fix_include($elem, $fix)
249 : $self->_apply_replacement($elem, $fix)
250 }
251  
252 9326 ▶sub _in_range ($self, $elem, $lines) {
Subroutine _in_range: called CC=3 SCAR=11.0
253 9326 ▶ return 1 unless $lines;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines 9005 321 9326
254 321 my $line = $elem->line_number;
255 321 ▶ $line >= $lines->[0] && $line <= $lines->[1]
Branch: 1 branch
BranchTrueFalseTotal
$line >= $lines->[0] and $line <= $lines->[1] 219 102 321
256 }
257  
258 374 ▶sub _shift_range ($self, $lines, $delta) {
Subroutine _shift_range: called CC=2 SCAR=6.9
259 374 ▶ $lines->[1] += $delta if $lines;
Branch: 1 branch
BranchTrueFalseTotal
if $lines 7 367 374
260 }
261  
262 1029 ▶sub _fix_once ($self, $source, $lines) {
Subroutine _fix_once: called CC=6 SCAR=17.9
263 1029 ▶ my $doc = PPI::Document->new(\$source) or return $source;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = "PPI::Document"->new(\$source) or return $source 1028 1 1029
264  
265 1028 my @fixes;
266 $doc->find(
267 9326 ▶ sub ($top, $elem) {
Subroutine __ANON__: called CC=6 SCAR=17.9
268 9326 ▶ return 0 unless $self->_in_range($elem, $lines);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_range($elem, $lines) 182 9144 9326
269 9144 my ($violation) = $self->{policy}->violates($elem, $doc);
270 9144 ▶ return 0 unless $violation;
Branch: 1 branch
BranchTrueFalseTotal
unless $violation 8767 377 9144
271 my $fix
272 = $violation->can("fix")
273 ? $violation->fix
274 377 ▶ : $self->{policy}->fix_data($violation->description);
Branch: 1 branch
BranchTrueFalseTotal
$violation->can('fix') ? : 350 27 377
275 377 ▶ if ($fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix) { } 374 3 377
276 374 push @fixes, [$elem, $fix];
277 } else {
278 3 my $msg = $violation->description;
279 warn "Perl::Critic::PJCJ::Fixer: no fix mapping for '$msg' at line "
280 . $elem->line_number . "\n"
281 3 ▶ unless $self->{warned}{$msg}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"warned"}{$msg}++ 2 1 3
282 }
283 377 0
284 }
285 1028 );
286 1028 ▶ return $source unless @fixes;
Branch: 1 branch
BranchTrueFalseTotal
unless @fixes 659 369 1028
287 369 my $applied = 0;
288 369 for my $entry (@fixes) {
289 374 my ($elem, $fix) = @$entry;
290 374 my $before = $elem->content =~ tr/\n//;
291 374 ▶ $applied = 1 if $self->_apply_fix($elem, $fix);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_apply_fix($elem, $fix) 351 23 374
292 374 $self->_shift_range($lines, ($elem->content =~ tr/\n//) - $before);
293 }
294 369 ▶ return $source unless $applied;
Branch: 1 branch
BranchTrueFalseTotal
unless $applied 23 346 369
295  
296 346 $doc->serialize
297 }
298  
299 344 ▶sub _restore_crlf ($self, $source, $fixed) {
Subroutine _restore_crlf: called CC=3 SCAR=11.0
300 344 ▶ return $fixed unless $source =~ /\r\n/;
Branch: 1 branch
BranchTrueFalseTotal
unless $source =~ /\r\n/u 341 3 344
301 3 ▶ return $fixed if $source =~ /\r(?!\n)|(?<!\r)\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $source =~ /\r(?!\n)|(?<!\r)\n/u 1 2 3
302 2 $fixed =~ s/\n/\r\n/gr
303 }
304  
305 686 ▶sub _finish ($self, $source, $current) {
Subroutine _finish: called CC=2 SCAR=6.9
306 686 ▶ return $current if $current eq $source;
Branch: 1 branch
BranchTrueFalseTotal
if $current eq $source 342 344 686
307 344 $self->_restore_crlf($source, $current)
308 }
309  
310 686 ▶sub fix ($self, $source, %opts) {
Subroutine fix: called CC=5 SCAR=16.1
Pod documented
311 686 ▶ my $lines = $opts{lines} ? [$opts{lines}->@*] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'lines'} ? : 7 679 686
312 686 my $current = $source;
313 686 $self->{warned} = {};
314 686 my %seen;
315 686 for (1 .. $Max_passes) {
316 1039 $seen{$current} = 1;
317 1039 my $next = $self->_fix_once($current, $lines);
318 1039 ▶ return $self->_finish($source, $next) if $next eq $current;
Branch: 1 branch
BranchTrueFalseTotal
if $next eq $current 684 355 1039
319 355 ▶ if ($seen{$next}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$next}) 1 354 355
320 1 warn "Perl::Critic::PJCJ::Fixer: fixes oscillate without "
321 . "converging; the result may still violate the policy\n";
322 1 return $self->_finish($source, $next);
323 }
324 354 $current = $next;
325 }
326 1 warn "Perl::Critic::PJCJ::Fixer: fixes still changing after "
327 . "$Max_passes passes; giving up\n";
328 1 $self->_finish($source, $current)
329 }
330  
331 42 "
332 A painter on the shore
333 Imagined all the world
334 Within the snowflake on his palm
335 "
336  
337 __END__
« utils/perl-hook ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/RequireConsistentQuoting.pm »