Policy/Compatibility/Gtk2Constants.pm

filter: Statementstmt 93.5% 101 / 108 Branchbran 77.0% 37 / 48 Conditioncond 84.0% 21 / 25 MC/DCmcdc 78.5% 11 / 14 Subroutinesub 94.7% 18 / 19 Podpod 100% 1 / 1 totaltotal 87.9% 189 / 215 CC 35 SCAR 23.1
CC
35
Cov
88%
violates
20.1
_highest_explicit_module_version
8.1
_include_module_version_with_exporter
6.5
CRAP 10.1
« Policy/Documentation/ProhibitDuplicateHeadings.pm ↑ Summary Policy/Documentation/ProhibitUnbalancedParens.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 3, or (at your option) any later
6 # version.
7 #
8 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15  
16  
17 package Perl::Critic::Policy::Compatibility::Gtk2Constants;
18 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use version (); # but don't import qv()
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Utils qw(is_function_call
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 is_method_call);
26 40 ▶use Perl::Critic::Pulp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 40 our $VERSION = 100;
32  
33 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use constant applies_to => qw(PPI::Token::Word PPI::Token::Symbol);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 40 my $v1_190 = version->new('1.190');
39 40 my $v1_210 = version->new('1.210');
40 40 my $v1_211 = version->new('1.211');
41  
42 40 my %constants = (
43 GTK_PRIORITY_RESIZE => ['Gtk2',$v1_190],
44 GDK_PRIORITY_EVENTS => ['Gtk2',$v1_190],
45 GDK_PRIORITY_REDRAW => ['Gtk2',$v1_190],
46 GDK_CURRENT_TIME => ['Gtk2',$v1_190],
47  
48 EVENT_PROPAGATE => ['Gtk2',$v1_210],
49 EVENT_STOP => ['Gtk2',$v1_210],
50  
51 GTK_PATH_PRIO_LOWEST => ['Gtk2',$v1_211],
52 GTK_PATH_PRIO_GTK => ['Gtk2',$v1_211],
53 GTK_PATH_PRIO_APPLICATION => ['Gtk2',$v1_211],
54 GTK_PATH_PRIO_THEME => ['Gtk2',$v1_211],
55 GTK_PATH_PRIO_RC => ['Gtk2',$v1_211],
56 GTK_PATH_PRIO_HIGHEST => ['Gtk2',$v1_211],
57  
58 SOURCE_CONTINUE => ['Glib',$v1_210],
59 SOURCE_REMOVE => ['Glib',$v1_210],
60 );
61  
62 79 ▶sub violates {
Subroutine violates: called CC=20 SCAR=30.0
Pod documented
63 79 my ($self, $elem, $document) = @_;
64  
65 79 my $elem_str;
66 79 ▶ if ($elem->isa('PPI::Token::Symbol')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Symbol')) { } 16 63 79
67 16 ▶ $elem->symbol_type eq '&'
Branch: 1 branch
BranchTrueFalseTotal
$elem->symbol_type eq "&" or return 10 6 16
68 or return; # only &SOURCE_REMOVE is for us
69 10 $elem_str = substr $elem->symbol, 1;
70 } else {
71 63 $elem_str = $elem->content;
72 }
73 73 my ($elem_qualifier, $elem_basename) = _qualifier_and_basename ($elem_str);
74  
75 # quick lookup excludes names not of interest
76 73 ▶ my $constinfo = $constants{$elem_basename}
Condition100%2/2
$constants{$elem_basename} || (return)
l!l
3241
MC/DC100%1/1
$constants{$elem_basename} || (return)
$constants{$elem_basename}
Truth table100%2/2
A
A= $constants{$elem_basename}
Aresult
11
00
77 || return;
78 32 my ($const_module, $want_version) = @$constinfo;
79  
80 32 ▶ if ($elem->isa('PPI::Token::Symbol') || is_function_call ($elem)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Symbol') or is_function_call($elem)) { } 24 8 32
elsif (is_method_call($elem)) { } 6 2 8
Condition100%3/3
$elem->isa("PPI::Token::Symbol") or is_function_call($elem)
l!l&&r!l&&!r
10148
MC/DC100%2/2
$elem->isa("PPI::Token::Symbol") or is_function_call($elem)
$elem->isa("PPI::Token::Symbol") is_function_call($elem)
Truth table100%3/3
A or B
A= $elem->isa("PPI::Token::Symbol")B= is_function_call($elem)
ABresult
1X1
011
000
81 24 ▶ if (defined $elem_qualifier) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem_qualifier) { } 18 6 24
82 18 ▶ if ($elem_qualifier ne $const_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_qualifier ne $const_module) 1 17 18
83 1 return; # from another module, eg. Foo::Bar::SOURCE_REMOVE
84 }
85 } else {
86 6 ▶ if (! _document_uses_module ($document, $const_module)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_document_uses_module($document, $const_module)) 4 2 6
87 4 return; # unqualified SOURCE_REMOVE, and no mention of Glib, etc
88 }
89 }
90  
91 } elsif (is_method_call ($elem)) {
92 6 ▶ if (defined $elem_qualifier) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem_qualifier) { } 0 6 6
93 # an oddity like Some::Where->Gtk2::SOURCE_REMOVE
94 0 ▶ if ($elem_qualifier ne $const_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_qualifier ne $const_module) 0 0 0
95 0 return; # from another module, Some::Where->Foo::Bar::SOURCE_REMOVE
96 }
97 } else {
98 # unqualified method name, eg. Some::Thing->SOURCE_REMOVE
99 6 my $class_elem = $elem->sprevious_sibling->sprevious_sibling;
100 6 ▶ if (! $class_elem || ! $class_elem->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class_elem and $class_elem->isa("PPI::Token::Word")) 2 4 6
Condition100%3/3
$class_elem and $class_elem->isa("PPI::Token::Word")
!ll&&!rl&&r
114
MC/DC100%2/2
$class_elem and $class_elem->isa("PPI::Token::Word")
$class_elem $class_elem->isa("PPI::Token::Word")
Truth table100%3/3
A and B
A= $class_elemB= $class_elem->isa("PPI::Token::Word")
ABresult
0X0
100
111
101 # ignore oddities like $foo->SOURCE_REMOVE
102 2 return;
103 }
104 4 my $class_name = $class_elem->content;
105 4 ▶ if ($class_name ne $const_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_name ne $const_module) 1 3 4
106 # some other class, eg. Foo::Bar->SOURCE_REMOVE
107 1 return;
108 }
109 }
110  
111 } else {
112 # not a function or method call
113 2 return;
114 }
115  
116 22 my $got_version = _highest_explicit_module_version ($document,$const_module);
117 22 ▶ if (defined $got_version && ref $got_version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got_version and ref $got_version) 14 8 22
Condition100%3/3
defined $got_version and ref $got_version
!ll&&!rl&&r
6214
MC/DC100%2/2
defined $got_version and ref $got_version
defined $got_version ref $got_version
Truth table100%3/3
A and B
A= defined $got_versionB= ref $got_version
ABresult
0X0
100
111
118 14 ▶ if ($got_version >= $want_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_version >= $want_version) 9 5 14
119 9 return;
120 }
121 }
122  
123 13 ▶ return $self->violation
Branch: 1 branch
BranchTrueFalseTotal
defined $got_version && ref $got_version ? : 5 8 13
Condition100%3/3
defined $got_version and ref $got_version
!ll&&!rl&&r
625
MC/DC100%2/2
defined $got_version and ref $got_version
defined $got_version ref $got_version
Truth table100%3/3
A and B
A= defined $got_versionB= ref $got_version
ABresult
0X0
100
111
124 ("$elem requires $const_module $want_version, but "
125 . (defined $got_version && ref $got_version
126 ? "version in file is $got_version"
127 : "no version specified in file"),
128 '',
129 $elem);
130 }
131  
132 # "Foo" return (undef, "Foo")
133 # "Foo::Bar::Quux" return ("Foo::Bar", "Quux")
134 #
135 77 ▶sub _qualifier_and_basename {
Subroutine _qualifier_and_basename: called CC=1 SCAR=0.0
136 77 my ($str) = @_;
137 77 return ($str =~ /(?:(.*)::)?(.*)/);
138 }
139  
140 # return true if $document has a "use" or "require" of $module (string name
141 # of a package)
142 6 ▶sub _document_uses_module {
Subroutine _document_uses_module: called CC=2 SCAR=7.1
143 6 my ($document, $module) = @_;
144  
145 6 ▶ my $aref = $document->find ('PPI::Statement::Include')
Condition100%2/2
$document->find('PPI::Statement::Include') || (return)
l!l
24
MC/DC100%1/1
$document->find('PPI::Statement::Include') || (return)
$document->find('PPI::Statement::Include')
Truth table100%2/2
A
A= $document->find('PPI::Statement::Include')
Aresult
11
00
146 || return; # if no Includes at all
147 2 ▶ return List::Util::first {$_->type eq 'use'
Subroutine __ANON__: called CC=3 SCAR=12.7
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq "use" and ($_->module || "") eq $module 2 0 2
Condition50%1/2
$_->module || ''
l!l
20
MC/DC0%0/1
$_->module || ''
$_->module
Truth table50%1/2
A
A= $_->module
Aresult
11
00
148 && (($_->module || '') eq $module)
149 2 } @$aref;
150 }
151  
152 # return a "version" object which is the highest explicit use for $module (a
153 # string) in $document
154 #
155 # A call like Foo::Bar->VERSION(123) is a version check, but not sure that's
156 # worth looking for.
157 #
158 # If there's no version number on any "use" of $module then the return is
159 # version->new(0). If there's no "use" of $module at all then the return is
160 # undef.
161 #
162 22 ▶sub _highest_explicit_module_version {
Subroutine _highest_explicit_module_version: called CC=7 SCAR=21.0
163 22 my ($document, $module) = @_;
164  
165 22 my $cache_key = __PACKAGE__.'::_highest_explicit_module_version--'.$module;
166 0 ▶ if (exists $document->{$cache_key}) { return $document->{$cache_key}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $document->{$cache_key}) 0 22 22
167  
168 22 ▶ my $aref = $document->find ('PPI::Statement::Include')
Condition100%2/2
$document->find('PPI::Statement::Include') || (return)
l!l
166
MC/DC100%1/1
$document->find('PPI::Statement::Include') || (return)
$document->find('PPI::Statement::Include')
Truth table100%2/2
A
A= $document->find('PPI::Statement::Include')
Aresult
11
00
169 || return; # if no Includes at all
170 16 ▶ my @incs = grep {$_->type eq 'use'
Condition40%2/5
$_->type eq 'use' && ($_->module || '') eq $module
!ll&&!rl&&r
0016
$_->module || ''
l!l
160
MC/DC0%0/2
$_->type eq 'use' && ($_->module || '') eq $module
$_->type eq 'use' $_->module
Truth table33%1/3
A && B
A= $_->type eq 'use'B= $_->module
ABresult
0X0
100
111
171 && (($_->module || '') eq $module)} @$aref;
172 ### all incs: @$aref
173 ### matched incs: @incs
174 0 ▶ if (! @incs) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@incs) 0 16 16
175  
176 16 my @vers = map { _include_module_version_with_exporter($_) } @incs;
177 ### versions: @vers
178 16 @vers = grep {defined} @vers;
179 16 ▶ if (! @vers) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@vers) 2 14 16
180  
181 14 @vers = map {version->new($_)} @vers;
182 0 ▶ my $maxver = List::Util::reduce {$a >= $b ? $a : $b} @vers;
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$a >= $b ? : 0 0 0
183 14 return ($document->{$cache_key} = $maxver);
184 }
185  
186  
187 # $inc is a PPI::Statement::Include.
188 #
189 # If $inc has a version number, either in perl's native form or as a string
190 # or number as handled by the Exporter package, then return that as a
191 # version object.
192 #
193 16 ▶sub _include_module_version_with_exporter {
Subroutine _include_module_version_with_exporter: called CC=6 SCAR=18.7
194 16 my ($inc) = @_;
195  
196 16 ▶ if (my $ver = Perl::Critic::Pulp::Utils::include_module_version ($inc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ver = Perl::Critic::Pulp::Utils::include_module_version($inc)) 8 8 16
197 8 return version->new ($ver->content);
198 }
199  
200 8 ▶ if (my $ver = Perl::Critic::Pulp::Utils::include_module_first_arg ($inc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ver = Perl::Critic::Pulp::Utils::include_module_first_arg($inc)) 6 2 8
201 6 ▶ if ($ver->isa('PPI::Token::Number')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ver->isa('PPI::Token::Number')) { } 0 6 6
elsif ($ver->isa('PPI::Token::Quote')) { } 6 0 6
202 0 $ver = $ver->content;
203 } elsif ($ver->isa('PPI::Token::Quote')) {
204 6 $ver = $ver->string;
205 } else {
206 0 return undef;
207 }
208 # Exporter looks only for a leading digit before calling ->VERSION, but
209 # be tighter here to avoid errors from version.pm about bad values
210 6 ▶ if ($ver =~ $Perl::Critic::Pulp::Utils::use_module_version_number_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver =~ /$Perl::Critic::Pulp::Utils::use_module_version_number_re/) 6 0 6
211 6 return version->new ($ver);
212 }
213 }
214  
215 2 return undef;
216 }
217  
218 40 1;
219 __END__
« Policy/Documentation/ProhibitDuplicateHeadings.pm ↑ Summary Policy/Documentation/ProhibitUnbalancedParens.pm »