File Coverage

blib/lib/PPI/Normal/Standard.pm
Criterion Covered Total %
statement 25 35 71.4
branch 13 36 36.1
condition n/a
subroutine 11 11 100.0
pod 0 5 0.0
total 49 87 56.3


line stmt bran cond sub pod time code
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.
12              
13             There is no reason for you to need to load this yourself.
14              
15             B.
16              
17             =cut
18              
19 63     63   362 use strict;
  63         117  
  63         29953  
20              
21             our $VERSION = '1.275';
22              
23              
24              
25              
26              
27             #####################################################################
28             # Configuration and Registration
29              
30             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 100   63   150 map { /\D/ ? "PPI::Normal::Standard::$_" : $_ } @METHODS
  630 50       2038  
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     11 0 18 my $Document = shift;
54 11     83   49 $Document->prune( sub { ! $_[1]->significant } );
  83         184  
55             }
56              
57             # Remove custom attributes that are not relevant to normalization
58             sub remove_useless_attributes {
59 11     11 0 18 my $Document = shift;
60 11         23 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             my $remove_pragma = map { $_ => 1 } qw{
74             strict warnings diagnostics less
75             };
76             sub remove_useless_pragma {
77 4     4 0 6 my $Document = shift;
78             $Document->prune( sub {
79 15 50   15   55 return '' unless $_[1]->isa('PPI::Statement::Include');
80 0 0       0 return 1 if $_[1]->version;
81 0 0       0 return 1 if $remove_pragma->{$_[1]->pragma};
82 0         0 '';
83 4         13 } );
84             }
85              
86             # Remove all semi-colons at the end of statements
87             sub remove_statement_separator {
88 4     4 0 8 my $Document = shift;
89             $Document->prune( sub {
90 15 100   15   49 $_[1]->isa('PPI::Token::Structure') or return '';
91 3 50       8 $_[1]->content eq ';' or return '';
92 3 50       9 my $stmt = $_[1]->parent or return '';
93 3 50       13 $stmt->isa('PPI::Statement') or return '';
94 3 50       11 $_[1]->next_sibling and return '';
95 3         14 1;
96 4         15 } );
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     4 0 9 my $Document = shift;
104             $Document->prune( sub {
105 12 100   12   46 $_[1]->isa('PPI::Token::Word') or return '';
106 4 50       9 $_[1]->content eq 'return' or return '';
107 0 0         my $stmt = $_[1]->parent or return '';
108 0 0         $stmt->isa('PPI::Statement::Break') or return '';
109 0 0         $stmt->children == 2 or return '';
110 0 0         $stmt->next_sibling and return '';
111 0 0         my $block = $stmt->parent or return '';
112 0 0         $block->isa('PPI::Structure::Block') or return '';
113 0           1;
114 4         13 } );
115             }
116              
117             1;
118              
119             =pod
120              
121             =head1 SUPPORT
122              
123             See the L in the main module.
124              
125             =head1 AUTHOR
126              
127             Adam Kennedy Eadamk@cpan.orgE
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