lib/Perl/Critic/Policy/CodeLayout/RequireTidyCode.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 75.0% 9 / 12 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 16 / 16 Podpod 83.3% 5 / 6 totaltotal 89.9% 98 / 109 CC 9 SCAR 11.2
CC
9
Cov
92%
violates
7.2
initialize_if_enabled
3.0
BEGIN
1.0
CRAP 3.1
« lib/Perl/Critic/Policy/CodeLayout/RequireConsistentNewlines.pm ↑ Summary lib/Perl/Critic/Violation.pm »
1 package Perl::Critic::Policy::CodeLayout::RequireTidyCode;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use Perl::Tidy qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use Perl::Critic::Exception::Fatal::Generic qw{ throw_generic };
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Perl::Critic::Utils qw{ :booleans :characters :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 our $VERSION = '1.156';
18  
19 #-----------------------------------------------------------------------------
20  
21 40 Readonly::Scalar my $DESC => q{Code is not tidy};
22 40 Readonly::Scalar my $EXPL => [ 33 ];
23  
24 #-----------------------------------------------------------------------------
25  
26 97 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
27 return (
28 {
29 97 name => 'perltidyrc',
30 description => 'The Perl::Tidy configuration file to use, if any.',
31 default_string => undef,
32 },
33 );
34 }
35  
36 76 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
37 83 ▶sub default_themes { return qw(core pbp cosmetic) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
38 7 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
39  
40 #-----------------------------------------------------------------------------
41  
42 27 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=3 SCAR=11.0
Pod documented
43 27 my ($self, undef) = @_;
44  
45 # Set configuration if defined
46 27 ▶ if (defined $self->{_perltidyrc} && $self->{_perltidyrc} eq $EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_perltidyrc"} and $self->{"_perltidyrc"} eq $Perl::Critic::Policy::CodeLayout::RequireTidyCode::EMPTY) 7 20 27
Condition66%2/3
defined $self->{"_perltidyrc"} and $self->{"_perltidyrc"} eq $Perl::Critic::Policy::CodeLayout::RequireTidyCode::EMPTY
!ll&&!rl&&r
2007
MC/DC50%1/2
defined $self->{"_perltidyrc"} and $self->{"_perltidyrc"} eq $Perl::Critic::Policy::CodeLayout::RequireTidyCode::EMPTY
defined $self->{"_perltidyrc"} $self->{"_perltidyrc"} eq $Perl::Critic::Policy::CodeLayout::RequireTidyCode::EMPTY
Truth table66%2/3
A and B
A= defined $self->{"_perltidyrc"}B= $self->{"_perltidyrc"} eq $Perl::Critic::Policy::CodeLayout::RequireTidyCode::EMPTY
ABresult
0X0
100
111
47 7 my $rc = $EMPTY;
48 7 $self->{_perltidyrc} = \$rc;
49 }
50  
51 27 return $TRUE;
52 }
53  
54 #-----------------------------------------------------------------------------
55  
56 7 ▶sub violates {
Subroutine violates: called CC=7 SCAR=19.8
Pod documented
57 7 my ( $self, $elem, $doc ) = @_;
58  
59 # Perl::Tidy seems to produce slightly different output, depending
60 # on the trailing whitespace in the input. As best I can tell,
61 # Perl::Tidy will truncate any extra trailing newlines, and if the
62 # input has no trailing newline, then it adds one. But when you
63 # re-run it through Perl::Tidy here, that final newline gets lost,
64 # which causes the policy to insist that the code is not tidy.
65 # This only occurs when Perl::Tidy is writing the output to a
66 # scalar, but does not occur when writing to a file. I may
67 # investigate further, but for now, this seems to do the trick.
68  
69 7 my $source = $doc->serialize();
70 7 $source =~ s{ \s+ \Z}{\n}xms;
71  
72 # Remove the shell fix code from the top of program, if applicable
73 ## no critic (ProhibitComplexRegexes)
74 7 my $shebang_re = qr< [#]! [^\015\012]+ [\015\012]+ >xms;
75 7 my $shell_re = qr<eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \$[{]1[+]"\$@"}'
76 [ \t]*[\012\015]+ [ \t]* if [^\015\012]+ [\015\012]+ >xms;
77 7 $source =~ s/\A ($shebang_re) $shell_re /$1/xms;
78  
79 7 my $dest = $EMPTY;
80 7 my $stderr = $EMPTY;
81  
82  
83 # Perl::Tidy gets confused if @ARGV has arguments from
84 # another program. Also, we need to override the
85 # stdout and stderr redirects that the user may have
86 # configured in their .perltidyrc file.
87 # Also override -b because we are using dest and source.
88 7 local @ARGV = qw(-nst -nse -nb);
89  
90 # Trap Perl::Tidy errors, just in case it dies
91 7 my $eval_worked = eval {
92  
93 # Beginning with version 20120619, Perl::Tidy modifies $source. So we
94 # make a copy so we can get a good comparison after tidying. Doing an
95 # s/// on $source after the fact appears not to work with previous
96 # versions of Perl::Tidy.
97 7 my $source_copy = $source;
98  
99 # In version 20120619 (and possibly earlier), Perl::Tidy assigns the
100 # stderr parameter directly to *STDERR. So when our $stderr goes out
101 # of scope, the handle gets closed. Subsequent calls to warn() will
102 # then cause a fatal exception. See RT #78182 for more details. In
103 # the meantime, we workaround it by localizing STDERR first.
104 7 local *STDERR = \*STDERR;
105  
106 # Perl::Tidy 20120619 doesn't accept a scalar ref or a glob ref
107 7 ▶ open my $handle, '>', \$stderr
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, ">", \$stderr or throw_generic("error opening scalar: $Perl::Critic::Policy::CodeLayout::RequireTidyCode::OS_ERROR") 7 0 7
108 or throw_generic "error opening scalar: $OS_ERROR";
109 7 $handle = *{$handle}{IO};
110  
111  
112 Perl::Tidy::perltidy(
113 source => \$source_copy,
114 destination => \$dest,
115 stderr => $handle,
116 7 ▶ defined $self->{_perltidyrc} ? (perltidyrc => $self->{_perltidyrc}) : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_perltidyrc'} ? : 6 1 7
117 );
118  
119 7 ▶ close $handle or throw_generic "Failed to close in memory file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or throw_generic("Failed to close in memory file: $Perl::Critic::Policy::CodeLayout::RequireTidyCode::OS_ERROR") 7 0 7
120  
121 7 1;
122 };
123  
124 7 ▶ if ($stderr or not $eval_worked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr or not $eval_worked) 0 7 7
Condition33%1/3
$stderr or not $eval_worked
l!l&&r!l&&!r
007
MC/DC0%0/2
$stderr or not $eval_worked
$stderr not $eval_worked
Truth table33%1/3
A or not(B)
A= $stderrB= not $eval_worked
ABresult
1X1
011
000
125 # Looks like perltidy had problems
126 0 return $self->violation( 'perltidy had errors!!', $EXPL, $elem );
127 }
128  
129 7 ▶ if ( $source ne $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source ne $dest) 2 5 7
130 2 return $self->violation( $DESC, $EXPL, $elem );
131 }
132  
133 5 return; #ok!
134 }
135  
136 40 1;
137  
138 #-----------------------------------------------------------------------------
139  
140 __END__
« lib/Perl/Critic/Policy/CodeLayout/RequireConsistentNewlines.pm ↑ Summary lib/Perl/Critic/Violation.pm »