blib/lib/Devel/Isa/Explainer.pm

filter: Statementstmt 83.1% 163 / 196 Branchbran 63.7% 37 / 58 Conditioncond 87.5% 14 / 16 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 89.2% 25 / 28 Podpod 100% 1 / 1 totaltotal 80.1% 247 / 308 CC 57 SCAR 21.1
CC
57
Cov
79%
__ANON__
20.0
_mg_type_shadow_clustered
20.0
_extract_mro
15.0
CRAP 8.2
« lib/App/Isa/Splain.pm ↑ Summary
1 8 ▶use 5.006; # our
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Devel::Isa::Explainer;
6  
7 8 our $VERSION = '0.002001';
8  
9 # ABSTRACT: Pretty Print Hierarchies of Subs in Packages
10  
11 8 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 8 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Term::ANSIColor 3.00 ('colored'); # bright_
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Carp ('croak');
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Package::Stash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use MRO::Compat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Perl critic is broken. This is not a void context.
20 ## no critic (BuiltinFunctions::ProhibitVoidMap)
21 8 ▶use constant 1.03 ( { map { ( ( sprintf '_E%x', $_ ), ( sprintf ' (id: %s#%d)', __PACKAGE__, $_ ), ) } 1 .. 5 } );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 8 ▶BEGIN { *import = \&Exporter::import } ## no critic (ProhibitCallsToUnexportedSubs)
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 our @EXPORT_OK = qw( explain_isa );
28  
29 # These exist for twiddling, but are presently undocumented as their interface
30 # is not deemed even remotely stable. Use at own risk.
31  
32 8 our @TYPE_UTIL = qw( cyan );
33 8 our @TYPE = qw( yellow );
34 8 our @PRIVATE = qw( reset );
35 8 our @PUBLIC = qw( bold bright_green );
36 8 our @SHADOWED_PRIVATE = qw( magenta );
37 8 our @SHADOWED_PUBLIC = qw( red );
38  
39 8 our $MAX_WIDTH = 80;
40 8 our $SHOW_SHADOWED = 1;
41 8 our $INDENT = q[ ] x 4;
42 8 our $SHADOW_SUFFIX = q{(^)};
43 8 our $SHADOWED_SUFFIX = q{}; # TBD
44 8 our $CLUSTERING = 'type_clustered';
45  
46  
47  
48  
49  
50  
51  
52  
53  
54 6 ▶sub explain_isa {
Subroutine explain_isa: called CC=5 SCAR=24.5
Subroutine __ANON__: not called CC=5 SCAR=24.5
Pod documented
55 0 my $nargs = scalar( my ( $target, ) = @_ );
56 0 ▶ 1 == $nargs or croak "Passed $nargs arguments, Expected 1" . _E1;
Branch: 2 branches
BranchTrueFalseTotal
1 == $nargs or croak("Passed $nargs arguments, Expected 1" . ' (id: Devel::Isa::Explainer#1)') 6 0 6
1 == $nargs or croak("Passed $nargs arguments, Expected 1" . ' (id: Devel::Isa::Explainer#1)') 0 0 0
57 0 ▶ defined $target or croak 'Expected defined target' . _E2;
Branch: 2 branches
BranchTrueFalseTotal
defined $target or croak("Expected defined target (id: Devel::Isa::Explainer#2)") 6 0 6
defined $target or croak("Expected defined target (id: Devel::Isa::Explainer#2)") 0 0 0
58 0 ▶ length $target or croak 'Expected target with non-zero length' . _E3;
Branch: 2 branches
BranchTrueFalseTotal
length $target or croak("Expected target with non-zero length (id: Devel::Isa::Explainer#3)") 6 0 6
length $target or croak("Expected target with non-zero length (id: Devel::Isa::Explainer#3)") 0 0 0
59 0 ▶ ref $target and croak 'Expected scalar target' . _E4;
Branch: 2 branches
BranchTrueFalseTotal
ref $target and croak("Expected scalar target (id: Devel::Isa::Explainer#4)") 0 6 6
ref $target and croak("Expected scalar target (id: Devel::Isa::Explainer#4)") 0 0 0
60 0 return _pp_key() . _pp_class($target);
61 }
62  
63 # -- no user servicable parts --
64 318 ▶sub _sub_type {
Subroutine _sub_type: called CC=5 SCAR=16.2
65 318 my ($sub) = @_;
66 318 ▶ return 'PRIVATE' if $sub =~ /\A_/sx;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /\A_/sx 94 224 318
67 224 ▶ return 'TYPE_UTIL' if $sub =~ /\A(is_|assert_|to_)[[:upper:]]/sx;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /\A(is_|assert_|to_)[[:upper:]]/sx 0 224 224
68 224 ▶ return 'PRIVATE' if $sub =~ /\A[[:upper:]][[:upper:]]/sx;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /\A[[:upper:]][[:upper:]]/sx 8 216 224
69 216 ▶ return 'TYPE' if $sub =~ /\A[[:upper:]]/sx;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /\A[[:upper:]]/sx 0 216 216
70 216 return 'PUBLIC';
71 }
72  
73 6 ▶sub _hl_TYPE_UTIL {
Subroutine _hl_TYPE_UTIL: called CC=2 SCAR=8.1
74 6 ▶ if ( $_[0] =~ /\A([^_]+_)(.*\z)/sx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\A([^_]+_)(.*\z)/sx) 6 0 6
75 6 return colored( \@TYPE_UTIL, $1 ) . colored( \@TYPE, $2 );
76 }
77 0 return $_[0];
78 }
79  
80 207 ▶sub _hl_suffix {
Subroutine _hl_suffix: called CC=3 SCAR=11.0
81 207 ▶ return colored( $_[0], $SHADOW_SUFFIX ) if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 29 178 207
82 178 ▶ return colored( $_[0], $SHADOWED_SUFFIX ) if $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 16 162 178
83 162 return q[];
84 }
85  
86 6 ▶sub _hl_TYPE { return colored( \@TYPE, $_[0] ) }
Subroutine _hl_TYPE: called CC=1 SCAR=0.0
87  
88 132 ▶sub _hl_PUBLIC {
Subroutine _hl_PUBLIC: called CC=2 SCAR=6.9
89 132 ▶ return ( $_[1] ? colored( \@SHADOWED_PUBLIC, $_[0] ) : colored( \@PUBLIC, $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 17 115 132
90 . _hl_suffix( \@SHADOWED_PUBLIC, $_[1], $_[2] );
91 }
92  
93 75 ▶sub _hl_PRIVATE {
Subroutine _hl_PRIVATE: called CC=2 SCAR=6.9
94 75 ▶ return ( $_[1] ? colored( \@SHADOWED_PRIVATE, $_[0] ) : colored( \@PRIVATE, $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 12 63 75
95 . _hl_suffix( \@SHADOWED_PRIVATE, $_[1], $_[2] );
96 }
97  
98 159 ▶sub _pp_sub {
Subroutine _pp_sub: called CC=1 SCAR=0.0
99 159 return __PACKAGE__->can( '_hl_' . _sub_type( $_[0] ) )->(@_);
100 }
101  
102 6 ▶sub _pp_key {
Subroutine _pp_key: called CC=2 SCAR=6.9
103 6 my @tokens;
104 6 push @tokens, 'Public Sub: ' . _hl_PUBLIC('foo_example');
105 6 push @tokens, 'Type Constraint: ' . _hl_TYPE('TypeName');
106 6 push @tokens, 'Type Constraint Utility: ' . _hl_TYPE_UTIL('typeop_TypeName');
107 6 push @tokens, 'Private/Boring Sub: ' . _hl_PRIVATE('foo_example');
108 6 ▶ if ($SHOW_SHADOWED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::Isa::Explainer::SHOW_SHADOWED) 6 0 6
109 6 push @tokens, 'Public Sub shadowing another: ' . _hl_PUBLIC( 'shadowing_example', 0, 1 );
110 6 push @tokens, 'Public Sub shadowed by higher scope: ' . _hl_PUBLIC( 'shadowed_example', 1 );
111 6 push @tokens, 'Public Sub shadowing another and shadowed itself: ' . _hl_PUBLIC( 'shadowed_shadowing_example', 1, 1 );
112  
113 6 push @tokens, 'Private/Boring Sub shadowing another: ' . _hl_PRIVATE( 'shadowing_example', 0, 1 );
114 6 push @tokens, 'Private/Boring Sub shadowed by higher scope: ' . _hl_PRIVATE( 'shadowed_example', 1 );
115 6 push @tokens, 'Private/Boring Sub another and shadowed itself: ' . _hl_PRIVATE( 'shadowing_shadowed_example', 1, 1 );
116 }
117 6 push @tokens, 'No Subs: ()';
118 6 return sprintf "Key:\n$INDENT%s\n\n", join qq[\n$INDENT], @tokens;
119 }
120  
121 12 ▶sub _mg_sorted {
Subroutine _mg_sorted: called CC=2 SCAR=8.5
122 12 my (%subs) = @_;
123 12 ▶ if ($SHOW_SHADOWED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::Isa::Explainer::SHOW_SHADOWED) 12 0 12
124 12 return ( [ sort { lc $a cmp lc $b } keys %subs ] );
125 }
126 0 return ( [ grep { !$subs{$_} } sort { lc $a cmp lc $b } keys %subs ] );
127 }
128  
129 0 ▶sub _mg_type_shadow_clustered {
Subroutine _mg_type_shadow_clustered: not called CC=4 SCAR=30.0
130 0 my (%subs) = @_;
131 0 my %clusters;
132 0 for my $sub ( keys %subs ) {
133 0 my $shadow = '.shadowed' x !!$subs{$sub};
134 0 $clusters{ _sub_type($sub) . $shadow }{$sub} = $subs{$sub};
135 }
136 0 my @out;
137 0 for my $type ( map { ( $_, "$_.shadowed" ) } qw( PUBLIC PRIVATE TYPE TYPE_UTIL ) ) {
138 0 ▶ next unless exists $clusters{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $clusters{$type} 0 0 0
139 0 push @out, _mg_sorted( %{ $clusters{$type} } );
140 }
141 0 return @out;
142 }
143  
144 8 ▶sub _mg_type_clustered {
Subroutine _mg_type_clustered: called CC=4 SCAR=13.9
145 8 my (%subs) = @_;
146 8 my %clusters;
147 8 for my $sub ( keys %subs ) {
148 159 $clusters{ _sub_type($sub) }{$sub} = $subs{$sub};
149 }
150 8 my @out;
151 8 for my $type (qw( PUBLIC PRIVATE TYPE TYPE_UTIL )) {
152 32 ▶ next unless exists $clusters{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $clusters{$type} 20 12 32
153 12 push @out, _mg_sorted( %{ $clusters{$type} } );
154 }
155 8 return @out;
156 }
157  
158 0 ▶sub _mg_aleph {
Subroutine _mg_aleph: not called CC=3 SCAR=24.8
159 0 my (%subs) = @_;
160 0 my %clusters;
161 0 for my $sub ( keys %subs ) {
162 0 $clusters{ lc( substr $sub, 0, 1 ) }{$sub} = $subs{$sub};
163 }
164 0 my @out;
165 0 for my $key ( sort keys %clusters ) {
166 0 push @out, _mg_sorted( %{ $clusters{$key} } );
167 }
168 0 return @out;
169  
170 }
171  
172 8 ▶sub _pp_subs {
Subroutine _pp_subs: called CC=5 SCAR=16.1
173 8 my (%subs) = @_;
174 8 my (@clusters) = __PACKAGE__->can( '_mg_' . $CLUSTERING )->(%subs);
175 8 my (@out_clusters);
176 8 for my $cluster (@clusters) {
177 12 my $cluster_out = q[];
178  
179 12 my @subs = @{$cluster};
180 12 ▶ while (@subs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subs) 41 12 53
181 41 my $line = $INDENT;
182 flowcontrol: {
183 41 my $sub = shift @subs;
184 159 $line .= $sub . q[, ];
185 159 ▶ redo flowcontrol if @subs and length $line < $MAX_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
if @subs and length $line < $Devel::Isa::Explainer::MAX_WIDTH 118 41 159
Condition100%3/3
@subs and length $line < $Devel::Isa::Explainer::MAX_WIDTH
!ll&&!rl&&r
1229118
MC/DC100%2/2
@subs and length $line < $Devel::Isa::Explainer::MAX_WIDTH
@subs length $line < $Devel::Isa::Explainer::MAX_WIDTH
Truth table100%3/3
A and B
A= @subsB= length $line < $Devel::Isa::Explainer::MAX_WIDTH
ABresult
0X0
100
111
186 }
187 41 $cluster_out .= "$line\n";
188 }
189  
190 # Suck up trailing ,
191 12 $cluster_out =~ s/,[ ]\n\z/\n/sx;
192 12 $cluster_out =~ s{(\w+)}{ _pp_sub($1, $subs{$1}->{shadowed}, $subs{$1}->{shadowing} ) }gsex;
193 12 push @out_clusters, $cluster_out;
194 }
195 8 return join qq[\n], @out_clusters;
196 }
197  
198 6 ▶sub _pp_class {
Subroutine _pp_class: called CC=4 SCAR=14.2
199 6 my ($class) = @_;
200 6 my $out = q[];
201 6 my $mro_order = _extract_mro($class);
202 4 for my $mro_entry ( @{$mro_order} ) {
203 8 $out .= colored( ['green'], $mro_entry->{class} ) . q[:];
204 8 ▶ my (%subs) = %{ $mro_entry->{subs} || {} };
Condition50%1/2
$mro_entry->{'subs'} || {}
l!l
80
MC/DC0%0/1
$mro_entry->{'subs'} || {}
$mro_entry->{'subs'}
Truth table50%1/2
A
A= $mro_entry->{'subs'}
Aresult
11
00
205 8 ▶ if ( not keys %subs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %subs) { } 0 8 8
206 0 $out .= " ()\n";
207 0 next;
208 }
209 8 else { $out .= "\n" }
210 8 $out .= _pp_subs(%subs) . "\n";
211  
212 8 next;
213 }
214 4 return $out;
215 }
216  
217 9 ▶sub _extract_mro {
Subroutine _extract_mro: called CC=15 SCAR=27.1
218 9 my ($class) = @_;
219 9 my ($seen_subs) = {};
220  
221 ## no critic (ProhibitCallsToUnexportedSubs)
222 9 my (@isa) = @{ mro::get_linear_isa($class) };
223  
224 # Discover all subs and compute full MRO every time a new sub-name
225 # is found
226 9 for my $isa (@isa) {
227 19 for my $sub ( Package::Stash->new($isa)->list_all_symbols('CODE') ) {
228 171 ▶ next if exists $seen_subs->{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen_subs->{$sub} 11 160 171
229  
230 # Compute the full sub->package MRO table bottom up
231  
232 160 $seen_subs->{$sub} = [];
233 160 my $currently_visible;
234 160 for my $class ( reverse @isa ) {
235 278 ▶ my $coderef = $class->can($sub) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $coderef = $class->can($sub) or next 197 81 278
236  
237 # Record the frame where the first new instance is seen.
238 197 ▶ if ( not defined $currently_visible or $currently_visible != $coderef ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $currently_visible or $currently_visible != $coderef) 170 27 197
Condition100%3/3
not defined $currently_visible or $currently_visible != $coderef
l!l&&r!l&&!r
1601027
MC/DC100%2/2
not defined $currently_visible or $currently_visible != $coderef
not defined $currently_visible $currently_visible != $coderef
Truth table100%3/3
not(A) or B
A= not defined $currently_visibleB= $currently_visible != $coderef
ABresult
1X1
011
000
239 170 unshift @{ $seen_subs->{$sub} }, $class; # note: we're working bottom-up
240 170 $currently_visible = $coderef;
241 170 next;
242 }
243 }
244 }
245 }
246  
247 8 my $class_data = {};
248  
249 # Group "seen subs" into class oriented structures,
250 # and classify them.
251 8 for my $sub ( keys %{$seen_subs} ) {
252 160 my @classes = @{ $seen_subs->{$sub} };
253  
254 160 for my $isa ( @{ $seen_subs->{$sub} } ) {
255  
256 # mark all subs both shadowing and shadowed until proven otherwise
257 170 $class_data->{$isa}->{$sub} = { shadowed => 1, shadowing => 1 };
258 }
259  
260 # mark top-most sub unshadowed
261 160 $class_data->{ $classes[0] }->{$sub}->{shadowed} = 0;
262  
263 # mark bottom-most sub unshadowing
264 160 $class_data->{ $classes[-1] }->{$sub}->{shadowing} = 0;
265  
266 }
267  
268 # Order class structures by MRO order
269 8 ▶ my (@mro_order) = map { { class => $_, subs => $class_data->{$_} || {} } } @isa;
Condition100%2/2
$class_data->{$_} || {}
l!l
153
MC/DC100%1/1
$class_data->{$_} || {}
$class_data->{$_}
Truth table100%2/2
A
A= $class_data->{$_}
Aresult
11
00
270  
271 8 ▶ if ( 1 > @mro_order or ( 1 >= @mro_order and 1 > keys %{ $mro_order[0]->{subs} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 > @mro_order or 1 >= @mro_order and 1 > keys %{$mro_order[0]{"subs"};}) 1 7 8
Condition83%5/6
1 > @mro_order or 1 >= @mro_order and 1 > keys %{$mro_order[0]{"subs"};}
l!l&&r!l&&!r
017
1 >= @mro_order and 1 > keys %{$mro_order[0]{"subs"};}
!ll&&!rl&&r
521
MC/DC66%2/3
1 > @mro_order or 1 >= @mro_order and 1 > keys %{$mro_order[0]{"subs"};}
1 > @mro_order 1 >= @mro_order 1 > keys %{$mro_order[0]{"subs"};}
Truth table75%3/4
A or B and C
A= 1 > @mro_orderB= 1 >= @mro_orderC= 1 > keys %{$mro_order[0]{"subs"};}
ABCresult
1XX1
0111
00X0
0100
272  
273 # Huh, No inheritance, and no subs. K.
274 1 my $module_path = $class;
275 1 $module_path =~ s{ (::|') }{/}sgx;
276  
277 # TODO: Maybe don't make this fatal and return data context to user instead?
278 # Undecided, and will have to come after more complex concerns.
279 # - kentnl, Apr 2016
280 1 ▶ if ( not $INC{ $module_path . '.pm' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$module_path . '.pm'}) 1 0 1
281 1 croak "No module called $class successfully loaded" . _E5;
282 }
283 }
284 7 return \@mro_order;
285 }
286  
287 8 1;
288  
289 __END__
« lib/App/Isa/Splain.pm ↑ Summary