Conformance/Check/CriticProfile.pm

filter: Statementstmt 10.7% 18 / 167 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 28.5% 6 / 21 Podpod 100% 14 / 14 totaltotal 10.7% 38 / 353 CC 65 SCAR 47.2
CC
65
Cov
6%
expected
240.0
differences
182.0
adopt
182.0
CRAP 111.7
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Check/GitHooks.pm »
1 package Dist::Conformance::Check::CriticProfile 0.007;
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 ▶use Mojo::Base 'Dist::Conformance::Check', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Config::Tiny();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Dist::Conformance::Schema();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 1 our $VALUE = { anyOf => [ { type => 'string', nullable => 1 }, { type => 'number' } ] };
16  
17 0 ▶sub config_schema ($) {
Subroutine config_schema: not called CC=1 SCAR=6.9
Pod documented
18 return {
19 0 required => [qw{file template}],
20 properties => {
21 file => $Dist::Conformance::Schema::PATH,
22 template => $Dist::Conformance::Schema::PATH,
23 overrides => {
24 type => 'object',
25 additionalProperties => { anyOf => [ { type => 'string', enum => ['off'] }, { type => 'object', additionalProperties => $VALUE } ] },
26 default => {},
27 },
28 settings => { type => 'object', additionalProperties => $VALUE, default => {} },
29 replace => { type => 'boolean' },
30 retired => { type => 'array', items => { type => 'string' } },
31 header => { type => 'string' },
32 outdated => { type => 'array', items => { type => 'string' } },
33 },
34 };
35 }
36  
37  
38 0 ▶sub policy_of ($section) {
Subroutine policy_of: not called CC=1 SCAR=6.9
Pod documented
39 0 my ( $off, $name ) = $section =~ m/\A(-?)(?:Perl::Critic::Policy::)?(.+)\z/;
40 0 return ( !!$off, $name );
41 }
42  
43  
44 0 ▶sub profile_of ( $text, $where ) {
Subroutine profile_of: not called CC=5 SCAR=34.0
Pod documented
45 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
46 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
47 0 for my $section ( grep { $_ ne q{_} } keys %$tiny ) {
48 0 my ( $off, $name ) = policy_of($section);
49 0 ▶ $profile{policies}{$name} = { off => $off, parameters => $off ? {} : $tiny->{$section} };
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
50 }
51 0 return \%profile;
52 }
53  
54  
55 0 ▶sub header_of ($line) {
Subroutine header_of: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($section) = $line =~ s/\s;\s.+$//r =~ m/\A\s*\[\s*(.+?)\s*\]\s*$/;
57 0 return $section;
58 }
59  
60  
61 0 ▶sub parameter_of ($line) {
Subroutine parameter_of: not called CC=3 SCAR=24.8
Pod documented
62 0 ▶ return if $line =~ m/\A\s*(?:[\#;]|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:[\#;]|$)/aasx 0 0 0
63 0 my ( $key, $value ) = $line =~ m/\A\s*([^=]+?)\s*=\s*(.*?)\s*$/;
64 0 ▶ return if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
65 0 return ( $key, $value );
66 }
67  
68  
69 0 ▶sub rewrite ( $lines, $overlay ) {
Subroutine rewrite: not called CC=8 SCAR=42.8
Pod documented
70 0 my ( @out, %seen, $after );
71 0 for my $line (@$lines) {
72 0 my ($key) = parameter_of($line);
73 0 my $kept = $line;
74 0 ▶ if ( defined $key && exists $overlay->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and exists $overlay->{$key}) 0 0 0
Condition0%0/3
defined $key and exists $overlay->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and exists $overlay->{$key}
defined $key exists $overlay->{$key}
Truth table0%0/3
A and B
A= defined $keyB= exists $overlay->{$key}
ABresult
0X0
100
111
75 0 $seen{$key} = 1;
76 0 ▶ next if !defined $overlay->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $overlay->{$key} 0 0 0
77 0 $kept = "$key = $overlay->{$key}\n";
78 }
79 0 push @out, $kept;
80 0 ▶ $after = scalar @out if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
81 }
82 0 ▶ my @new = map {"$_ = $overlay->{$_}\n"} grep { !$seen{$_} && defined $overlay->{$_} } sort keys %$overlay;
Condition0%0/3
!$seen{$_} && defined $overlay->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
!$seen{$_} && defined $overlay->{$_}
!$seen{$_} defined $overlay->{$_}
Truth table0%0/3
not(A) && B
A= !$seen{$_}B= defined $overlay->{$_}
ABresult
0X0
100
111
83 0 ▶ splice @out, $after // 0, 0, @new;
Condition0%0/2
$after // 0
l!l
00
MC/DC0%0/1
$after // 0
$after
Truth table0%0/2
A
A= $after
Aresult
11
00
84 0 return @out;
85 }
86  
87  
88 0 ▶sub overrides ($self) {
Subroutine overrides: not called CC=5 SCAR=34.0
Pod documented
89 0 my %overrides;
90 0 for my $given ( sort keys %{ $self->config->{overrides} } ) {
91 0 my ( $off, $name ) = policy_of($given);
92 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
93 0 ▶ die "overrides names the policy $name twice\n" if exists $overrides{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $overrides{$name} 0 0 0
94 0 $overrides{$name} = $self->config->{overrides}{$given};
95 }
96 0 return \%overrides;
97 }
98  
99  
100 0 ▶sub expected ($self) {
Subroutine expected: not called CC=15 SCAR=54.8
Pod documented
101 0 my $template = $self->config->{template};
102 0 my ( $text, @unfilled ) = $self->template($template);
103 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
104 0 my $standard = profile_of( $text, $template )->{policies};
105 0 my $given = $self->overrides;
106 0 my %overrides;
107 0 for my $as ( sort keys %$given ) {
108 0 my $name = full_name( $as, $standard );
109 0 ▶ die "overrides names the policy $name twice\n" if exists $overrides{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $overrides{$name} 0 0 0
110 0 $overrides{$name} = $given->{$as};
111 }
112 0 my $overrides = \%overrides;
113  
114 0 ▶ $text .= "\n" if $text =~ m/[^\n]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[^\n]\z/aasx 0 0 0
115 0 my @lines = split /^/, $text;
116 0 my @headers = grep { defined header_of( $lines[$_] ) } 0 .. $#lines;
117 0 ▶ my @bodies = ( [ 0, $headers[0] // scalar @lines ] );
Condition0%0/2
$headers[0] // scalar @lines
l!l
00
MC/DC0%0/1
$headers[0] // scalar @lines
$headers[0]
Truth table0%0/2
A
A= $headers[0]
Aresult
11
00
118 0 ▶ push @bodies, map { [ $headers[$_] + 1, $headers[ $_ + 1 ] // scalar @lines ] } 0 .. $#headers;
Condition0%0/2
$headers[$_ + 1] // scalar @lines
l!l
00
MC/DC0%0/1
$headers[$_ + 1] // scalar @lines
$headers[$_ + 1]
Truth table0%0/2
A
A= $headers[$_ + 1]
Aresult
11
00
119  
120 0 my @out = rewrite( [ @lines[ $bodies[0][0] .. $bodies[0][1] - 1 ] ], $self->config->{settings} );
121 0 my %named;
122 0 for my $i ( 0 .. $#headers ) {
123 0 my $name = ( policy_of( header_of( $lines[ $headers[$i] ] ) ) )[1];
124 0 my @body = @lines[ $bodies[ $i + 1 ][0] .. $bodies[ $i + 1 ][1] - 1 ];
125 0 my $want = $overrides->{$name};
126 0 $named{$name} = 1;
127 0 ▶ if ( !defined $want ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $want) { } 0 0 0
elsif ($want eq 'off') { } 0 0 0
128 0 push @out, $lines[ $headers[$i] ], @body;
129 }
130 elsif ( $want eq 'off' ) {
131 0 my %gone;
132 0 for my $line (@body) {
133 0 my ($key) = parameter_of($line);
134 0 ▶ $gone{$key} = undef if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
135 }
136 0 push @out, "[-$name]\n", rewrite( \@body, \%gone );
137 }
138 else {
139 0 push @out, "[$name]\n", rewrite( \@body, $want );
140 }
141 }
142  
143 0 my @own = grep { !$named{$_} } sort keys %$overrides;
144 0 ▶ push @out, "\n# The overrides of .conformance.yml\n" if @own;
Branch: 1 branch
BranchTrueFalseTotal
if @own 0 0 0
145 0 for my $name (@own) {
146 0 my $want = $overrides->{$name};
147 0 ▶ push @out, ref $want ? ( "[$name]\n", rewrite( [], $want ) ) : "[-$name]\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $want ? : 0 0 0
148 }
149 0 return join q{}, @out;
150 }
151  
152  
153 0 ▶sub differences ( $have, $want ) {
Subroutine differences: not called CC=13 SCAR=52.0
Pod documented
154 0 my ( $hp, $wp ) = ( $have->{policies}, $want->{policies} );
155 0 ▶ my $same = sub ( $x, $y ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 return join( "\n", map {"$_=$x->{$_}"} sort keys %$x ) eq join( "\n", map {"$_=$y->{$_}"} sort keys %$y );
157 0 };
158 my @kinds = (
159 0 [ 'lacks the policy', grep { !$hp->{$_} } sort keys %$wp ],
160 0 [ 'names the policy', grep { !$wp->{$_} } sort keys %$hp ],
161 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
162 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
163 0 ▶ [ 'sets other parameters for the policy', grep { $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
164 );
165 0 my @differences;
166 0 for my $kind (@kinds) {
167 0 my ( $phrase, @names ) = @$kind;
168 0 ▶ next if !@names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
169 0 ▶ my $text = ( @names > 1 ? $phrase =~ s/policy\z/policies/r : $phrase ) . ' ' . join ', ', @names;
Branch: 1 branch
BranchTrueFalseTotal
@names > 1 ? : 0 0 0
170 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
171 0 push @differences, $text;
172 }
173 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
174 0 return @differences;
175 }
176  
177  
178 0 ▶sub check ( $self, $snapshot ) {
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
179 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
180 0 ▶ my $text = $snapshot->content($file) // return ( 'fail', "$file is missing" );
Condition0%0/2
$snapshot->content($file) // (return "fail", "$file is missing")
l!l
00
MC/DC0%0/1
$snapshot->content($file) // (return "fail", "$file is missing")
$snapshot->content($file)
Truth table0%0/2
A
A= $snapshot->content($file)
Aresult
11
00
181 0 my $want = $self->expected;
182 0 ▶ return ( 'pass', "is $template with the overrides" ) if $text eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if $text eq $want 0 0 0
183 0 ▶ my $have = eval { profile_of( $text, $file ) } // return ( 'fail', Dist::Conformance::Schema::first_line($@) . ', and conformance update replaces it' );
Condition0%0/2
eval { do { profile_of($text, $file) } } // (return "fail", Dist::Conformance::Schema::first_line($@) . ', and conformance update replaces it')
l!l
00
MC/DC0%0/1
eval { do { profile_of($text, $file) } } // (return "fail", Dist::Conformance::Schema::first_line($@) . ', and conformance update replaces it')
eval { do { profile_of($text, $file) } }
Truth table0%0/2
A
A= eval { do { profile_of($text, $file) } }
Aresult
11
00
184 0 my @differences = differences( $have, profile_of( $want, $template ) );
185 0 ▶ return ( 'info', "differs from $template with the overrides only in comments and layout, which conformance update rewrites" ) if !@differences;
Branch: 1 branch
BranchTrueFalseTotal
unless @differences 0 0 0
186 0 return ( 'fail', join( '; ', @differences ) . ', which conformance update corrects' );
187 }
188  
189  
190 0 ▶sub update ( $self, $update ) {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
191 0 my $want = eval { $self->expected };
192 0 ▶ return $update->by_hand( Dist::Conformance::Schema::first_line($@) . ': ' . $self->name . ' cannot write ' . $self->config->{file} ) if !defined $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want 0 0 0
193 0 $update->write( $self->config->{file}, $want );
194 0 return;
195 }
196  
197  
198 0 ▶sub adopt ( $self, $snapshot ) {
Subroutine adopt: not called CC=13 SCAR=52.0
Pod documented
199 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
200 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
201 0 my ( $filled, @unfilled ) = $self->template($template);
202 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
203 0 my $have = profile_of( $text, $file );
204 0 my $want = profile_of( $filled, $template );
205  
206 0 my %overrides;
207 0 for my $given ( sort keys %{ $have->{policies} } ) {
208 0 my $mine = $have->{policies}{$given};
209 0 my $name = full_name( $given, $want->{policies} );
210 0 my $standard = $want->{policies}{$name};
211 0 ▶ if ( $mine->{off} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mine->{"off"}) 0 0 0
212 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
213 0 next;
214 }
215 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
216 0 $overrides{$name} = { %{ $mine->{parameters} } };
217 0 next;
218 }
219 0 my $changed = overlay( $standard->{parameters}, $mine->{parameters} );
220 0 ▶ $overrides{$name} = $changed if %$changed;
Branch: 1 branch
BranchTrueFalseTotal
if %$changed 0 0 0
221 }
222 0 my $settings = overlay( $want->{settings}, $have->{settings} );
223 0 ▶ my %adopted = ( %overrides ? ( overrides => \%overrides ) : (), %$settings ? ( settings => $settings ) : () );
Branch: 2 branches
BranchTrueFalseTotal
%overrides ? : 0 0 0
%$settings ? : 0 0 0
224 0 ▶ return %adopted ? \%adopted : undef;
Branch: 1 branch
BranchTrueFalseTotal
%adopted ? : 0 0 0
225 }
226  
227  
228 0 ▶sub full_name ( $name, $policies ) {
Subroutine full_name: not called CC=3 SCAR=24.8
Pod documented
229 0 ▶ return $name if exists $policies->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $policies->{$name} 0 0 0
230 0 my @longer = grep { substr( $_, -length("::$name") ) eq "::$name" } keys %$policies;
231 0 ▶ return @longer == 1 ? $longer[0] : $name;
Branch: 1 branch
BranchTrueFalseTotal
@longer == 1 ? : 0 0 0
232 }
233  
234  
235 0 ▶sub overlay ( $from, $to ) {
Subroutine overlay: not called CC=3 SCAR=24.8
Pod documented
236 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
237 0 $overlay{$_} = undef for grep { !exists $to->{$_} } keys %$from;
238 0 return \%overlay;
239 }
240  
241 __END__
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Check/GitHooks.pm »