blib/lib/Perl/Critic/Policy/CodeLayout/RequireConsistentNewlines.pm

filter: Statementstmt 100% 54 / 54 Branchbran 80.0% 8 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 93.1% 81 / 87 CC 7 SCAR 11.7
CC
7
Cov
96%
violates
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.2
« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm ↑ Summary blib/lib/Perl/Critic/Policy/CodeLayout/RequireTidyCode.pm »
1 package Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines;
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 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use PPI::Token::Whitespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.150';
14  
15 40 Readonly::Scalar my $LINE_END => qr/\015{1,2}\012|[\012\015]/mxs;
16  
17 #-----------------------------------------------------------------------------
18  
19 40 Readonly::Scalar my $DESC => q{Use the same newline through the source};
20 40 Readonly::Scalar my $EXPL => q{Change your newlines to be the same throughout};
21  
22 #-----------------------------------------------------------------------------
23  
24 119 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 139 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
26 74 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
27 62 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
28  
29 #-----------------------------------------------------------------------------
30  
31 sub violates {
32 62 ▶ my ( $self, undef, $doc ) = @_;
Subroutine violates: called CC=7 SCAR=19.5
Pod documented
33  
34 62 my $filename = $doc->filename();
35 62 ▶ return if !$filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 35 27 62
36  
37 27 my $fh;
38 27 ▶ return if !open $fh, '<', $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless open $fh, "<", $filename 0 27 27
39 27 local $RS = undef;
40 27 my $source = <$fh>;
41 27 ▶ close $fh or return;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return 0 27 27
42  
43 27 my $newline; # undef until we find the first one
44 27 my $line = 1;
45 27 my @v;
46 27 ▶ while ( $source =~ m/\G([^\012\015]*)($LINE_END)/cgmxs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /\G([^\012\015]*)($LINE_END)/cgmsx) 561 27 588
47 561 my $code = $1;
48 561 my $nl = $2;
49 561 my $col = length $code;
50 561 ▶ $newline ||= $nl;
Condition66%2/3
$newline ||= $nl
l!l&&r!l&&!r
534270
MC/DC0%0/2
$newline ||= $nl
$newline $nl
Truth table66%2/3
A ||= B
A= $newlineB= $nl
ABresult
1X1
011
000
51 561 ▶ if ( $nl ne $newline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl ne $newline) 64 497 561
52 64 my $token = PPI::Token::Whitespace->new( $nl );
53 # TODO this is a terrible violation of encapsulation, but absent a
54 # mechanism to override the line numbers in the violation, I do
55 # not know what to do about it.
56 64 $token->{_location} = [$line, $col, $col, $line, $filename];
57 64 push @v, $self->violation( $DESC, $EXPL, $token );
58 }
59 561 $line++;
60 }
61 27 return @v;
62 }
63  
64 40 1;
65  
66 #-----------------------------------------------------------------------------
67  
68 __END__
69  
70 =pod
71  
72 =for stopwords GnuPG
73  
74 =head1 NAME
75  
76 Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines - Use the same newline through the source.
77  
78  
79 =head1 AFFILIATION
80  
81 This Policy is part of the core L<Perl::Critic|Perl::Critic>
82 distribution.
83  
84  
85 =head1 DESCRIPTION
86  
87 Source code files are divided into lines with line endings of C<\r>,
88 C<\n> or C<\r\n>. Mixing these different line endings causes problems
89 in many text editors and, notably, Module::Signature and GnuPG.
90  
91  
92 =head1 CAVEAT
93  
94 This policy works outside of PPI because PPI automatically normalizes
95 source code to local newline conventions. So, this will only work if
96 we know the filename of the source code.
97  
98  
99 =head1 CONFIGURATION
100  
101 This Policy is not configurable except for the standard options.
102  
103  
104 =head1 AUTHOR
105  
106 Chris Dolan <cdolan@cpan.org>
107  
108  
109 =head1 COPYRIGHT
110  
111 Copyright (c) 2006-2011 Chris Dolan.
112  
113 This program is free software; you can redistribute it and/or modify
114 it under the same terms as Perl itself.
115  
116 =cut
117  
118 # Local Variables:
119 # mode: cperl
120 # cperl-indent-level: 4
121 # fill-column: 78
122 # indent-tabs-mode: nil
123 # c-indentation-style: bsd
124 # End:
125 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm ↑ Summary blib/lib/Perl/Critic/Policy/CodeLayout/RequireTidyCode.pm »