PPI/Normal/Standard.pm

filter: Statementstmt 75.0% 30 / 40 Branchbran 36.1% 13 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 58.7% 54 / 92 CC 19 SCAR 29.6
CC
19
Cov
57%
__ANON__
50.5
__ANON__
12.2
__ANON__
6.6
CRAP 19.3
« PPI/Token/HereDoc.pm ↑ Summary PPI/Token/Magic.pm »
1 package PPI::Normal::Standard;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Normal::Standard - Provides standard document normalization functions
8  
9 =head1 DESCRIPTION
10  
11 This module provides the default normalization methods for L<PPI::Normal>.
12  
13 There is no reason for you to need to load this yourself.
14  
15 B<Move along, nothing to see here>.
16  
17 =cut
18  
19 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 63 our $VERSION = '1.260'; # VERSION
22  
23  
24  
25  
26  
27 #####################################################################
28 # Configuration and Registration
29  
30 63 my @METHODS = (
31 remove_insignificant_elements => 1,
32 remove_useless_attributes => 1,
33 remove_useless_pragma => 2,
34 remove_statement_separator => 2,
35 remove_useless_return => 2,
36 );
37  
38 sub import {
39 PPI::Normal->register(
40 63 ▶ map { /\D/ ? "PPI::Normal::Standard::$_" : $_ } @METHODS
Subroutine import: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
"PPI::Normal"->register(map({/\D/ ? "PPI::Normal::Standard::$_" : $_;} @METHODS)) or die "Failed to register PPI::Normal::Standard transforms" 0 63 63
/\D/ ? : 315 315 630
41 ) or die "Failed to register PPI::Normal::Standard transforms";
42 }
43  
44  
45  
46  
47  
48 #####################################################################
49 # Level 1 Transforms
50  
51 # Remove all insignificant elements
52 sub remove_insignificant_elements {
53 11 ▶ my $Document = shift;
Subroutine remove_insignificant_elements: called CC=1 SCAR=0.0
Pod undocumented
54 11 ▶ $Document->prune( sub { ! $_[1]->significant } );
Subroutine __ANON__: called CC=1 SCAR=0.0
55 }
56  
57 # Remove custom attributes that are not relevant to normalization
58 sub remove_useless_attributes {
59 11 ▶ my $Document = shift;
Subroutine remove_useless_attributes: called CC=1 SCAR=0.0
Pod undocumented
60 11 delete $Document->{tab_width};
61  
62 ### FIXME - Add support for more things
63 }
64  
65  
66  
67  
68  
69 #####################################################################
70 # Level 2 Transforms
71  
72 # Remove version dependencies and pragma
73 63 my $remove_pragma = map { $_ => 1 } qw{
74 strict warnings diagnostics less
75 };
76 sub remove_useless_pragma {
77 4 ▶ my $Document = shift;
Subroutine remove_useless_pragma: called CC=1 SCAR=2.6
Pod undocumented
78 $Document->prune( sub {
79 15 ▶ return '' unless $_[1]->isa('PPI::Statement::Include');
Subroutine __ANON__: called CC=4 SCAR=25.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("PPI::Statement::Include") 15 0 15
80 0 ▶ return 1 if $_[1]->version;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]->version 0 0 0
81 0 ▶ return 1 if $remove_pragma->{$_[1]->pragma};
Branch: 1 branch
BranchTrueFalseTotal
if $$remove_pragma{$_[1]->pragma} 0 0 0
82 0 '';
83 4 } );
84 }
85  
86 # Remove all semi-colons at the end of statements
87 sub remove_statement_separator {
88 4 ▶ my $Document = shift;
Subroutine remove_statement_separator: called CC=1 SCAR=0.1
Pod undocumented
89 $Document->prune( sub {
90 15 ▶ $_[1]->isa('PPI::Token::Structure') or return '';
Subroutine __ANON__: called CC=6 SCAR=18.8
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Structure") or return "" 12 3 15
91 3 ▶ $_[1]->content eq ';' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->content eq ";" or return "" 0 3 3
92 3 ▶ my $stmt = $_[1]->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $_[1]->parent or return "" 0 3 3
93 3 ▶ $stmt->isa('PPI::Statement') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$stmt->isa("PPI::Statement") or return "" 0 3 3
94 3 ▶ $_[1]->next_sibling and return '';
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->next_sibling and return "" 0 3 3
95 3 1;
96 4 } );
97 }
98  
99 # In any block, the "return" in the last statement is not
100 # needed if there is only one and only one thing after the
101 # return.
102 sub remove_useless_return {
103 4 ▶ my $Document = shift;
Subroutine remove_useless_return: called CC=1 SCAR=3.4
Pod undocumented
104 $Document->prune( sub {
105 12 ▶ $_[1]->isa('PPI::Token::Word') or return '';
Subroutine __ANON__: called CC=9 SCAR=39.2
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") or return "" 8 4 12
106 4 ▶ $_[1]->content eq 'return' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->content eq "return" or return "" 4 0 4
107 0 ▶ my $stmt = $_[1]->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $_[1]->parent or return "" 0 0 0
108 0 ▶ $stmt->isa('PPI::Statement::Break') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$stmt->isa("PPI::Statement::Break") or return "" 0 0 0
109 0 ▶ $stmt->children == 2 or return '';
Branch: 1 branch
BranchTrueFalseTotal
$stmt->children == 2 or return "" 0 0 0
110 0 ▶ $stmt->next_sibling and return '';
Branch: 1 branch
BranchTrueFalseTotal
$stmt->next_sibling and return "" 0 0 0
111 0 ▶ my $block = $stmt->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $block = $stmt->parent or return "" 0 0 0
112 0 ▶ $block->isa('PPI::Structure::Block') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$block->isa("PPI::Structure::Block") or return "" 0 0 0
113 0 1;
114 4 } );
115 }
116  
117 63 1;
118  
119 =pod
120  
121 =head1 SUPPORT
122  
123 See the L<support section|PPI/SUPPORT> in the main module.
124  
125 =head1 AUTHOR
126  
127 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
128  
129 =head1 COPYRIGHT
130  
131 Copyright 2005 - 2011 Adam Kennedy.
132  
133 This program is free software; you can redistribute
134 it and/or modify it under the same terms as Perl itself.
135  
136 The full text of the license can be found in the
137 LICENSE file included with this module.
138  
139 =cut
« PPI/Token/HereDoc.pm ↑ Summary PPI/Token/Magic.pm »