blib/lib/PPIx/EditorTools/ReturnObject.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran 80.0% 8 / 10 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 81.1% 56 / 69 SCAR 22.4
CC
9
Cov
83%
element
4.2
code
3.1
ppi
3.1
CRAP 9.4
« blib/lib/PPIx/EditorTools/IntroduceTemporaryVariable.pm ↑ Summary blib/lib/PPIx/EditorTools/RenamePackageFromPath.pm »
1 package PPIx::EditorTools::ReturnObject;
2 25 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Simple object to return values from PPIx::EditorTools
4 25 $PPIx::EditorTools::ReturnObject::VERSION = '0.20';
5 25 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 sub new {
12 26 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=8.1
Pod documented
13 26 ▶ return bless {@_}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0260
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
14 }
15  
16 sub element {
17 14 ▶ my ($self) = @_;
Subroutine element: called CC=4 SCAR=14.5
Pod documented
18  
19 # If element is a code ref, run the code once then cache the
20 # result
21 14 ▶ if ( exists $self->{element}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"element"} and ref $self->{"element"} and ref $self->{"element"} eq "CODE") 2 12 14
Condition50%3/6
exists $self->{"element"} and ref $self->{"element"} and ref $self->{"element"} eq "CODE"
!ll&&!rl&&r
0122
exists $self->{"element"} and ref $self->{"element"}
!ll&&!rl&&r
0014
MC/DC33%1/3
exists $self->{"element"} and ref $self->{"element"} and ref $self->{"element"} eq "CODE"
exists $self->{"element"} ref $self->{"element"} ref $self->{"element"} eq "CODE"
Truth table50%2/4
A and B and C
A= exists $self->{"element"}B= ref $self->{"element"}C= ref $self->{"element"} eq "CODE"
ABCresult
0XX0
10X0
1100
1111
22 and ref( $self->{element} )
23 and ref( $self->{element} ) eq 'CODE' )
24 {
25 2 $self->{element} = $self->{element}->(@_);
26 }
27  
28 14 return $self->{element};
29 }
30  
31 sub ppi {
32 3 ▶ my ( $self ) = @_;
Subroutine ppi: called CC=3 SCAR=11.2
Pod documented
33  
34 # $self->{ppi} = $doc if $doc; # TODO: and isa?
35  
36 3 ▶ return $self->{ppi} if $self->{ppi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ppi"} 1 2 3
37  
38 2 ▶ if ( $self->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"code"}) 2 0 2
39 2 my $code = $self->{code};
40 2 $self->{ppi} = PPI::Document->new( \$code );
41 2 return $self->{ppi};
42 }
43  
44 0 return;
45 }
46  
47 sub code {
48 20 ▶ my ( $self ) = @_;
Subroutine code: called CC=3 SCAR=11.2
Pod documented
49  
50 # $self->{code} = $doc if $doc;
51  
52 20 ▶ return $self->{code} if $self->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"code"} 4 16 20
53  
54 16 ▶ if ( $self->{ppi} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ppi"}) 16 0 16
55 16 $self->{code} = $self->{ppi}->serialize;
56 16 return $self->{code};
57 }
58  
59 0 return;
60 }
61  
62 25 1;
63  
64 =pod
65  
66 =encoding UTF-8
67  
68 =head1 NAME
69  
70 PPIx::EditorTools::ReturnObject - Simple object to return values from PPIx::EditorTools
71  
72 =head1 VERSION
73  
74 version 0.20
75  
76 =head1 SYNOPSIS
77  
78 my $brace = PPIx::EditorTools::FindUnmatchedBrace->new->find(
79 code => "package TestPackage;\nsub x { 1;\n"
80 );
81 my $location = $brace->element->location;
82 my $ppi = $brace->element->ppi;
83  
84 =head1 DESCRIPTION
85  
86 Retuning a simple C<PPI::Element> from many of the C<PPIx::EditorTools>
87 methods often results in the loss of the overall context for that element.
88 C<PPIx::EditorTools::ReturnObject> provides an object that can be passed
89 around which retains the overall context.
90  
91 For example, in C<PPIx::EditorTools::FindUnmatchedBrace> if the unmatched
92 brace were returned by its C<PPI::Structure::Block> the containing
93 C<PPI::Document> is likely to go out of scope, thus the C<location>
94 method no longer returns a valid location (rather it returns undef).
95 Using the C<ReturnObject> preserves the C<PPI::Document> and the containing
96 context.
97  
98 =head1 METHODS
99  
100 =over 4
101  
102 =item new()
103  
104 Constructor which should be used by C<PPIx::EditorTools>. Accepts the following
105 named parameters:
106  
107 =over 4
108  
109 =item ppi
110  
111 A C<PPI::Document> representing the (possibly modified) code.
112  
113 =item code
114  
115 A string representing the (possibly modified) code.
116  
117 =item element
118  
119 A C<PPI::Element> or a subclass thereof representing the interesting element.
120  
121 =back
122  
123 =item ppi
124  
125 Accessor to retrieve the C<PPI::Document>. May create the C<PPI::Document>
126 from the $code string (lazily) if needed.
127  
128 =item code
129  
130 Accessor to retrieve the string representation of the code. May be retrieved
131 from the C<PPI::Document> via the serialize method (lazily) if needed.
132  
133 =back
134  
135 =head1 SEE ALSO
136  
137 C<PPIx::EditorTools>, L<App::EditorTools>, L<Padre>, and L<PPI>.
138  
139 =head1 AUTHORS
140  
141 =over 4
142  
143 =item *
144  
145 Steffen Mueller C<smueller@cpan.org>
146  
147 =item *
148  
149 Mark Grimes C<mgrimes@cpan.org>
150  
151 =item *
152  
153 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
154  
155 =item *
156  
157 Gabor Szabo <gabor@szabgab.com>
158  
159 =item *
160  
161 Yanick Champoux <yanick@cpan.org>
162  
163 =back
164  
165 =head1 COPYRIGHT AND LICENSE
166  
167 This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
168  
169 This is free software; you can redistribute it and/or modify it under
170 the same terms as the Perl 5 programming language system itself.
171  
172 =cut
173  
174 __END__
175  
176  
177 # Copyright 2008-2009 The Padre development team as listed in Padre.pm.
178 # LICENSE
179 # This program is free software; you can redistribute it and/or
180 # modify it under the same terms as Perl 5 itself.
« blib/lib/PPIx/EditorTools/IntroduceTemporaryVariable.pm ↑ Summary blib/lib/PPIx/EditorTools/RenamePackageFromPath.pm »