Conformance/Check/CriticProfile.pm

filter: Statementstmt 21.2% 17 / 80 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 37.5% 6 / 16 Podpod 100% 10 / 10 totaltotal 22.0% 33 / 150 CC 21 SCAR 30.2
CC
21
Cov
14%
sections
56.0
update
30.0
policies
20.0
CRAP 20.6
« Conformance/Update.pm ↑ Summary Conformance/Check/ClaudeSettings.pm »
1 package Dist::Conformance::Check::CriticProfile 0.004;
2  
3 # ABSTRACT: The perlcritic profile enables or turns off every policy of its template
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 header => { type => 'string', minLength => 1 },
23 outdated => { type => 'array', items => { type => 'string', minLength => 1 } },
24 },
25 };
26 }
27  
28  
29 0 ▶sub policies ( $text, $where ) {
Subroutine policies: not called CC=4 SCAR=30.0
Pod documented
30 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
31 0 my %policies;
32 0 for my $section ( grep { $_ ne q{_} } keys %$profile ) {
33 0 my ( $off, $name ) = policy_of($section);
34 0 ▶ $policies{$name} = $off ? 'disabled' : 'enabled';
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
35 }
36 0 return \%policies;
37 }
38  
39  
40 0 ▶sub policy_of ($section) {
Subroutine policy_of: not called CC=1 SCAR=6.9
Pod documented
41 0 my ( $off, $name ) = $section =~ m/\A(-?)(?:Perl::Critic::Policy::)?(.+)\z/;
42 0 return ( !!$off, $name );
43 }
44  
45  
46 0 ▶sub missing_policies ( $self, $text ) {
Subroutine missing_policies: not called CC=2 SCAR=17.9
Pod documented
47 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
48 0 my $want = policies( ( $self->template($template) )[0], $template );
49 0 my $have = policies( $text, $file );
50 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
51 0 return @missing;
52 }
53  
54  
55 0 ▶sub lacks (@missing) {
Subroutine lacks: not called CC=2 SCAR=17.9
Pod documented
56 0 ▶ return 'lacks the ' . ( @missing == 1 ? 'policy ' : 'policies ' ) . join ', ', @missing;
Branch: 1 branch
BranchTrueFalseTotal
@missing == 1 ? : 0 0 0
57 }
58  
59  
60 0 ▶sub verdict ( $self, $text ) {
Subroutine verdict: not called CC=3 SCAR=24.8
Pod documented
61 0 my $template = $self->config->{template};
62 0 my @missing = eval { $self->missing_policies($text) };
63 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
64 0 my $detail = Dist::Conformance::Schema::first_line($@);
65 0 return ( 'fail', $detail, "$detail: fix it by hand, or set replace: true to overwrite it" );
66 }
67 0 ▶ return ( 'pass', "has every policy of $template" ) if !@missing;
Branch: 1 branch
BranchTrueFalseTotal
unless @missing 0 0 0
68 0 return ( 'fail', lacks(@missing) . " of $template", undef, @missing );
69 }
70  
71  
72 0 ▶sub sections ($text) {
Subroutine sections: not called CC=7 SCAR=40.3
Pod documented
73 0 my ( %sections, $name );
74 0 for my $line ( split /^/, $text ) {
75 0 ▶ if ( my ($section) = $line =~ s/\s;\s.+$//r =~ m/\A\s*\[\s*(.+?)\s*\]\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($section) = ($line =~ s/\s;\s.+$//aarsx) =~ /\A\s*\[\s*(.+?)\s*\]\s*$/aasx) 0 0 0
76 0 my ( $off, $policy ) = policy_of($section);
77 0 ▶ $name = $off ? undef : $policy;
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 0 0 0
78 0 ▶ $sections{$name} = q{} if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
79 0 next;
80 }
81 0 ▶ next if !defined $name || $line =~ m/\A\s*(?:[\#;]|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $line =~ /\A\s*(?:[\#;]|$)/aasx 0 0 0
Condition0%0/3
not defined $name or $line =~ /\A\s*(?:[\#;]|$)/aasx
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $name or $line =~ /\A\s*(?:[\#;]|$)/aasx
not defined $name $line =~ /\A\s*(?:[\#;]|$)/aasx
Truth table0%0/3
not(A) or B
A= not defined $nameB= $line =~ /\A\s*(?:[\#;]|$)/aasx
ABresult
1X1
011
000
82 0 $sections{$name} .= $line =~ s/\n?\z/\n/r;
83 }
84 0 return { map { $_ => "[$_]\n$sections{$_}" } keys %sections };
85 }
86  
87  
88 0 ▶sub check ( $self, $snapshot ) {
Subroutine check: not called CC=2 SCAR=17.9
Pod documented
89 0 my $file = $self->config->{file};
90 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
91 0 my ( $status, $detail ) = $self->verdict($text);
92 0 return ( $status, $detail );
93 }
94  
95  
96 0 ▶sub update ( $self, $update ) {
Subroutine update: not called CC=5 SCAR=34.0
Pod documented
97 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
98 0 my $text = $update->snapshot->content($file);
99 0 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 0 0 0
100 0 my ( $status, undef, $by_hand, @missing ) = $self->verdict($text);
101 0 ▶ return if $status eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "pass" 0 0 0
102 0 ▶ if ( !$self->config->{replace} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->{"replace"}) 0 0 0
103 0 ▶ return $self->append_policies( $update, $text, @missing ) if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 0 0
104 0 return $update->by_hand($by_hand);
105 }
106 }
107 0 $self->write_template( $update, $file, $template );
108 0 return;
109 }
110  
111  
112 0 ▶sub append_policies ( $self, $update, $text, @missing ) {
Subroutine append_policies: not called CC=3 SCAR=24.8
Pod documented
113 0 my ( $file, $template ) = @{ $self->config }{qw{file template}};
114 0 my ( $policies, @unfilled ) = $self->template($template);
115 0 ▶ if (@unfilled) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unfilled) 0 0 0
116 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' );
117 0 return;
118 }
119 0 my $sections = sections($policies);
120 0 ▶ $text .= "\n" if $text =~ m/[^\n]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[^\n]\z/aasx 0 0 0
121 0 $update->write( $file, $text . join q{}, @{$sections}{@missing} );
122 0 return;
123 }
124  
125 __END__
« Conformance/Update.pm ↑ Summary Conformance/Check/ClaudeSettings.pm »