File Coverage

blib/lib/PPIx/EditorTools/RenamePackage.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 38 42 90.4


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::RenamePackage;
2              
3             # ABSTRACT: Change the package name
4              
5 3     3   234306 use strict;
  3         9  
  3         127  
6              
7             BEGIN {
8 3     3   62 $^W = 1;
9             }
10 3     3   16 use base 'PPIx::EditorTools';
  3         5  
  3         783  
11              
12 3     3   18 use Class::XSAccessor accessors => { 'replacement' => 'replacement' };
  3         5  
  3         29  
13              
14 3     3   746 use PPI;
  3         6  
  3         68  
15 3     3   16 use Carp;
  3         5  
  3         776  
16              
17             our $VERSION = '0.18';
18              
19             =pod
20              
21             =head1 NAME
22              
23             PPIx::EditorTools::RenamePackage - Change the package name
24              
25             =head1 SYNOPSIS
26              
27             my $munged = PPIx::EditorTools::RenamePackage->new->rename(
28             code => "package TestPackage;\nuse strict;\nBEGIN {
29             $^W = 1;
30             }\n1;\n",
31             replacement => 'NewPackage'
32             );
33              
34             my $new_code_as_string = $munged->code;
35             my $package_ppi_element = $munged->element;
36              
37             =head1 DESCRIPTION
38              
39             This module uses PPI to change the package name of code.
40              
41             =head1 METHODS
42              
43             =over 4
44              
45             =item new()
46              
47             Constructor. Generally shouldn't be called with any arguments.
48              
49             =item rename( ppi => PPI::Document $ppi, replacement => Str )
50             =item rename( code => Str $code, replacement => Str )
51              
52             Accepts either a C to process or a string containing
53             the code (which will be converted into a C) to process.
54             Replaces the package name with that supplied in the C
55             parameter and returns a C with the
56             new code available via the C or C accessors, as a
57             C or C, respectively.
58              
59             Croaks with a "package name not found" exception if unable to find the
60             package name.
61              
62             =back
63              
64             =cut
65              
66             sub rename {
67 6     6 1 1681 my ( $self, %args ) = @_;
68 6         50 $self->process_doc(%args);
69 6   33     24 my $replacement = $args{replacement} || croak "replacement required";
70              
71 6         14 my $doc = $self->ppi;
72              
73             # TODO: support MooseX::Declare
74 6 100       35 my $package = $doc->find_first('PPI::Statement::Package')
75             or die "no package found";
76 5 50       1127 my $namespace = $package->schild(1) or croak "package name not found";
77 5 50       97 $namespace->isa('PPI::Token::Word') or croak "package name not found";
78 5         12 $namespace->{content} = $replacement;
79              
80 5         33 return PPIx::EditorTools::ReturnObject->new(
81             ppi => $doc,
82             element => $package
83             );
84             }
85              
86             1;
87              
88             __END__