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             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Change the package name
4             $PPIx::EditorTools::RenamePackage::VERSION = '0.20';
5 3     3   163924 use strict;
  3         11  
  3         82  
6              
7             BEGIN {
8 3     3   52 $^W = 1;
9             }
10 3     3   13 use base 'PPIx::EditorTools';
  3         10  
  3         486  
11              
12 3     3   20 use Class::XSAccessor accessors => { 'replacement' => 'replacement' };
  3         6  
  3         16  
13              
14 3     3   491 use PPI;
  3         6  
  3         46  
15 3     3   11 use Carp;
  3         5  
  3         416  
16              
17              
18             sub rename {
19 6     6 1 1755 my ( $self, %args ) = @_;
20 6         27 $self->process_doc(%args);
21 6   33     21 my $replacement = $args{replacement} || croak "replacement required";
22              
23 6         16 my $doc = $self->ppi;
24              
25             # TODO: support MooseX::Declare
26 6 100       21 my $package = $doc->find_first('PPI::Statement::Package')
27             or die "no package found";
28 5 50       935 my $namespace = $package->schild(1) or croak "package name not found";
29 5 50       85 $namespace->isa('PPI::Token::Word') or croak "package name not found";
30 5         11 $namespace->{content} = $replacement;
31              
32 5         21 return PPIx::EditorTools::ReturnObject->new(
33             ppi => $doc,
34             element => $package
35             );
36             }
37              
38             1;
39              
40             __END__
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =head1 NAME
47              
48             PPIx::EditorTools::RenamePackage - Change the package name
49              
50             =head1 VERSION
51              
52             version 0.20
53              
54             =head1 SYNOPSIS
55              
56             my $munged = PPIx::EditorTools::RenamePackage->new->rename(
57             code => <<'END_CODE',
58             package TestPackage;
59             use strict;
60              
61             BEGIN { $^W = 1; }
62             1;
63             END_CODE
64             replacement => 'NewPackage'
65             );
66              
67             my $new_code_as_string = $munged->code;
68             my $package_ppi_element = $munged->element;
69              
70             =head1 DESCRIPTION
71              
72             This module uses PPI to change the package name of code.
73              
74             =head1 METHODS
75              
76             =over 4
77              
78             =item new()
79              
80             Constructor. Generally shouldn't be called with any arguments.
81              
82             =item rename( ppi => PPI::Document $ppi, replacement => Str )
83              
84             =item rename( code => Str $code, replacement => Str )
85              
86             Accepts either a C<PPI::Document> to process or a string containing
87             the code (which will be converted into a C<PPI::Document>) to process.
88             Replaces the package name with that supplied in the C<replacement>
89             parameter and returns a C<PPIx::EditorTools::ReturnObject> with the
90             new code available via the C<ppi> or C<code> accessors, as a
91             C<PPI::Document> or C<string>, respectively.
92              
93             Croaks with a "package name not found" exception if unable to find the
94             package name.
95              
96             =back
97              
98             =head1 SEE ALSO
99              
100             This class inherits from C<PPIx::EditorTools>.
101             Also see L<App::EditorTools>, L<Padre>, and L<PPI>.
102              
103             =head1 AUTHORS
104              
105             =over 4
106              
107             =item *
108              
109             Steffen Mueller C<smueller@cpan.org>
110              
111             =item *
112              
113             Mark Grimes C<mgrimes@cpan.org>
114              
115             =item *
116              
117             Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
118              
119             =item *
120              
121             Gabor Szabo <gabor@szabgab.com>
122              
123             =item *
124              
125             Yanick Champoux <yanick@cpan.org>
126              
127             =back
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
132              
133             This is free software; you can redistribute it and/or modify it under
134             the same terms as the Perl 5 programming language system itself.
135              
136             =cut