Policy/Miscellanea/TextDomainPlaceholders.pm

filter: Statementstmt 97.6% 126 / 129 Branchbran 95.5% 65 / 68 Conditioncond 68.7% 33 / 48 MC/DCmcdc 48.3% 15 / 31 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 87.3% 256 / 293 CC 53 SCAR 28.2
CC
53
Cov
91%
violates
24.3
_arg_string
23.4
__ANON__
4.0
CRAP 16.8
« Policy/ValuesAndExpressions/NotWithCompare.pm ↑ Summary Policy/ValuesAndExpressions/ConstantBeforeLt.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # This file is part of Perl-Critic-Pulp.
4  
5 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders;
20 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Perl::Critic::Utils qw(is_function_call
Subroutine BEGIN: called CC=1 SCAR=0.0
26 parse_arg_list
27 40 interpolate);
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32 40 our $VERSION = 100;
33  
34 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 40 ▶use constant applies_to => 'PPI::Token::Word';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 40 my %funcs = (__x => 1,
40 __nx => 1,
41 __xn => 1,
42  
43 __px => 1,
44 __npx => 1);
45  
46 83 ▶sub violates {
Subroutine violates: called CC=24 SCAR=31.9
Pod documented
47 83 my ($self, $elem, $document) = @_;
48  
49 83 my $funcname = $elem->content;
50 83 $funcname =~ s/^Locale::TextDomain:://;
51 83 ▶ $funcs{$funcname} || return;
Branch: 1 branch
BranchTrueFalseTotal
$funcs{$funcname} or return 49 34 83
52 ### TextDomainPlaceholders: $elem->content
53  
54 49 ▶ is_function_call($elem) || return;
Branch: 1 branch
BranchTrueFalseTotal
is_function_call($elem) or return 42 7 49
55  
56 42 my @violations;
57  
58 # The arg crunching bits assume one parsed expression results in one arg,
59 # which is not true if the expressions are an array, a hash, or a function
60 # call returning multiple values. The one-arg-one-value assumption is
61 # reasonable on the whole though.
62 #
63 # In the worst case you'd have to take any function call value part like
64 # "foo => FOO()" to perhaps return multiple values -- which would
65 # completely defeat testing of normal cases, so don't want to do that.
66 #
67 # ENHANCE-ME: One bit that could be done though is to recognise a %foo arg
68 # as giving an even number of values, so keyword checking could continue
69 # past it.
70  
71 # each element of @args is an arrayref containing PPI elements making up
72 # the arg
73 42 my @args = parse_arg_list ($elem);
74 ### got total arg count: scalar(@args)
75  
76 42 ▶ if ($funcname =~ /p/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($funcname =~ /p/) 4 38 42
77 # msgctxt context arg to __p, __npx
78 4 shift @args;
79 }
80  
81 # one format to __x, two to __nx and other "n" funcs
82 42 ▶ my @format_args = splice @args, 0, ($funcname =~ /n/ ? 2 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$funcname =~ /n/ ? : 17 25 42
83  
84 42 ▶ if ($funcname =~ /n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($funcname =~ /n/) 17 25 42
85 # count arg to __nx and other "n" funcs
86 17 my $count_arg = shift @args;
87 17 ▶ if (! $count_arg
Branch: 1 branch
BranchTrueFalseTotal
if (not $count_arg or do { () = _arg_word_or_string($count_arg, $document); $str =~ /^[[:alpha:]_]\w*$/ and not $any_vars }) 8 9 17
Condition100%3/3
not $count_arg or do { () = _arg_word_or_string($count_arg, $document); $str =~ /^[[:alpha:]_]\w*$/ and not $any_vars }
l!l&&r!l&&!r
359
MC/DC100%2/2
not $count_arg or do { () = _arg_word_or_string($count_arg, $document); $str =~ /^[[:alpha:]_]\w*$/ and not $any_vars }
not $count_arg do { () = _arg_word_or_string($count_arg, $document); $str =~ /^[[:alpha:]_]\w*$/ and not $any_vars }
Truth table100%3/3
not(A) or B
A= not $count_argB= do { () = _arg_word_or_string($count_arg, $document); $str =~ /^[[:alpha:]_]\w*$/ and not $any_vars }
ABresult
1X1
011
000
88 || do {
89 # if it looks like a keyword symbol foo=> or 'foo' etc
90 14 my ($str, $any_vars) = _arg_word_or_string ($count_arg, $document);
91 14 ▶ ($str =~ /^[[:alpha:]_]\w*$/ && ! $any_vars)
Condition66%2/3
$str =~ /^[[:alpha:]_]\w*$/ && !$any_vars
!ll&&!rl&&r
905
MC/DC50%1/2
$str =~ /^[[:alpha:]_]\w*$/ && !$any_vars
$str =~ /^[[:alpha:]_]\w*$/ !$any_vars
Truth table66%2/3
A && not(B)
A= $str =~ /^[[:alpha:]_]\w*$/B= !$any_vars
ABresult
0X0
100
111
92 }) {
93 8 ▶ push @violations, $self->violation
Condition66%2/3
$count_arg->[0] || $elem
l!l&&r!l&&!r
530
MC/DC0%0/2
$count_arg->[0] || $elem
$count_arg->[0] $elem
Truth table66%2/3
A || B
A= $count_arg->[0]B= $elem
ABresult
1X1
011
000
94 ("Probably missing 'count' argument to $funcname",
95 '',
96 $count_arg->[0] || $elem);
97 }
98 }
99  
100 ### got data arg count: scalar(@args)
101  
102 42 my $args_any_vars = 0;
103 42 my %arg_keys;
104 42 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 38 42 80
105 38 my $arg = shift @args;
106 38 my ($str, $any_vars) = _arg_word_or_string ($arg, $document);
107 38 ▶ $args_any_vars ||= $any_vars;
Condition100%3/3
$args_any_vars ||= $any_vars
l!l&&r!l&&!r
2630
MC/DC100%2/2
$args_any_vars ||= $any_vars
$args_any_vars $any_vars
Truth table100%3/3
A ||= B
A= $args_any_varsB= $any_vars
ABresult
1X1
011
000
108 ### arg: @$arg
109 ### $str
110 ### $any_vars
111 38 ▶ if (! $any_vars) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($any_vars) 31 7 38
112 31 $arg_keys{$str} = $arg;
113 }
114 38 shift @args; # value part
115 }
116  
117 42 my %format_keys;
118 my $format_any_vars;
119  
120 42 foreach my $format_arg (@format_args) {
121 59 my ($format_str, $any_vars) = _arg_string ($format_arg, $document);
122 59 ▶ $format_any_vars ||= $any_vars;
Condition66%2/3
$format_any_vars ||= $any_vars
l!l&&r!l&&!r
0356
MC/DC50%1/2
$format_any_vars ||= $any_vars
$format_any_vars $any_vars
Truth table66%2/3
A ||= B
A= $format_any_varsB= $any_vars
ABresult
1X1
011
000
123  
124 59 ▶ while ($format_str =~ /\{(\w+)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format_str =~ /\{(\w+)\}/g) 50 59 109
125 50 my $format_key = $1;
126 ### $format_key
127 50 $format_keys{$format_key} = 1;
128  
129 50 ▶ if (! $args_any_vars && ! exists $arg_keys{$format_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args_any_vars or exists $arg_keys{$format_key}) 21 29 50
Condition100%3/3
$args_any_vars or exists $arg_keys{$format_key}
l!l&&r!l&&!r
82121
MC/DC100%2/2
$args_any_vars or exists $arg_keys{$format_key}
$args_any_vars exists $arg_keys{$format_key}
Truth table100%3/3
A or B
A= $args_any_varsB= exists $arg_keys{$format_key}
ABresult
1X1
011
000
130 21 ▶ push @violations, $self->violation
Condition33%1/3
$format_arg->[0] || $elem
l!l&&r!l&&!r
2100
MC/DC0%0/2
$format_arg->[0] || $elem
$format_arg->[0] $elem
Truth table33%1/3
A || B
A= $format_arg->[0]B= $elem
ABresult
1X1
011
000
131 ("Format key '$format_key' not in arg list",
132 '',
133 $format_arg->[0] || $elem);
134 }
135 }
136 }
137  
138 42 ▶ if (! $format_any_vars) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format_any_vars) 39 3 42
139 39 foreach my $arg_key (keys %arg_keys) {
140 28 ▶ if (! exists $format_keys{$arg_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $format_keys{$arg_key}) 13 15 28
141 13 my $arg = $arg_keys{$arg_key};
142 13 ▶ push @violations, $self->violation
Branch: 1 branch
BranchTrueFalseTotal
@format_args == 1 ? : 8 5 13
Condition33%1/3
$arg->[0] || $elem
l!l&&r!l&&!r
1300
MC/DC0%0/2
$arg->[0] || $elem
$arg->[0] $elem
Truth table33%1/3
A || B
A= $arg->[0]B= $elem
ABresult
1X1
011
000
143 ("Argument key '$arg_key' not used by format"
144 . (@format_args == 1 ? '' : 's'),
145 '',
146 $arg->[0] || $elem);
147 }
148 }
149 }
150 ### total violation count: scalar(@violations)
151  
152 42 return @violations;
153 }
154  
155 52 ▶sub _arg_word_or_string {
Subroutine _arg_word_or_string: called CC=3 SCAR=11.0
156 52 my ($arg, $document) = @_;
157 52 ▶ if (@$arg == 1 && $arg->[0]->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$arg == 1 and $arg->[0]->isa('PPI::Token::Word')) { } 27 25 52
Condition66%2/3
@$arg == 1 and $arg->[0]->isa("PPI::Token::Word")
!ll&&!rl&&r
02527
MC/DC50%1/2
@$arg == 1 and $arg->[0]->isa("PPI::Token::Word")
@$arg == 1 $arg->[0]->isa("PPI::Token::Word")
Truth table66%2/3
A and B
A= @$arg == 1B= $arg->[0]->isa("PPI::Token::Word")
ABresult
0X0
100
111
158 27 return ("$arg->[0]", 0);
159 } else {
160 25 return _arg_string ($arg, $document);
161 }
162 }
163  
164 # $arg is an arrayref of PPI::Element which are an argument
165 # if it's a constant string or "." concat of such then
166 # return ($str, $any_vars) where $str is the string content
167 # and $any_vars is true if there's any variables to be interpolated in $str
168 #
169 190 ▶sub _arg_string {
Subroutine _arg_string: called CC=22 SCAR=31.5
170 190 my ($arg, $document) = @_;
171 ### _arg_string() ...
172  
173 190 my @elems = @$arg;
174 190 my $ret = '';
175 190 my $any_vars = 0;
176  
177 190 ▶ while (@elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems) 202 0 202
178 202 my $elem = shift @elems;
179  
180 202 ▶ if ($elem->isa('PPI::Token::Quote')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Quote')) { } 85 117 202
elsif ($elem->isa('PPI::Token::HereDoc')) { } 4 113 117
elsif ($elem->isa('PPI::Token::Number')) { } 10 103 113
elsif ($elem->isa('PPI::Token::Word')) { } 88 15 103
181 85 my $str = $elem->string;
182 85 ▶ if ($elem->isa('PPI::Token::Quote::Double')
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Quote::Double") or $elem->isa("PPI::Token::Quote::Interpolate")) 22 63 85
Condition100%3/3
$elem->isa("PPI::Token::Quote::Double") or $elem->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
21163
MC/DC100%2/2
$elem->isa("PPI::Token::Quote::Double") or $elem->isa("PPI::Token::Quote::Interpolate")
$elem->isa("PPI::Token::Quote::Double") $elem->isa("PPI::Token::Quote::Interpolate")
Truth table100%3/3
A or B
A= $elem->isa("PPI::Token::Quote::Double")B= $elem->isa("PPI::Token::Quote::Interpolate")
ABresult
1X1
011
000
183 || $elem->isa('PPI::Token::Quote::Interpolate')) {
184 # ENHANCE-ME: use $arg->interpolations() when available also on
185 # PPI::Token::Quote::Interpolate
186 22 ▶ $any_vars ||= _string_any_vars ($str);
Condition66%2/3
$any_vars ||= _string_any_vars($str)
l!l&&r!l&&!r
0418
MC/DC50%1/2
$any_vars ||= _string_any_vars($str)
$any_vars _string_any_vars($str)
Truth table66%2/3
A ||= B
A= $any_varsB= _string_any_vars($str)
ABresult
1X1
011
000
187 }
188 85 $ret .= $str;
189  
190 } elsif ($elem->isa('PPI::Token::HereDoc')) {
191 4 my $str = join('',$elem->heredoc);
192 4 ▶ if ($elem =~ /`$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem =~ /`$/) { } 0 4 4
elsif (not $elem =~ /'$/) { } 3 1 4
193 0 $str = ' '; # no idea what running backticks might produce
194 0 $any_vars = 1;
195 } elsif ($elem !~ /'$/) {
196 # explicit "HERE" or default HERE expand vars
197 3 ▶ $any_vars ||= _string_any_vars ($str);
Condition66%2/3
$any_vars ||= _string_any_vars($str)
l!l&&r!l&&!r
012
MC/DC50%1/2
$any_vars ||= _string_any_vars($str)
$any_vars _string_any_vars($str)
Truth table66%2/3
A ||= B
A= $any_varsB= _string_any_vars($str)
ABresult
1X1
011
000
198 }
199 4 $ret .= $str;
200  
201 } elsif ($elem->isa('PPI::Token::Number')) {
202 ### number can work like a constant string ...
203 10 $ret .= $elem->content;
204  
205 } elsif ($elem->isa('PPI::Token::Word')) {
206 ### word ...
207 88 my $next;
208 88 ▶ if ($elem eq '__PACKAGE__') {
Branch: 4 branches
BranchTrueFalseTotal
if ($elem eq '__PACKAGE__') { } 8 80 88
elsif ($elem eq '__LINE__') { } 8 72 80
elsif ($elem eq '__FILE__') { } 4 68 72
elsif ($next = $elem->snext_sibling and $next->isa('PPI::Token::Operator') and $next eq '=>') { } 64 4 68
209 8 $ret .= _elem_package_name($elem);
210  
211 } elsif ($elem eq '__LINE__') {
212 ### logical line: $elem->location->[3]
213 8 $ret .= $elem->location->[3]; # logical line using any #line directives
214  
215 } elsif ($elem eq '__FILE__') {
216 4 my $filename = _elem_logical_filename($elem,$document);
217 4 ▶ if (! defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 2 2 4
218 2 $filename = 'unknown-filename.pl';
219 }
220 ### $filename
221 4 $ret .= $filename;
222  
223 ▶ } elsif (($next = $elem->snext_sibling)
Condition66%4/6
$next = $elem->snext_sibling and $next->isa("PPI::Token::Operator") and $next eq "=>"
!ll&&!rl&&r
4064
$next = $elem->snext_sibling and $next->isa("PPI::Token::Operator")
!ll&&!rl&&r
0464
MC/DC33%1/3
$next = $elem->snext_sibling and $next->isa("PPI::Token::Operator") and $next eq "=>"
$next = $elem->snext_sibling $next->isa("PPI::Token::Operator") $next eq "=>"
Truth table50%2/4
A and B and C
A= $next = $elem->snext_siblingB= $next->isa("PPI::Token::Operator")C= $next eq "=>"
ABCresult
0XX0
10X0
1100
1111
224 && $next->isa('PPI::Token::Operator')
225 && $next eq '=>') {
226 ### word quoted by => ...
227 64 $ret .= $elem->content;
228 64 last;
229 } else {
230 ### some function call or something ...
231 4 return ('', 2);
232 }
233  
234 } else {
235 ### some variable or expression or something ...
236 15 return ('', 2);
237 }
238  
239  
240 119 ▶ if (! @elems) { last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@elems) 107 12 119
241 12 my $op = shift @elems;
242 12 ▶ if (! ($op->isa('PPI::Token::Operator') && $op eq '.')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->isa("PPI::Token::Operator") and $op eq ".") 0 12 12
Condition33%1/3
$op->isa("PPI::Token::Operator") and $op eq "."
!ll&&!rl&&r
0012
MC/DC0%0/2
$op->isa("PPI::Token::Operator") and $op eq "."
$op->isa("PPI::Token::Operator") $op eq "."
Truth table33%1/3
A and B
A= $op->isa("PPI::Token::Operator")B= $op eq "."
ABresult
0X0
100
111
243 # something other than "." concat
244 0 return ('', 2);
245 }
246 }
247 171 return ($ret, $any_vars);
248 }
249  
250 # $str is the contents of a "" or qq{} string
251 # return true if it has any $ or @ interpolation forms
252 42 ▶sub _string_any_vars {
Subroutine _string_any_vars: called CC=1 SCAR=0.0
253 42 my ($str) = @_;
254 42 return ($str =~ /(^|[^\\])(\\\\)*[\$@]/);
255 }
256  
257 # $elem is a PPI::Element
258 # Return the name (a string) of its containing package, or "main" if not
259 # under any package statement.
260 #
261 8 ▶sub _elem_package_name {
Subroutine _elem_package_name: called CC=3 SCAR=11.0
262 8 my ($elem) = @_;
263 8 ▶ if (my $packelem = Perl::Critic::Pulp::Utils::elem_package($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $packelem = Perl::Critic::Pulp::Utils::elem_package($elem)) 3 5 8
264 3 ▶ if (my $name = $packelem->namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $packelem->namespace) 1 2 3
265 1 return $name;
266 }
267 }
268 7 return 'main';
269 }
270  
271 # As per perlsyn.pod, except \2 instead of \g2 since \g only in perl 5.10 up.
272 # Is this in a module somewhere?
273 40 my $line_directive_re =
274 qr/^\# \s*
275 line \s+ (\d+) \s*
276 (?:\s("?)([^"]+)\2)? \s*
277 $/xm;
278  
279 # $elem is a PPI::Element
280 # Return its logical filename (a string).
281 # This is from a "#line" comment directive, or the $document filename if no
282 # such.
283 #
284 4 ▶sub _elem_logical_filename {
Subroutine _elem_logical_filename: called CC=2 SCAR=6.9
285 4 my ($elem, $document) = @_;
286 ### _elem_logical_filename(): "$elem"
287  
288 4 my $filename;
289 116 ▶ $document->find_first (sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
290 116 my ($doc, $e) = @_;
291 # ### comment: (ref $e)." ".$e->content
292 116 ▶ if ($e == $elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e == $elem) 4 112 116
293 ### not found before target elem, stop ...
294 4 return undef;
295 }
296 112 ▶ if ($e->isa('PPI::Token::Comment')
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("PPI::Token::Comment") and $e->content =~ /$line_directive_re/) 2 110 112
Condition66%2/3
$e->isa("PPI::Token::Comment") and $e->content =~ /$line_directive_re/
!ll&&!rl&&r
11002
MC/DC50%1/2
$e->isa("PPI::Token::Comment") and $e->content =~ /$line_directive_re/
$e->isa("PPI::Token::Comment") $e->content =~ /$line_directive_re/
Truth table66%2/3
A and B
A= $e->isa("PPI::Token::Comment")B= $e->content =~ /$line_directive_re/
ABresult
0X0
100
111
297 && $e->content =~ $line_directive_re) {
298 2 $filename = $3;
299 ### found line directive: $filename
300 }
301 112 return 0; # continue
302 4 });
303 4 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) { } 2 2 4
304 2 return $filename;
305 } else {
306 ### not found, use document: $document->filename
307 2 return $document->filename;
308 }
309 }
310  
311 40 1;
312 __END__
« Policy/ValuesAndExpressions/NotWithCompare.pm ↑ Summary Policy/ValuesAndExpressions/ConstantBeforeLt.pm »