Conformance/Check/CriticProfile.pm

filter: Statementstmt 16.9% 21 / 124 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 25.0% 7 / 28 Podpod 100% 20 / 20 totaltotal 17.5% 48 / 274 CC 43 SCAR 43.2
CC
43
Cov
10%
differences
182.0
adopt
182.0
profile_of
30.0
CRAP 74.9
« Conformance/Check/AuthorDeps.pm ↑ Summary Conformance/Check/DistIni.pm »
1 package Dist::Conformance::Check::CriticProfile 0.011;
2  
3 # ABSTRACT: The perlcritic profile is its template, with the overrides of the repository
4  
5 1 ▶use 5.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::Base 'Dist::Conformance::Check::IniTemplate', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Config::Tiny();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Dist::Conformance::Schema();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 our $VALUE = { anyOf => [ { type => 'string', nullable => 1 }, { type => 'number' } ] };
17  
18 0 ▶sub config_schema ($) {
Subroutine config_schema: not called CC=1 SCAR=6.9
Pod documented
19 return {
20 0 required => [qw{file template}],
21 properties => {
22 file => $Dist::Conformance::Schema::PATH,
23 template => $Dist::Conformance::Schema::PATH,
24 overrides => {
25 type => 'object',
26 additionalProperties => { anyOf => [ { type => 'string', enum => ['off'] }, { type => 'object', additionalProperties => $VALUE } ] },
27 default => {},
28 },
29 settings => { type => 'object', additionalProperties => $VALUE, default => {} },
30 replace => { type => 'boolean' },
31 retired => { type => 'array', items => { type => 'string' } },
32 header => { type => 'string' },
33 outdated => { type => 'array', items => { type => 'string' } },
34 },
35 };
36 }
37  
38  
39 0 ▶sub policy_of ($section) {
Subroutine policy_of: not called CC=1 SCAR=6.9
Pod documented
40 0 my ( $off, $name ) = $section =~ m/\A(-?)(?:Perl::Critic::Policy::)?(.+)\z/;
41 0 return ( !!$off, $name );
42 }
43  
44  
45 0 ▶sub profile_of ( $text, $where ) {
Subroutine profile_of: not called CC=5 SCAR=34.0
Pod documented
46 0 ▶ my $tiny = Config::Tiny->read_string($text) // die( "$where does not parse as a perlcritic profile: " . Config::Tiny->errstr . "\n" );
Condition0%0/2
"Config::Tiny"->read_string($text) // die("$where does not parse as a perlcritic profile: " . "Config::Tiny"->errstr . "\n")
l!l
00
MC/DC0%0/1
"Config::Tiny"->read_string($text) // die("$where does not parse as a perlcritic profile: " . "Config::Tiny"->errstr . "\n")
"Config::Tiny"->read_string($text)
Truth table0%0/2
A
A= "Config::Tiny"->read_string($text)
Aresult
11
00
47 0 ▶ my %profile = ( settings => $tiny->{_} // {}, policies => {} );
Condition0%0/2
$$tiny{"_"} // {}
l!l
00
MC/DC0%0/1
$$tiny{"_"} // {}
$$tiny{"_"}
Truth table0%0/2
A
A= $$tiny{"_"}
Aresult
11
00
48 0 for my $section ( grep { $_ ne q{_} } keys %$tiny ) {
49 0 my ( $off, $name ) = policy_of($section);
50 0 ▶ $profile{policies}{$name} = { off => $off, parameters => $off ? {} : $tiny->{$section} };
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
51 }
52 0 return \%profile;
53 }
54  
55  
56 0 ▶sub header_of ($line) {
Subroutine header_of: not called CC=1 SCAR=6.9
Pod documented
57 0 my ($section) = $line =~ s/\s;\s.+$//r =~ m/\A\s*\[\s*(.+?)\s*\]\s*$/;
58 0 return $section;
59 }
60  
61  
62 0 ▶sub parameter_of ($line) {
Subroutine parameter_of: not called CC=3 SCAR=24.8
Pod documented
63 0 ▶ return if $line =~ m/\A\s*(?:[\#;]|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:[\#;]|$)/aasx 0 0 0
64 0 my ( $key, $value ) = $line =~ m/\A\s*([^=]+?)\s*=\s*(.*?)\s*$/;
65 0 ▶ return if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
66 0 return ( $key, $value );
67 }
68  
69  
70 0 ▶sub rewrite ( $lines, $overlay ) {
Subroutine rewrite: not called CC=1 SCAR=6.9
Pod documented
71 0 return __PACKAGE__->rewrite_lines( $lines, $overlay );
72 }
73  
74 # How Perl::Critic reads a line, a section and a profile, for
75 # L<Dist::Conformance::Check::IniTemplate>.
76 0 ▶sub line_header ( $, $line ) { return header_of($line) }
Subroutine line_header: not called CC=1 SCAR=6.9
Pod documented
77 0 ▶sub line_parameter ( $, $line ) { return parameter_of($line) }
Subroutine line_parameter: not called CC=1 SCAR=6.9
Pod documented
78 0 ▶sub section_of ( $, $header ) { return policy_of($header) }
Subroutine section_of: not called CC=1 SCAR=6.9
Pod documented
79 0 ▶sub on_header ( $, $name, $ ) { return "[$name]\n" }
Subroutine on_header: not called CC=1 SCAR=6.9
Pod documented
80 0 ▶sub comment ($) { return q{#} }
Subroutine comment: not called CC=1 SCAR=6.9
Pod documented
81 0 ▶sub parse ( $, $text, $where ) { return profile_of( $text, $where ) }
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
82  
83 # A policy that an override turns off keeps its section, as C<[-Name]>, with
84 # its parameters removed.
85 0 ▶sub off_lines ( $, $name, $body ) {
Subroutine off_lines: not called CC=3 SCAR=24.8
Pod documented
86 0 my %gone;
87 0 for my $line (@$body) {
88 0 my ($key) = parameter_of($line);
89 0 ▶ $gone{$key} = undef if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
90 }
91 0 return ( "[-$name]\n", rewrite( $body, \%gone ) );
92 }
93  
94 0 ▶sub compare ( $self, $text, $want ) {
Subroutine compare: not called CC=1 SCAR=6.9
Pod documented
95 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
96 0 return differences( profile_of( $text, $file ), profile_of( $want, $template ) );
97 }
98  
99 # The entries of C<overrides>, by the short name of each policy.
100 # @returns HashRef from the name of each policy to C<off> or its parameters
101 # @throws on a name that is not that of a policy, and on two entries for one policy
102 # @example my $overrides = $check->overrides;
103 0 ▶sub overrides ($self) {
Subroutine overrides: not called CC=5 SCAR=34.0
Pod documented
104 0 my %overrides;
105 0 for my $given ( sort keys %{ $self->config->{overrides} } ) {
106 0 my ( $off, $name ) = policy_of($given);
107 0 ▶ die "overrides names $given, which is not the name of a policy\n" if $off || !Dist::Conformance::Schema::matches( $Dist::Conformance::Check::MODULE, $name );
Branch: 1 branch
BranchTrueFalseTotal
if $off or not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name) 0 0 0
Condition0%0/3
$off or not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$off or not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
$off not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
Truth table0%0/3
A or not(B)
A= $offB= not Dist::Conformance::Schema::matches($Dist::Conformance::Check::MODULE, $name)
ABresult
1X1
011
000
108 0 ▶ die "overrides names the policy $name twice\n" if exists $overrides{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $overrides{$name} 0 0 0
109 0 $overrides{$name} = $self->config->{overrides}{$given};
110 }
111 0 return \%overrides;
112 }
113  
114 # The entries of C<overrides>, by the full name of each policy, as the
115 # template C<$standard> names it.
116 # @param standard HashRef the template, as L<profile_of|/"profile_of($text, $where)"> returns it
117 # @returns HashRef from the name of each policy to C<off> or its parameters
118 # @throws on a name that is not that of a policy, and on two entries for one policy
119 # @example my $overrides = $check->resolved_overrides($standard);
120 0 ▶sub resolved_overrides ( $self, $standard ) {
Subroutine resolved_overrides: not called CC=3 SCAR=24.8
Pod documented
121 0 my $given = $self->overrides;
122 0 my %overrides;
123 0 for my $as ( sort keys %$given ) {
124 0 my $name = full_name( $as, $standard->{policies} );
125 0 ▶ die "overrides names the policy $name twice\n" if exists $overrides{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $overrides{$name} 0 0 0
126 0 $overrides{$name} = $given->{$as};
127 }
128 0 return \%overrides;
129 }
130  
131  
132 0 ▶sub differences ( $have, $want ) {
Subroutine differences: not called CC=13 SCAR=52.0
Pod documented
133 0 my ( $hp, $wp ) = ( $have->{policies}, $want->{policies} );
134 0 ▶ my $same = sub ( $x, $y ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 return join( "\n", map { "$_=$x->{$_}" } sort keys %$x ) eq join( "\n", map { "$_=$y->{$_}" } sort keys %$y );
136 0 };
137 my @kinds = (
138 0 [ 'lacks the policy', grep { !$hp->{$_} } sort keys %$wp ],
139 0 [ 'names the policy', grep { !$wp->{$_} } sort keys %$hp ],
140 0 ▶ [ 'enables the policy', grep { $hp->{$_} && $wp->{$_}{off} && !$hp->{$_}{off} } sort keys %$wp ],
Condition0%0/6
$$hp{$_} && $$wp{$_}{'off'} && !$$hp{$_}{'off'}
!ll&&!rl&&r
000
$$hp{$_} and $$wp{$_}{"off"}
!ll&&!rl&&r
000
MC/DC0%0/3
$$hp{$_} && $$wp{$_}{'off'} && !$$hp{$_}{'off'}
$$hp{$_} $$wp{$_}{"off"} !$$hp{$_}{'off'}
Truth table0%0/4
A and B && not(C)
A= $$hp{$_}B= $$wp{$_}{"off"}C= !$$hp{$_}{'off'}
ABCresult
0XX0
10X0
1100
1111
141 0 ▶ [ 'turns off the policy', grep { $hp->{$_} && !$wp->{$_}{off} && $hp->{$_}{off} } sort keys %$wp ],
Condition0%0/6
$$hp{$_} && !$$wp{$_}{'off'} && $$hp{$_}{'off'}
!ll&&!rl&&r
000
$$hp{$_} and not $$wp{$_}{"off"}
!ll&&!rl&&r
000
MC/DC0%0/3
$$hp{$_} && !$$wp{$_}{'off'} && $$hp{$_}{'off'}
$$hp{$_} not $$wp{$_}{"off"} $$hp{$_}{'off'}
Truth table0%0/4
A and not(B) && C
A= $$hp{$_}B= not $$wp{$_}{"off"}C= $$hp{$_}{'off'}
ABCresult
0XX0
10X0
1100
1111
142 0 ▶ [ 'sets other parameters for the policy', grep { local ($_) = $_; $hp->{$_} && !$wp->{$_}{off} && !$hp->{$_}{off} && !$same->( $hp->{$_}{parameters}, $wp->{$_}{parameters} ) } sort keys %$wp ],
Condition0%0/9
$$hp{$_} && !$$wp{$_}{'off'} && !$$hp{$_}{'off'} && !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
!ll&&!rl&&r
000
$$hp{$_} and not $$wp{$_}{"off"} and not $$hp{$_}{"off"}
!ll&&!rl&&r
000
$$hp{$_} and not $$wp{$_}{"off"}
!ll&&!rl&&r
000
MC/DC0%0/4
$$hp{$_} && !$$wp{$_}{'off'} && !$$hp{$_}{'off'} && !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
$$hp{$_} not $$wp{$_}{"off"} not $$hp{$_}{"off"} !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
Truth table0%0/5
A and not(B) and not(C) && not(D)
A= $$hp{$_}B= not $$wp{$_}{"off"}C= not $$hp{$_}{"off"}D= !&$same($$hp{$_}{'parameters'}, $$wp{$_}{'parameters'})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
143 );
144 0 my @differences;
145 0 for my $kind (@kinds) {
146 0 my ( $phrase, @names ) = @$kind;
147 0 ▶ next if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
148 0 ▶ my $text = ( @names > 1 ? $phrase =~ s/policy\z/policies/r : $phrase ) . ' ' . join ', ', @names;
Branch: 1 branch
BranchTrueFalseTotal
@names > 1 ? : 0 0 0
149 0 ▶ $text .= ', which neither the template nor the overrides name' if $phrase eq 'names the policy';
Branch: 1 branch
BranchTrueFalseTotal
if $phrase eq "names the policy" 0 0 0
150 0 push @differences, $text;
151 }
152 0 ▶ push @differences, 'sets other global settings' if !$same->( $have->{settings}, $want->{settings} );
Branch: 1 branch
BranchTrueFalseTotal
unless &$same($$have{"settings"}, $$want{"settings"}) 0 0 0
153 0 return @differences;
154 }
155  
156  
157 0 ▶sub adopt ( $self, $snapshot ) {
Subroutine adopt: not called CC=13 SCAR=52.0
Pod documented
158 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
159 0 ▶ my $text = $snapshot->content($file) // return;
Condition0%0/2
$snapshot->content($file) // (return)
l!l
00
MC/DC0%0/1
$snapshot->content($file) // (return)
$snapshot->content($file)
Truth table0%0/2
A
A= $snapshot->content($file)
Aresult
11
00
160 0 my ( $filled, @unfilled ) = $self->template($template);
161 0 ▶ die( "$template needs the variables " . join( ', ', @unfilled ) . " in .conformance.yml: nothing sets them\n" ) if @unfilled;
Branch: 1 branch
BranchTrueFalseTotal
if @unfilled 0 0 0
162 0 my $have = profile_of( $text, $file );
163 0 my $want = profile_of( $filled, $template );
164  
165 0 my %overrides;
166 0 for my $given ( sort keys %{ $have->{policies} } ) {
167 0 my $mine = $have->{policies}{$given};
168 0 my $name = full_name( $given, $want->{policies} );
169 0 my $standard = $want->{policies}{$name};
170 0 ▶ if ( $mine->{off} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$mine{"off"}) 0 0 0
171 0 ▶ $overrides{$name} = 'off' if !$standard || !$standard->{off};
Branch: 1 branch
BranchTrueFalseTotal
unless $standard and $$standard{"off"} 0 0 0
Condition0%0/3
$standard and $$standard{"off"}
!ll&&!rl&&r
000
MC/DC0%0/2
$standard and $$standard{"off"}
$standard $$standard{"off"}
Truth table0%0/3
A and B
A= $standardB= $$standard{"off"}
ABresult
0X0
100
111
172 0 next;
173 }
174 0 ▶ if ( !$standard || $standard->{off} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $standard or $$standard{"off"}) 0 0 0
Condition0%0/3
not $standard or $$standard{"off"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $standard or $$standard{"off"}
not $standard $$standard{"off"}
Truth table0%0/3
not(A) or B
A= not $standardB= $$standard{"off"}
ABresult
1X1
011
000
175 0 $overrides{$name} = { %{ $mine->{parameters} } };
176 0 next;
177 }
178 0 my $changed = overlay( $standard->{parameters}, $mine->{parameters} );
179 0 ▶ $overrides{$name} = $changed if %$changed;
Branch: 1 branch
BranchTrueFalseTotal
if %$changed 0 0 0
180 }
181 0 my $settings = overlay( $want->{settings}, $have->{settings} );
182 0 ▶ my %adopted = ( %overrides ? ( overrides => \%overrides ) : (), %$settings ? ( settings => $settings ) : () );
Branch: 2 branches
BranchTrueFalseTotal
%overrides ? : 0 0 0
%$settings ? : 0 0 0
183 0 ▶ return %adopted ? \%adopted : undef;
Branch: 1 branch
BranchTrueFalseTotal
%adopted ? : 0 0 0
184 }
185  
186  
187 0 ▶sub full_name ( $name, $policies ) {
Subroutine full_name: not called CC=3 SCAR=24.8
Pod documented
188 0 ▶ return $name if exists $policies->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$policies{$name} 0 0 0
189 0 my @longer = grep { local ($_) = $_; substr( $_, -length("::$name") ) eq "::$name" } keys %$policies;
190 0 ▶ return @longer == 1 ? $longer[0] : $name;
Branch: 1 branch
BranchTrueFalseTotal
@longer == 1 ? : 0 0 0
191 }
192  
193  
194 0 ▶sub overlay ( $from, $to ) {
Subroutine overlay: not called CC=3 SCAR=24.8
Pod documented
195 0 ▶ my %overlay = map { $_ => $to->{$_} } grep { !exists $from->{$_} || $from->{$_} ne $to->{$_} } keys %$to;
Condition0%0/3
!exists($$from{$_}) || $$from{$_} ne $$to{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
!exists($$from{$_}) || $$from{$_} ne $$to{$_}
!exists($$from{$_}) $$from{$_} ne $$to{$_}
Truth table0%0/3
not(A) || B
A= !exists($$from{$_})B= $$from{$_} ne $$to{$_}
ABresult
1X1
011
000
196 0 $overlay{$_} = undef for grep { !exists $to->{$_} } keys %$from;
197 0 return \%overlay;
198 }
199  
200 __END__
« Conformance/Check/AuthorDeps.pm ↑ Summary Conformance/Check/DistIni.pm »