Policy/Miscellanea/TextDomainUnused.pm

filter: Statementstmt 100% 58 / 58 Branchbran 91.6% 11 / 12 Conditioncond 60.0% 9 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 88.3% 99 / 112 SCAR 26.5
CC
14
Cov
92%
violates
5.0
__ANON__
3.6
__ANON__
3.0
CRAP 14.1
« Pulp/PodParser.pm ↑ Summary Policy/Documentation/ProhibitParagraphEndComma.pm »
1 # Copyright 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::TextDomainUnused;
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  
27 40 our $VERSION = 99;
28  
29 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub violates {
35 14 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=5 SCAR=16.1
Pod documented
36  
37 14 ▶ my $use = _find_use_locale_textdomain($document) || return;
Condition50%1/2
_find_use_locale_textdomain($document) || (return)
l!l
140
MC/DC0%0/1
_find_use_locale_textdomain($document) || (return)
_find_use_locale_textdomain($document)
Truth table50%1/2
A
A= _find_use_locale_textdomain($document)
Aresult
11
00
38 14 ▶ if (_any_calls_locale_textdomain($document)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (_any_calls_locale_textdomain($document)) 7 7 14
39 7 ▶ if (_any_vars_locale_textdomain($document)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (_any_vars_locale_textdomain($document)) 2 5 7
40 5 ▶ if (_any_strings_locale_textdomain($document)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (_any_strings_locale_textdomain($document)) 3 2 5
41  
42 2 return $self->violation
43 ('Locale::TextDomain imported, but none of its functions used',
44 '',
45 $use);
46 }
47  
48 # return a PPI::Statement::Include which is a "use" or "require" of
49 # Locale::TextDomain, or return false if there's no such
50 sub _find_use_locale_textdomain {
51 14 ▶ my ($document) = @_;
Subroutine _find_use_locale_textdomain: called CC=2 SCAR=7.5
52 14 ▶ my $aref = $document->find ('PPI::Statement::Include')
Condition50%1/2
$document->find('PPI::Statement::Include') || (return)
l!l
140
MC/DC0%0/1
$document->find('PPI::Statement::Include') || (return)
$document->find('PPI::Statement::Include')
Truth table50%1/2
A
A= $document->find('PPI::Statement::Include')
Aresult
11
00
53 || return; # if no includes at all
54 14 ▶ return List::Util::first { $_->type ne 'no'
Subroutine __ANON__: called CC=3 SCAR=12.7
Branch: 1 branch
BranchTrueFalseTotal
$_->type ne "no" and ($_->module || "") eq "Locale::TextDomain" 14 0 14
Condition50%1/2
$_->module || ''
l!l
140
MC/DC0%0/1
$_->module || ''
$_->module
Truth table50%1/2
A
A= $_->module
Aresult
11
00
55 && ($_->module||'') eq 'Locale::TextDomain'
56 14 } @$aref;
57 }
58  
59  
60 # The following qw() copied from @Locale::TextDomain::EXPORT of libintl-perl
61 # 1.18, with $__ %__ moved to %vars below. __p and friends are new in 1.17,
62 # but no need to check that.
63 #
64 40 my %funcs = map {($_=>1)}
65 qw(__ __x __n __nx __xn __p __px __np __npx
66 N__ N__n N__p N__np);
67  
68 # and also as full names "Locale::TextDomain::__"
69 40 foreach (keys %funcs) {
70 520 $funcs{"Locale::TextDomain::$_"} = 1;
71 }
72  
73 # return true if $document has any of the Locale::TextDomain functions used,
74 # like __() etc
75 sub _any_calls_locale_textdomain {
76 14 ▶ my ($document) = @_;
Subroutine _any_calls_locale_textdomain: called CC=2 SCAR=7.0
77 14 ▶ my $aref = $document->find ('PPI::Token::Word')
Condition50%1/2
$document->find('PPI::Token::Word') || (return)
l!l
140
MC/DC0%0/1
$document->find('PPI::Token::Word') || (return)
$document->find('PPI::Token::Word')
Truth table50%1/2
A
A= $document->find('PPI::Token::Word')
Aresult
11
00
78 || return; # if no word tokens at all
79 50 ▶ return List::Util::first { $funcs{$_->content}
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$funcs{$_->content} and is_function_call($_) 7 43 50
80 && is_function_call($_)
81 14 } @$aref;
82 }
83  
84 ## no critic (RequireInterpolationOfMetachars)
85 40 my %vars = ('$__' => 1,
86 '%__' => 1);
87 ## use critic
88  
89 sub _any_vars_locale_textdomain {
90 7 ▶ my ($document) = @_;
Subroutine _any_vars_locale_textdomain: called CC=2 SCAR=6.9
91 7 ▶ my $aref = $document->find ('PPI::Token::Symbol')
Condition100%2/2
$document->find('PPI::Token::Symbol') || (return)
l!l
25
MC/DC100%1/1
$document->find('PPI::Token::Symbol') || (return)
$document->find('PPI::Token::Symbol')
Truth table100%2/2
A
A= $document->find('PPI::Token::Symbol')
Aresult
11
00
92 || return; # if no symbols at all
93 2 ▶ return List::Util::first { $vars{$_->symbol} } @$aref;
Subroutine __ANON__: called CC=1 SCAR=0.0
94 }
95 sub _any_strings_locale_textdomain {
96 5 ▶ my ($document) = @_;
Subroutine _any_strings_locale_textdomain: called CC=2 SCAR=7.1
97 5 ▶ my $aref = $document->find ('PPI::Token::Quote')
Condition50%1/2
$document->find('PPI::Token::Quote') || (return)
l!l
50
MC/DC0%0/1
$document->find('PPI::Token::Quote') || (return)
$document->find('PPI::Token::Quote')
Truth table50%1/2
A
A= $document->find('PPI::Token::Quote')
Aresult
11
00
98 || return; # if no strings at all
99 9 ▶ return List::Util::first { ($_->isa('PPI::Token::Quote::Interpolate')
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("PPI::Token::Quote::Interpolate") || $_->isa("PPI::Token::Quote::Double") and $_->string =~ /\$__(\W|$)/ 8 1 9
Condition66%2/3
$_->isa("PPI::Token::Quote::Interpolate") or $_->isa("PPI::Token::Quote::Double")
l!l&&r!l&&!r
081
MC/DC50%1/2
$_->isa("PPI::Token::Quote::Interpolate") or $_->isa("PPI::Token::Quote::Double")
$_->isa("PPI::Token::Quote::Interpolate") $_->isa("PPI::Token::Quote::Double")
Truth table66%2/3
A or B
A= $_->isa("PPI::Token::Quote::Interpolate")B= $_->isa("PPI::Token::Quote::Double")
ABresult
1X1
011
000
100 || $_->isa('PPI::Token::Quote::Double'))
101 5 && $_->string =~ /\$__(\W|$)/ } @$aref;
102 }
103  
104 40 1;
105 __END__
106  
107 =for stopwords textdomain perlcritic TextDomainUnused eg TextDomain PPI Ryde
108  
109 =head1 NAME
110  
111 Perl::Critic::Policy::Miscellanea::TextDomainUnused - check for Locale::TextDomain imported but unused
112  
113 =head1 DESCRIPTION
114  
115 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
116 add-on. It reports when you have L<C<Locale::TextDomain>|Locale::TextDomain>
117 like
118  
119 use Locale::TextDomain ('MyMessageDomain');
120  
121 but then don't use any of its functions or variables
122  
123 __ __x __n __nx __xn
124 __p __px __np __npx
125 N__ N__n N__p N__np
126 %__ $__
127  
128 C<Locale::TextDomain> is not needed when not used, but it's also not
129 actively harmful so this policy is only low severity and under the
130 C<cosmetic> theme (see L<Perl::Critic/POLICY THEMES>).
131  
132 The check is good if you've got C<Locale::TextDomain> as boilerplate code in
133 most of your program, but in some modules it's unused. You can remove it
134 entirely from non-interactive modules, or comment it out from modules which
135 might have messages but don't yet. The best thing picked up is when your
136 boilerplate has got into a programmatic module which shouldn't say anything
137 at the user level.
138  
139 The saving from removing unused C<Locale::TextDomain> is modest, just some
140 imports and a hash entry holding the "textdomain" for the package.
141  
142 It's easy to imagine a general kind of "module imported but unused" policy
143 check, but in practice its hard for perlcritic to know the automatic imports
144 of every module, and quite a few modules have side-effects, so this
145 TextDomainUnused policy just starts with one case of an unused include.
146  
147 =head2 Interpolated Variables
148  
149 The variables C<%__> and C<$__> are recognised in double-quote interpolated
150 strings just by looking for a C<$__> somewhere in the string, eg.
151  
152 print "*** $__{'A Message'} ***\n"; # ok
153  
154 It's not hard to trick the recognition with escapes, or a hash slice style,
155 but in general taking any C<$__> to be a TextDomain use is close enough.
156 (Perhaps in the future PPI will do a full parse of interpolated
157 expressions.)
158  
159 =head1 SEE ALSO
160  
161 L<Perl::Critic::Pulp>,
162 L<Perl::Critic>,
163 L<Locale::TextDomain>,
164 L<Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders>
165  
166 =head1 HOME PAGE
167  
168 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
169  
170 =head1 COPYRIGHT
171  
172 Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
173  
174 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
175 under the terms of the GNU General Public License as published by the Free
176 Software Foundation; either version 3, or (at your option) any later
177 version.
178  
179 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
180 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
181 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
182 more details.
183  
184 You should have received a copy of the GNU General Public License along with
185 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
186  
187 =cut
« Pulp/PodParser.pm ↑ Summary Policy/Documentation/ProhibitParagraphEndComma.pm »