lib/Code/TidyAll/Plugin.pm

filter: Statementstmt 97.4% 114 / 117 Branchbran 91.6% 22 / 24 Conditioncond 66.6% 12 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 92.5% 25 / 27 Podpod 33.3% 2 / 6 totaltotal 89.1% 181 / 203 CC 22 SCAR 13.7
CC
22
Cov
93%
_build_is_tidier
12.0
process_source_or_file
9.0
__ANON__
4.0
CRAP 3.9
« blib/lib/Code/TidyAll/Git/Prereceive.pm ↑ Summary blib/lib/Code/TidyAll/Plugin/JSHint.pm »
1 package Code::TidyAll::Plugin;
2  
3 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 58 ▶use Code::TidyAll::Util::Zglob qw(zglobs_to_regex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 58 ▶use IPC::Run3 qw(run3);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 58 ▶use Specio::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 58 ▶use Specio::Library::Builtins;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 58 ▶use Specio::Library::Numeric;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 58 ▶use Specio::Library::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 58 ▶use Text::Diff 1.44 qw(diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 58 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 58 our $VERSION = '0.85';
19  
20 58 has argv => (
21 is => 'ro',
22 isa => t('Str'),
23 default => q{}
24 );
25  
26 # This really belongs in Code::TidyAll::Role::RunsCommand but moving it there
27 # breaks plugins not in the core distro that expect to just inherit from this
28 # module and be able to specify a cmd attribute.
29 58 has cmd => (
30 is => 'ro',
31 isa => t('NonEmptyStr'),
32 lazy => 1,
33 builder => '_build_cmd',
34 );
35  
36 58 has diff_on_tidy_error => (
37 is => 'ro',
38 isa => t('Bool'),
39 default => 0
40 );
41  
42 58 has is_tidier => (
43 is => 'lazy',
44 isa => t('Bool'),
45 );
46  
47 58 has is_validator => (
48 is => 'lazy',
49 isa => t('Bool'),
50 );
51  
52 58 has name => (
53 is => 'ro',
54 required => 1
55 );
56  
57 58 has select => (
58 is => 'lazy',
59 isa => t( 'ArrayRef', of => t('NonEmptyStr') ),
60 );
61  
62 58 has select_regex => (
63 is => 'lazy',
64 isa => t('RegexpRef'),
65 );
66  
67 58 has selects => (
68 is => 'lazy',
69 isa => t( 'ArrayRef', of => t('NonEmptyStr') ),
70 );
71  
72 58 has shebang => (
73 is => 'ro',
74 isa => t( 'ArrayRef', of => t('NonEmptyStr') ),
75 );
76  
77 58 has tidyall => (
78 is => 'ro',
79  
80 # This should be "object_isa_type( class => 'Code::TidyAll' )" but then
81 # we'd need to load Code::TidyAll, leading to a circular require
82 # situation.
83 isa => t('Object'),
84 required => 1,
85 weak_ref => 1
86 );
87  
88 58 has weight => (
89 is => 'lazy',
90 isa => t('PositiveOrZeroInt'),
91 );
92  
93 58 with 'Code::TidyAll::Role::HasIgnore';
94  
95 120 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
96 120 my $orig = shift;
97 120 my $class = shift;
98  
99 120 my $args = $class->$orig(@_);
100  
101 120 for my $key (qw( ignore select shebang )) {
102 360 ▶ if ( defined $args->{$key} && !ref $args->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$key} and not ref $args->{$key}) 114 246 360
Condition100%3/3
defined $args->{$key} and not ref $args->{$key}
!ll&&!rl&&r
19947114
MC/DC100%2/2
defined $args->{$key} and not ref $args->{$key}
defined $args->{$key} not ref $args->{$key}
Truth table100%3/3
A and not(B)
A= defined $args->{$key}B= not ref $args->{$key}
ABresult
0X0
100
111
103 114 $args->{$key} = [ $args->{$key} ];
104 }
105 }
106  
107 120 return $args;
108 58 };
109  
110 0 ▶sub _build_cmd {
Subroutine _build_cmd: not called CC=1 SCAR=6.9
111 0 die 'no default cmd specified';
112 }
113  
114 115 ▶sub _build_selects {
Subroutine _build_selects: called CC=1 SCAR=0.0
115 115 my ($self) = @_;
116 115 return $self->_parse_zglob_list( $self->select );
117 }
118  
119 85 ▶sub _build_select_regex {
Subroutine _build_select_regex: called CC=1 SCAR=0.0
120 85 my ($self) = @_;
121 85 return zglobs_to_regex( @{ $self->selects } );
122 }
123  
124 0 ▶sub _build_is_tidier {
Subroutine _build_is_tidier: not called CC=3 SCAR=24.8
125 0 my ($self) = @_;
126 0 ▶ return ( $self->can('transform_source') || $self->can('transform_file') ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('transform_source') || $self->can('transform_file') ? : 0 0 0
Condition0%0/3
$self->can("transform_source") or $self->can("transform_file")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->can("transform_source") or $self->can("transform_file")
$self->can("transform_source") $self->can("transform_file")
Truth table0%0/3
A or B
A= $self->can("transform_source")B= $self->can("transform_file")
ABresult
1X1
011
000
127 }
128  
129 38 ▶sub _build_is_validator {
Subroutine _build_is_validator: called CC=3 SCAR=11.1
130 38 my ($self) = @_;
131 38 ▶ return ( $self->can('validate_source') || $self->can('validate_file') ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('validate_source') || $self->can('validate_file') ? : 4 34 38
Condition66%2/3
$self->can("validate_source") or $self->can("validate_file")
l!l&&r!l&&!r
4034
MC/DC50%1/2
$self->can("validate_source") or $self->can("validate_file")
$self->can("validate_source") $self->can("validate_file")
Truth table66%2/3
A or B
A= $self->can("validate_source")B= $self->can("validate_file")
ABresult
1X1
011
000
132 }
133  
134 # default weight
135 38 ▶sub _build_weight {
Subroutine _build_weight: called CC=2 SCAR=6.9
136 38 my ($self) = @_;
137 38 ▶ return 60 if $self->is_validator;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_validator 4 34 38
138 34 return 50;
139 }
140  
141 120 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
142 120 my ( $self, $params ) = @_;
143  
144 # Strict constructor
145 #
146 120 $self->validate_params($params);
147 }
148  
149 120 ▶sub validate_params {
Subroutine validate_params: called CC=3 SCAR=11.0
Pod undocumented
150 120 my ( $self, $params ) = @_;
151  
152 120 delete( $params->{only_modes} );
153 120 delete( $params->{except_modes} );
154 120 ▶ if ( my @bad_params = grep { !$self->can($_) } keys(%$params) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_params) = grep({not $self->can($_);} keys %$params)) 2 118 120
155 die sprintf(
156 q{unknown option%s %s for plugin '%s'},
157 @bad_params > 1 ? 's' : q{},
158 2 ▶ join( ', ', sort map {qq['$_']} @bad_params ),
Branch: 1 branch
BranchTrueFalseTotal
@bad_params > 1 ? : 1 1 2
159 $self->name
160 );
161 }
162 }
163  
164 # No-ops by default; may be overridden in subclass
165 164 ▶sub preprocess_source {
Subroutine preprocess_source: called CC=1 SCAR=0.0
Pod documented
166 164 return $_[1];
167 }
168  
169 146 ▶sub postprocess_source {
Subroutine postprocess_source: called CC=1 SCAR=0.0
Pod documented
170 146 return $_[1];
171 }
172  
173 165 ▶sub process_source_or_file {
Subroutine process_source_or_file: called CC=9 SCAR=22.0
Pod undocumented
174 165 my ( $self, $orig_source, $rel_path, $check_only ) = @_;
175  
176 165 my $new_source = $orig_source;
177 165 ▶ if ( $self->can('transform_source') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("transform_source")) 138 27 165
178 138 foreach my $iter ( 1 .. $self->tidyall->iterations ) {
179 139 $new_source = $self->transform_source($new_source);
180 }
181 }
182 157 ▶ if ( $self->can('transform_file') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("transform_file")) 11 146 157
183 11 my $tempfile = $self->_write_temp_file( $rel_path, $new_source );
184 11 foreach my $iter ( 1 .. $self->tidyall->iterations ) {
185 11 $self->transform_file($tempfile);
186 }
187 10 $new_source = $tempfile->slurp_raw;
188 }
189 156 ▶ if ( $self->can('validate_source') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("validate_source")) 4 152 156
190 4 $self->validate_source($new_source);
191 }
192 155 ▶ if ( $self->can('validate_file') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("validate_file")) 10 145 155
193 10 my $tempfile = $self->_write_temp_file( $rel_path, $new_source );
194 10 $self->validate_file($tempfile);
195 }
196  
197 150 my $diff;
198 150 ▶ if ( $check_only && $new_source ne $orig_source ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_only and $new_source ne $orig_source) 12 138 150
Condition100%3/3
$check_only and $new_source ne $orig_source
!ll&&!rl&&r
1231512
MC/DC100%2/2
$check_only and $new_source ne $orig_source
$check_only $new_source ne $orig_source
Truth table100%3/3
A and B
A= $check_onlyB= $new_source ne $orig_source
ABresult
0X0
100
111
199 12 $diff = $self->_maybe_diff( $orig_source, $new_source, $rel_path );
200 }
201  
202 150 return ( $new_source, $diff );
203 }
204  
205 12 ▶sub _maybe_diff {
Subroutine _maybe_diff: called CC=2 SCAR=6.9
206 12 my $self = shift;
207  
208 12 ▶ return unless $self->diff_on_tidy_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->diff_on_tidy_error 10 2 12
209  
210 2 my $orig = shift;
211 2 my $new = shift;
212 2 my $rel_path = shift;
213  
214 2 my $orig_file = $self->_write_temp_file( $rel_path . '.orig', $orig );
215 2 my $new_file = $self->_write_temp_file( $rel_path . '.new', $new );
216  
217 2 return diff( $orig_file->stringify, $new_file->stringify, { Style => 'Unified' } );
218 }
219  
220 25 ▶sub _write_temp_file {
Subroutine _write_temp_file: called CC=1 SCAR=0.0
221 25 my ( $self, $rel_path, $source ) = @_;
222  
223 25 my $tempfile = $self->tidyall->_tempdir->child($rel_path);
224 25 $tempfile->parent->mkpath( { mode => 0755 } );
225 25 $tempfile->spew_raw($source);
226 25 return $tempfile;
227 }
228  
229 95 ▶sub matches_path {
Subroutine matches_path: called CC=3 SCAR=11.4
Pod undocumented
230 95 my ( $self, $path ) = @_;
231  
232 return
233 95 ▶ $path =~ $self->select_regex
Condition66%4/6
$path =~ $self->select_regex && !($path =~ $self->tidyall->ignore_regex) && !($path =~ $self->ignore_regex)
!ll&&!rl&&r
3092
$path =~ $self->select_regex and not $path =~ $self->tidyall->ignore_regex
!ll&&!rl&&r
3092
MC/DC33%1/3
$path =~ $self->select_regex && !($path =~ $self->tidyall->ignore_regex) && !($path =~ $self->ignore_regex)
$path =~ $self->select_regex not $path =~ $self->tidyall->ignore_regex !($path =~ $self->ignore_regex)
Truth table50%2/4
A and not(B) && not(C)
A= $path =~ $self->select_regexB= not $path =~ $self->tidyall->ignore_regexC= !($path =~ $self->ignore_regex)
ABCresult
0XX0
10X0
1100
1111
234 && $path !~ $self->tidyall->ignore_regex
235 && $path !~ $self->ignore_regex;
236 }
237  
238 58 1;
239  
240 # ABSTRACT: Create plugins for tidying or validating code
241  
242 __END__
« blib/lib/Code/TidyAll/Git/Prereceive.pm ↑ Summary blib/lib/Code/TidyAll/Plugin/JSHint.pm »