Conformance/Check/CriticProfile.pm

filter: Statementstmt 14.1% 17 / 120 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 28.5% 6 / 21 Podpod 100% 15 / 15 totaltotal 14.6% 38 / 260 CC 43 SCAR 40.1
CC
43
Cov
8%
without_sections
156.0
update
72.0
verdict
56.0
CRAP 55.4
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Check/GitHooks.pm »
1 package Dist::Conformance::Check::CriticProfile 0.005;
2  
3 # ABSTRACT: The perlcritic profile enables or turns off every policy of its template, and no retired one
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 0 ▶sub config_schema ($) {
Subroutine config_schema: not called CC=1 SCAR=6.9
Pod documented
16 return {
17 0 required => [qw{file template}],
18 properties => {
19 file => $Dist::Conformance::Schema::PATH,
20 template => $Dist::Conformance::Schema::PATH,
21 replace => { type => 'boolean', default => 0 },
22 retired => { type => 'array', items => $Dist::Conformance::Check::MODULE, uniqueItems => 1, default => [] },
23 header => { type => 'string', minLength => 1 },
24 outdated => { type => 'array', items => { type => 'string', minLength => 1 } },
25 },
26 };
27 }
28  
29  
30 0 ▶sub policies ( $text, $where ) {
Subroutine policies: not called CC=4 SCAR=30.0
Pod documented
31 0 ▶ my $profile = 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
32 0 my %policies;
33 0 for my $section ( grep { $_ ne q{_} } keys %$profile ) {
34 0 my ( $off, $name ) = policy_of($section);
35 0 ▶ $policies{$name} = $off ? 'disabled' : 'enabled';
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
36 }
37 0 return \%policies;
38 }
39  
40  
41 0 ▶sub policy_of ($section) {
Subroutine policy_of: not called CC=1 SCAR=6.9
Pod documented
42 0 my ( $off, $name ) = $section =~ m/\A(-?)(?:Perl::Critic::Policy::)?(.+)\z/;
43 0 return ( !!$off, $name );
44 }
45  
46  
47 0 ▶sub missing_policies ( $self, $text ) {
Subroutine missing_policies: not called CC=2 SCAR=17.9
Pod documented
48 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
49 0 my $want = policies( ( $self->template($template) )[0], $template );
50 0 my $have = policies( $text, $file );
51 0 ▶ my @missing = sort grep { $want->{$_} eq 'enabled' && !exists $have->{$_} } keys %$want;
Condition0%0/3
$want->{$_} eq 'enabled' && !exists $have->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$want->{$_} eq 'enabled' && !exists $have->{$_}
$want->{$_} eq 'enabled' !exists $have->{$_}
Truth table0%0/3
A && not(B)
A= $want->{$_} eq 'enabled'B= !exists $have->{$_}
ABresult
0X0
100
111
52 0 return @missing;
53 }
54  
55  
56 0 ▶sub retired ($self) {
Subroutine retired: not called CC=4 SCAR=30.0
Pod documented
57 0 my @retired = map { ( policy_of($_) )[1] } @{ $self->config->{retired} };
58 0 ▶ return if !@retired;
Branch: 1 branch
BranchTrueFalseTotal
unless @retired 0 0 0
59 0 my $template = $self->config->{template};
60 0 my $want = policies( ( $self->template($template) )[0], $template );
61 0 ▶ my @both = grep { ( $want->{$_} // q{} ) eq 'enabled' } @retired;
Condition0%0/2
$want->{$_} // ""
l!l
00
MC/DC0%0/1
$want->{$_} // ""
$want->{$_}
Truth table0%0/2
A
A= $want->{$_}
Aresult
11
00
62 0 ▶ die( "$template enables " . join( ', ', @both ) . ", which retired names too: a policy is either in the template or retired\n" ) if @both;
Branch: 1 branch
BranchTrueFalseTotal
if @both 0 0 0
63 0 return @retired;
64 }
65  
66  
67 0 ▶sub enabled_retired ( $self, $text, @retired ) {
Subroutine enabled_retired: not called CC=2 SCAR=17.9
Pod documented
68 0 my $have = policies( $text, $self->config->{file} );
69 0 ▶ my @enabled = sort grep { ( $have->{$_} // q{} ) eq 'enabled' } @retired;
Condition0%0/2
$have->{$_} // ""
l!l
00
MC/DC0%0/1
$have->{$_} // ""
$have->{$_}
Truth table0%0/2
A
A= $have->{$_}
Aresult
11
00
70 0 return @enabled;
71 }
72  
73  
74 0 ▶sub lacks (@missing) {
Subroutine lacks: not called CC=2 SCAR=17.9
Pod documented
75 0 ▶ return 'lacks the ' . ( @missing == 1 ? 'policy ' : 'policies ' ) . join ', ', @missing;
Branch: 1 branch
BranchTrueFalseTotal
@missing == 1 ? : 0 0 0
76 }
77  
78  
79 0 ▶sub verdict ( $self, $text ) {
Subroutine verdict: not called CC=7 SCAR=40.3
Pod documented
80 0 my $template = $self->config->{template};
81 0 my @retired = $self->retired;
82 0 my ( $missing, $enabled ) = eval { ( [ $self->missing_policies($text) ], [ $self->enabled_retired( $text, @retired ) ] ) };
83 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
84 0 my $detail = Dist::Conformance::Schema::first_line($@);
85 0 return ( 'fail', $detail, "$detail: fix it by hand, or set replace: true to overwrite it" );
86 }
87 0 ▶ return ( 'pass', "has every policy of $template" ) if !@$missing && !@$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless @$missing or @$enabled 0 0 0
Condition0%0/3
@$missing or @$enabled
l!l&&r!l&&!r
000
MC/DC0%0/2
@$missing or @$enabled
@$missing @$enabled
Truth table0%0/3
A or B
A= @$missingB= @$enabled
ABresult
1X1
011
000
88 0 my @details;
89 0 ▶ push @details, lacks(@$missing) . " of $template" if @$missing;
Branch: 1 branch
BranchTrueFalseTotal
if @$missing 0 0 0
90 0 ▶ push @details, 'enables the retired ' . ( @$enabled == 1 ? 'policy ' : 'policies ' ) . join( ', ', @$enabled ) . ', which conformance update removes' if @$enabled;
Branch: 2 branches
BranchTrueFalseTotal
if @$enabled 0 0 0
@$enabled == 1 ? : 0 0 0
91 0 return ( 'fail', join( '; ', @details ), undef, $missing, $enabled );
92 }
93  
94  
95 0 ▶sub header_of ($line) {
Subroutine header_of: not called CC=1 SCAR=6.9
Pod documented
96 0 my ($section) = $line =~ s/\s;\s.+$//r =~ m/\A\s*\[\s*(.+?)\s*\]\s*$/;
97 0 return $section;
98 }
99  
100  
101 0 ▶sub ignored ($line) {
Subroutine ignored: not called CC=1 SCAR=6.9
Pod documented
102 0 return $line =~ m/\A\s*(?:[\#;]|$)/;
103 }
104  
105  
106 0 ▶sub sections ($text) {
Subroutine sections: not called CC=7 SCAR=40.3
Pod documented
107 0 my ( %sections, $name );
108 0 for my $line ( split /^/, $text ) {
109 0 ▶ if ( defined( my $section = header_of($line) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $section = header_of($line))) 0 0 0
110 0 my ( $off, $policy ) = policy_of($section);
111 0 ▶ $name = $off ? undef : $policy;
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
112 0 ▶ $sections{$name} = q{} if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
113 0 next;
114 }
115 0 ▶ next if !defined $name || ignored($line);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or ignored($line) 0 0 0
Condition0%0/3
not defined $name or ignored($line)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $name or ignored($line)
not defined $name ignored($line)
Truth table0%0/3
not(A) or B
A= not defined $nameB= ignored($line)
ABresult
1X1
011
000
116 0 $sections{$name} .= $line =~ s/\n?\z/\n/r;
117 }
118 0 return { map { $_ => "[$_]\n$sections{$_}" } keys %sections };
119 }
120  
121  
122 0 ▶sub without_sections ( $text, @names ) {
Subroutine without_sections: not called CC=12 SCAR=50.5
Pod documented
123 0 my %gone = map { $_ => 1 } @names;
124 0 my @lines = split /^/, $text;
125 0 my %remove;
126 0 for my $start ( grep { defined header_of( $lines[$_] ) } 0 .. $#lines ) {
127 0 my ( $off, $policy ) = policy_of( header_of( $lines[$start] ) );
128 0 ▶ next if $off || !$gone{$policy};
Branch: 1 branch
BranchTrueFalseTotal
if $off or not $gone{$policy} 0 0 0
Condition0%0/3
$off or not $gone{$policy}
l!l&&r!l&&!r
000
MC/DC0%0/2
$off or not $gone{$policy}
$off not $gone{$policy}
Truth table0%0/3
A or not(B)
A= $offB= not $gone{$policy}
ABresult
1X1
011
000
129 0 my $end = $start + 1;
130 0 ▶ $end++ while $end <= $#lines && !defined header_of( $lines[$end] );
Branch: 1 branch
BranchTrueFalseTotal
$end <= $#lines and not defined header_of($lines[$end]) and do { ++$end } 0 0 0
Condition0%0/3
$end <= $#lines and not defined header_of($lines[$end])
!ll&&!rl&&r
000
MC/DC0%0/2
$end <= $#lines and not defined header_of($lines[$end])
$end <= $#lines not defined header_of($lines[$end])
Truth table0%0/3
A and not(B)
A= $end <= $#linesB= not defined header_of($lines[$end])
ABresult
0X0
100
111
131 0 ▶ $end-- while $end > $start + 1 && ignored( $lines[ $end - 1 ] );
Branch: 1 branch
BranchTrueFalseTotal
$end > $start + 1 and ignored($lines[$end - 1]) and do { --$end } 0 0 0
Condition0%0/3
$end > $start + 1 and ignored($lines[$end - 1])
!ll&&!rl&&r
000
MC/DC0%0/2
$end > $start + 1 and ignored($lines[$end - 1])
$end > $start + 1 ignored($lines[$end - 1])
Truth table0%0/3
A and B
A= $end > $start + 1B= ignored($lines[$end - 1])
ABresult
0X0
100
111
132 0 my $above = $start;
133 0 ▶ $above-- while $above > 0 && $lines[ $above - 1 ] =~ m/\A\s*[\#;]/;
Branch: 1 branch
BranchTrueFalseTotal
$above > 0 and $lines[$above - 1] =~ /\A\s*[\#;]/aasx and do { --$above } 0 0 0
Condition0%0/3
$above > 0 and $lines[$above - 1] =~ /\A\s*[\#;]/aasx
!ll&&!rl&&r
000
MC/DC0%0/2
$above > 0 and $lines[$above - 1] =~ /\A\s*[\#;]/aasx
$above > 0 $lines[$above - 1] =~ /\A\s*[\#;]/aasx
Truth table0%0/3
A and B
A= $above > 0B= $lines[$above - 1] =~ /\A\s*[\#;]/aasx
ABresult
0X0
100
111
134 0 ▶ $above = $start if $above == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $above == 0 0 0 0
135 0 $remove{$_} = 1 for $above .. $end - 1;
136 }
137 0 return join q{}, @lines[ grep { !$remove{$_} } 0 .. $#lines ];
138 }
139  
140  
141 0 ▶sub check ( $self, $snapshot ) {
Subroutine check: not called CC=2 SCAR=17.9
Pod documented
142 0 my $file = $self->config->{file};
143 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
144 0 my ( $status, $detail ) = $self->verdict($text);
145 0 return ( $status, $detail );
146 }
147  
148  
149 0 ▶sub update ( $self, $update ) {
Subroutine update: not called CC=8 SCAR=42.8
Pod documented
150 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
151 0 my $text = $update->snapshot->content($file);
152 0 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 0 0 0
153 0 my ( $status, undef, $by_hand, $missing, $enabled ) = $self->verdict($text);
154 0 ▶ return if $status eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "pass" 0 0 0
155 0 ▶ if ( !$self->config->{replace} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->{"replace"}) 0 0 0
156 0 ▶ return $update->by_hand($by_hand) if defined $by_hand;
Branch: 1 branch
BranchTrueFalseTotal
if defined $by_hand 0 0 0
157 0 my $edited = without_sections( $text, @$enabled );
158 0 ▶ $edited = $self->append_policies( $update, $edited, @$missing ) // $edited if @$missing;
Branch: 1 branch
BranchTrueFalseTotal
if @$missing 0 0 0
Condition0%0/3
$self->append_policies($update, $edited, @$missing) // $edited
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->append_policies($update, $edited, @$missing) // $edited
$self->append_policies($update, $edited, @$missing) $edited
Truth table0%0/3
A // B
A= $self->append_policies($update, $edited, @$missing)B= $edited
ABresult
1X1
011
000
159 0 ▶ $update->write( $file, $edited ) if $edited ne $text;
Branch: 1 branch
BranchTrueFalseTotal
if $edited ne $text 0 0 0
160 0 return;
161 }
162 }
163 0 $self->write_template( $update, $file, $template );
164 0 return;
165 }
166  
167  
168 0 ▶sub append_policies ( $self, $update, $text, @missing ) {
Subroutine append_policies: not called CC=3 SCAR=24.8
Pod documented
169 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
170 0 my ( $policies, @unfilled ) = $self->template($template);
171 0 ▶ if (@unfilled) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unfilled) 0 0 0
172 0 $update->by_hand( 'Add the policies ' . join( ', ', @missing ) . " of $template to $file by hand, or set the variables " . join( ', ', @unfilled ) . ' in .conformance.yml: nothing sets them' );
173 0 return;
174 }
175 0 my $sections = sections($policies);
176 0 ▶ $text .= "\n" if $text =~ m/[^\n]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[^\n]\z/aasx 0 0 0
177 0 return $text . join q{}, @{$sections}{@missing};
178 }
179  
180 __END__
« Conformance/Check/SystemPackages.pm ↑ Summary Conformance/Check/GitHooks.pm »