blib/lib/PPIx/EditorTools/RenamePackage.pm

filter: Statementstmt 100% 28 / 28 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 87.2% 41 / 47 SCAR 16.2
CC
5
Cov
89%
rename
5.3
BEGIN
1.0
BEGIN
1.0
CRAP 5.0
« blib/lib/PPIx/EditorTools/RenamePackageFromPath.pm ↑ Summary blib/lib/PPIx/EditorTools/FindVariableDeclaration.pm »
1 package PPIx::EditorTools::RenamePackage;
2 5 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Change the package name
4 5 $PPIx::EditorTools::RenamePackage::VERSION = '0.20';
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN {
8 5 ▶ $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 }
10 5 ▶use base 'PPIx::EditorTools';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Class::XSAccessor accessors => { 'replacement' => 'replacement' };
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 sub rename {
19 6 ▶ my ( $self, %args ) = @_;
Subroutine rename: called CC=5 SCAR=16.6
Pod documented
20 6 $self->process_doc(%args);
21 6 ▶ my $replacement = $args{replacement} || croak "replacement required";
Condition33%1/3
$args{'replacement'} || croak('replacement required')
l!l&&r!l&&!r
600
MC/DC0%0/2
$args{'replacement'} || croak('replacement required')
$args{'replacement'} croak('replacement required')
Truth table33%1/3
A || B
A= $args{'replacement'}B= croak('replacement required')
ABresult
1X1
011
000
22  
23 6 my $doc = $self->ppi;
24  
25 # TODO: support MooseX::Declare
26 6 ▶ my $package = $doc->find_first('PPI::Statement::Package')
Branch: 1 branch
BranchTrueFalseTotal
my $package = $doc->find_first("PPI::Statement::Package") or die "no package found" 1 5 6
27 or die "no package found";
28 5 ▶ my $namespace = $package->schild(1) or croak "package name not found";
Branch: 1 branch
BranchTrueFalseTotal
my $namespace = $package->schild(1) or croak("package name not found") 0 5 5
29 5 ▶ $namespace->isa('PPI::Token::Word') or croak "package name not found";
Branch: 1 branch
BranchTrueFalseTotal
$namespace->isa("PPI::Token::Word") or croak("package name not found") 0 5 5
30 5 $namespace->{content} = $replacement;
31  
32 5 return PPIx::EditorTools::ReturnObject->new(
33 ppi => $doc,
34 element => $package
35 );
36 }
37  
38 5 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
« blib/lib/PPIx/EditorTools/RenamePackageFromPath.pm ↑ Summary blib/lib/PPIx/EditorTools/FindVariableDeclaration.pm »