PPI/Normal/Standard.pm

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