File Coverage

blib/lib/PPI/Transform/PackageName.pm
Criterion Covered Total %
statement 48 48 100.0
branch 13 18 72.2
condition 8 19 42.1
subroutine 6 6 100.0
pod 2 2 100.0
total 77 93 82.8


line stmt bran cond sub pod time code
1 10     10   51 use strict;
  10         16  
  10         365  
2 10     10   53 use warnings;
  10         16  
  10         594  
3              
4             package PPI::Transform::PackageName;
5              
6             # ABSTRACT: Subclass of PPI::Transform specific for modifying package names
7             our $VERSION = 'v0.0.7'; # VERSION
8              
9 10     10   51 use base qw(PPI::Transform);
  10         16  
  10         10221  
10              
11 10     10   18979 use Carp;
  10         22  
  10         7319  
12              
13             sub new
14             {
15 38     38 1 86 my $self = shift;
16 38   33     287 my $class = ref($self) || $self;
17 38         199 my %arg = @_;
18 38 50       157 if(exists $arg{-all}) {
19 38 50 33     441 croak "-all and other options are contradictory" if exists $arg{-package_name} || exists $arg{-word} || exists $arg{-quote};
      33        
20 38         142 $arg{-package_name} = $arg{-all};
21 38         120 $arg{-word} = $arg{-all};
22 38         104 $arg{-quote} = $arg{-all};
23             }
24 38         352 return bless {
25             _PKG => $arg{-package_name},
26             _WORD => $arg{-word},
27             _QUOTE => $arg{-quote},
28             }, $class;
29             }
30              
31             sub document
32             {
33 38     38 1 584670 my ($self, $doc) = @_;
34 38         300 $doc->prune('PPI::Token::Comment');
35 38 50 33     164971 if(defined $self->{_PKG} && defined $self->{_WORD}) {
36 38         301 my $words = $doc->find('PPI::Token::Word');
37 38   50     182455 $words ||= [];
38 38         313 for my $word (@$words) {
39 646 100 66     3013 if(defined $self->{_PKG} && $word->statement->class eq 'PPI::Statement::Package') {
    50          
40 96         3543 my $content = $word->content;
41 96         1214 $_ = $content;
42 96         867 $self->{_PKG}->();
43 96 100       994 $word->set_content($_) if $_ ne $content;
44             } elsif(defined $self->{_WORD}) {
45 550         12507 my $content = $word->content;
46 550         2487 $_ = $content;
47 550         1326 $self->{_WORD}->();
48 550 100       2341 $word->set_content($_) if $_ ne $content;
49             }
50             }
51             }
52 38 50       240 if(defined $self->{_QUOTE}) {
53 38         172 my $quotes = $doc->find('PPI::Token::Quote');
54 38   50     180007 $quotes ||= [];
55 38         143 for my $quote (@$quotes) {
56 94         578 my $string = $quote->string;
57 94         735 $_ = $string;
58 94         323 $self->{_QUOTE}->();
59 94 100       379 if($_ ne $string) {
60 28         149 my $content = $quote->content;
61 28         205 my $index = index($content, $string);
62 28         175 substr($quote->{content}, $index, length($string)) = $_;
63             }
64             }
65             }
66 38         167 return 1;
67             }
68              
69             1;
70              
71             __END__