blib/lib/App/GitHooks/Plugin/PerlCritic.pm

filter: Statementstmt 91.6% 44 / 48 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 86.6% 65 / 75 CC 7 SCAR 11.3
CC
7
Cov
85%
run_pre_commit_file
6.7
__ANON__
2.3
__ANON__
2.0
CRAP 3.1
↑ Summary
1 package App::GitHooks::Plugin::PerlCritic;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use base 'App::GitHooks::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # External dependencies.
9 8 ▶use Perl::Critic;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Perl::Critic::Git;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Internal dependencies.
14 8 ▶use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 =head1 NAME
18  
19 App::GitHooks::Plugin::PerlCritic - Verify that all changes and addition to the Perl files pass PerlCritic checks.
20  
21  
22 =head1 DESCRIPTION
23  
24 PerlCritic is a static source code analysis tool. This plugin calls PerlCritic
25 and verifies that the staged modifications do not trigger any violations.
26  
27 Note that if you stage half of a file for committing, this plugin will
28 correctly only check for code violations in the staged half.
29  
30  
31 =head1 VERSION
32  
33 Version 1.1.0
34  
35 =cut
36  
37 8 our $VERSION = '1.1.0';
38  
39 # Tweak the format of the violations reported by PerlCritic.
40 # Note: the PerlCritic package unfortunately doesn't honor .perlcriticrc,
41 # unlike the perlcritic executable.
42 8 Perl::Critic::Violation::set_format( "%m at line %l column %c. %e. (Severity: %s, %p)\n" );
43  
44  
45 =head1 METHODS
46  
47 =head2 get_file_pattern()
48  
49 Return a pattern to filter the files this plugin should analyze.
50  
51 my $file_pattern = App::GitHooks::Plugin::PerlCritic->get_file_pattern(
52 app => $app,
53 );
54  
55 =cut
56  
57 sub get_file_pattern
58 6 ▶{
Subroutine get_file_pattern: called CC=1 SCAR=0.0
Pod documented
59 6 return qr/\.(?:pl|pm|t|cgi)$/x;
60 }
61  
62  
63 =head2 get_file_check_description()
64  
65 Return a description of the check performed on files by the plugin and that
66 will be displayed to the user, if applicable, along with an indication of the
67 success or failure of the plugin.
68  
69 my $description = App::GitHooks::Plugin::PerlCritic->get_file_check_description();
70  
71 =cut
72  
73 sub get_file_check_description
74 5 ▶{
Subroutine get_file_check_description: called CC=1 SCAR=0.0
Pod documented
75 5 return "The file passes Perl::Critic's review.";
76 }
77  
78  
79 =head2 run_pre_commit_file()
80  
81 Code to execute for each file as part of the pre-commit hook.
82  
83 my $success = App::GitHooks::Plugin::PerlCritic->run_pre_commit_file();
84  
85 =cut
86  
87 sub run_pre_commit_file
88 2 ▶{
Subroutine run_pre_commit_file: called CC=6 SCAR=19.0
Pod documented
89 2 my ( $class, %args ) = @_;
90 2 my $file = delete( $args{'file'} );
91 2 my $git_action = delete( $args{'git_action'} );
92 2 my $app = delete( $args{'app'} );
93 2 my $staged_changes = $app->get_staged_changes();
94 2 my $repository = $app->get_repository();
95  
96 # Ignore deleted files.
97 2 ▶ return $PLUGIN_RETURN_SKIPPED
Branch: 1 branch
BranchTrueFalseTotal
if $git_action eq "D" 0 2 2
98 if $git_action eq 'D';
99  
100 # Ignore merges, since they correspond mostly to code written by other people.
101 2 ▶ return $PLUGIN_RETURN_SKIPPED
Branch: 1 branch
BranchTrueFalseTotal
if $staged_changes->is_merge 0 2 2
102 if $staged_changes->is_merge();
103  
104 # Ignore revert commits.
105 2 ▶ return $PLUGIN_RETURN_SKIPPED
Branch: 1 branch
BranchTrueFalseTotal
if $staged_changes->is_revert 0 2 2
106 if $staged_changes->is_revert();
107  
108 # Get PerlCritic violations for uncommitted files only.
109 2 my $error = undef;
110 my $violations = try
111 2 ▶ {
Subroutine __ANON__: called CC=2 SCAR=8.4
112 2 ▶ if ( $git_action eq 'A' )
Branch: 1 branch
BranchTrueFalseTotal
if ($git_action eq 'A') { } 2 0 2
113 {
114 2 my $critic = Perl::Critic->new();
115 2 return [ $critic->critique( $file ) ];
116 }
117 else
118 {
119 0 my $git_critic = Perl::Critic::Git->new(
120 file => $repository->work_tree() . '/' . $file,
121 );
122  
123 0 return $git_critic->report_violations(
124 author => 'not.committed.yet',
125 );
126 }
127 }
128 catch
129 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 # Unless PPI dies, we should never get into this catch() part.
131 # uncoverable subroutine
132 0 $error = $_;
133 0 return [];
134 2 };
135  
136 2 ▶ die "Failed to run PerlCritic: $error.\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 2 2
137 if defined( $error );
138  
139 2 ▶ die "Violations found:\n" . join( '', map { $_->to_string() } @$violations ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$violations != 0 1 1 2
140 if scalar( @$violations ) != 0;
141  
142 1 return $PLUGIN_RETURN_PASSED;
143 }
144  
145  
146 =head1 BUGS
147  
148 Please report any bugs or feature requests through the web interface at
149 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PerlCritic/issues/new>.
150 I will be notified, and then you'll automatically be notified of progress on
151 your bug as I make changes.
152  
153  
154 =head1 SUPPORT
155  
156 You can find documentation for this module with the perldoc command.
157  
158 perldoc App::GitHooks::Plugin::PerlCritic
159  
160  
161 You can also look for information at:
162  
163 =over
164  
165 =item * GitHub's request tracker
166  
167 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PerlCritic/issues>
168  
169 =item * AnnoCPAN: Annotated CPAN documentation
170  
171 L<http://annocpan.org/dist/app-githooks-plugin-perlcritic>
172  
173 =item * CPAN Ratings
174  
175 L<http://cpanratings.perl.org/d/app-githooks-plugin-perlcritic>
176  
177 =item * MetaCPAN
178  
179 L<https://metacpan.org/release/App-GitHooks-Plugin-PerlCritic>
180  
181 =back
182  
183  
184 =head1 AUTHOR
185  
186 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
187 C<< <aubertg at cpan.org> >>.
188  
189  
190 =head1 COPYRIGHT & LICENSE
191  
192 Copyright 2013-2017 Guillaume Aubert.
193  
194 This code is free software; you can redistribute it and/or modify it under the
195 same terms as Perl 5 itself.
196  
197 This program is distributed in the hope that it will be useful, but WITHOUT ANY
198 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
199 PARTICULAR PURPOSE. See the LICENSE file for more details.
200  
201 =cut
202  
203 8 1;
↑ Summary