PerlNitpick/Rule/QuoteSimpleStringWithSingleQuote.pm

filter: Statementstmt 72.7% 24 / 33 Branchbran 37.5% 3 / 8 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 53.7% 36 / 67 CC 14 SCAR 33.5
CC
14
Cov
57%
rewrite
38.5
__ANON__
1.0
__ANON__
1.0
CRAP 28.6
↑ Summary PerlNitpick/Rule/RewriteWithAssignmentOperators.pm »
1 package App::PerlNitpick::Rule::QuoteSimpleStringWithSingleQuote;
2 # ABSTRACT: Re-quote strings with single quotes ('') if they look "simple"
3  
4 =encoding UTF-8
5  
6 =head1 DESCRIPTION
7  
8 This nitpicking rule re-quote simple strings with single-quote.
9 For example, C<"coffee"> becomes C<'coffee'>.
10  
11 =head2 Simple strings ?
12  
13 Simple strings is a subset of strings that satisfies all of these
14 constraints:
15  
16 - is a string literal (not variable)
17 - is quoted with: q, qq, double-quote ("), or single-quote (')
18 - is a single-line string
19 - has no interpolations inside
20 - has no quote characters inside
21 - has no sigil characters inside
22 - has no metachar
23  
24 For example, here's a short list of simple strings:
25  
26 - q<肆拾貳>
27 - qq{Latte Art}
28 - "Spring"
29 - "Error: insufficient vespene gas"
30  
31 While here are some counter examples:
32  
33 - "john.smith@example.com"
34 - "'s-Gravenhage"
35 - 'Look at this @{[ longmess() ]}'
36 - q<The symbol $ is also known as dollor sign.>
37  
38 Roughly speaking, given a string, if you can re-quote it with single-quote (')
39 without changing its value -- then it is a simple string.
40  
41 =cut
42  
43 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 ▶sub rewrite {
Subroutine rewrite: called CC=14 SCAR=36.5
Pod undocumented
47 2 my ($self, $doc) = @_;
48  
49 2 ▶ for my $tok (@{$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Double') }) || []}) {
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Double'); } ) || []
l!l
20
MC/DC0%0/1
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Double'); } ) || []
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Double'); } )
Truth table50%1/2
A
A= $doc->find(sub { $_[1]->isa('PPI::Token::Quote::Double'); } )
Aresult
11
00
50 2 ▶ next if $tok->interpolations;
Branch: 1 branch
BranchTrueFalseTotal
if $tok->interpolations 0 2 2
51 2 my $value = $tok->string;
52 2 ▶ next if index($value, "\n") > 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($value, "\n") > 0 1 1 2
53 1 $tok->simplify;
54 }
55  
56 2 my @todo;
57 2 ▶ for my $tok (@{ $doc->find(sub { $_[1]->isa('PPI::Token::Quote::Interpolate') }) || []}) {
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Interpolate'); } ) || []
l!l
02
MC/DC0%0/1
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Interpolate'); } ) || []
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Interpolate'); } )
Truth table50%1/2
A
A= $doc->find(sub { $_[1]->isa('PPI::Token::Quote::Interpolate'); } )
Aresult
11
00
58 0 my $value = $tok->string;
59 0 ▶ next if $value =~ /[\\\$@%\'\"]/ || index($value, "\n") > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\\\$\@%\'\"]/ or index($value, "\n") > 0 0 0 0
Condition0%0/3
$value =~ /[\\\$\@%\'\"]/ or index($value, "\n") > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$value =~ /[\\\$\@%\'\"]/ or index($value, "\n") > 0
$value =~ /[\\\$\@%\'\"]/ index($value, "\n") > 0
Truth table0%0/3
A or B
A= $value =~ /[\\\$\@%\'\"]/B= index($value, "\n") > 0
ABresult
1X1
011
000
60 0 push @todo, $tok;
61 }
62  
63 2 ▶ for my $tok (@{ $doc->find(sub { $_[1]->isa('PPI::Token::Quote::Literal') }) || []}) {
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Literal'); } ) || []
l!l
02
MC/DC0%0/1
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Literal'); } ) || []
$doc->find(sub { $_[1]->isa('PPI::Token::Quote::Literal'); } )
Truth table50%1/2
A
A= $doc->find(sub { $_[1]->isa('PPI::Token::Quote::Literal'); } )
Aresult
11
00
64 0 my $value = $tok->string;
65 0 ▶ next if $value =~ /\'/ || index($value, "\n") > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\'/ or index($value, "\n") > 0 0 0 0
Condition0%0/3
$value =~ /\'/ or index($value, "\n") > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$value =~ /\'/ or index($value, "\n") > 0
$value =~ /\'/ index($value, "\n") > 0
Truth table0%0/3
A or B
A= $value =~ /\'/B= index($value, "\n") > 0
ABresult
1X1
011
000
66 0 push @todo, $tok;
67 }
68  
69 2 for my $tok (@todo) {
70 0 my $value = $tok->string;
71 # I probably know what I am doing.
72 0 $tok->{content} = "'" . $tok->string . "'";
73 0 bless $tok, 'PPI::Token::Quote::Single';
74 }
75  
76 2 return $doc;
77 }
78  
79 1 1;
↑ Summary PerlNitpick/Rule/RewriteWithAssignmentOperators.pm »