lib/Meta/Grapher/Moose.pm

filter: Statementstmt 100% 150 / 150 Branchbran 83.3% 40 / 48 Conditioncond 55.5% 15 / 27 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 23 / 23 Podpod 100% 1 / 1 totaltotal 88.3% 235 / 266 CC 39 SCAR 19.2
CC
39
Cov
91%
_get_methods_and_attributes
16.1
_process_package
7.0
_maybe_add_edge_to_graph
6.1
CRAP 6.8
↑ Summary lib/Meta/Grapher/Moose/Renderer/Plantuml.pm »
1 package Meta::Grapher::Moose;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.03';
8  
9 1 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Meta::Grapher::Moose::Constants qw( CLASS ROLE P_ROLE ANON_ROLE );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has package => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1,
20 );
21  
22 1 has show_meta => (
23 is => 'ro',
24 isa => 'Bool',
25 );
26  
27 1 has show_new => (
28 is => 'ro',
29 isa => 'Bool',
30 );
31  
32 1 has show_destroy => (
33 is => 'ro',
34 isa => 'Bool',
35 );
36  
37 1 has show_moose_object => (
38 is => 'ro',
39 isa => 'Bool',
40 );
41  
42 1 has _renderer => (
43 is => 'ro',
44 init_arg => 'renderer',
45 does => 'Meta::Grapher::Moose::Role::Renderer',
46 required => 1,
47 );
48  
49 # these are an internal record of what we asked our renderer to render. It's
50 # used for de-duplication purposes (to avoid asking the renderer to render
51 # the same thing twice), but is inaccessible to the renderer that is responsible
52 # for keeping it's own state.
53  
54 has _nodes => (
55 traits => ['Hash'],
56 is => 'ro',
57 isa => 'HashRef[Bool]',
58 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 handles => {
60 _set_node => 'set',
61 _already_seen_node => 'get',
62 },
63 );
64  
65 16 ▶sub _seen_node {
Subroutine _seen_node: called CC=1 SCAR=0.0
66 16 my $self = shift;
67 16 my $node = shift;
68 16 $self->_set_node( $node => 1 );
69 }
70  
71 has _edges => (
72 traits => ['Hash'],
73 is => 'ro',
74 isa => 'HashRef[Bool]',
75 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 handles => {
77 _set_edge => 'set',
78 _already_seen_edge => 'get',
79 },
80 );
81  
82 18 ▶sub _seen_edge {
Subroutine _seen_edge: called CC=1 SCAR=0.0
83 18 my $self = shift;
84 18 my $edge = shift;
85 18 $self->_set_edge( $edge => 1 );
86 }
87  
88 1 with 'MooseX::Getopt::Dashes';
89  
90 1 ▶sub run {
Subroutine run: called CC=2 SCAR=7.0
Pod documented
91 1 my $self = shift;
92  
93 1 my $package = $self->package;
94  
95 # This just produces a better error message than Module::Runtime or any
96 # other runtime loader.
97 #
98 ## no critic (BuiltinFunctions::ProhibitStringyEval)
99 1 ▶ eval "require $package; 1;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $package; 1;" or die $@ 1 0 1
100 or die $@;
101 ## use critic
102  
103 1 $self->_process_package( $package, 2048 );
104 1 $self->_renderer->render;
105  
106 1 return 0;
107 }
108  
109 21 ▶sub _get_methods_and_attributes {
Subroutine _get_methods_and_attributes: called CC=16 SCAR=27.8
110 21 my $self = shift;
111 21 my $meta = shift;
112  
113 # HERE YOU ARE ETHAN
114 # - turn into a for loop, add filtering for meta
115 21 my @methods;
116 21 for my $method_name ( $meta->get_method_list ) {
117 92 ▶ next if $method_name eq 'meta' && !$self->show_meta;
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "meta" and not $self->show_meta 21 71 92
Condition66%2/3
$method_name eq "meta" and not $self->show_meta
!ll&&!rl&&r
71021
MC/DC50%1/2
$method_name eq "meta" and not $self->show_meta
$method_name eq "meta" not $self->show_meta
Truth table66%2/3
A and not(B)
A= $method_name eq "meta"B= not $self->show_meta
ABresult
0X0
100
111
118 71 ▶ next if $method_name eq 'new' && !$self->show_new;
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" and not $self->show_new 4 67 71
Condition66%2/3
$method_name eq "new" and not $self->show_new
!ll&&!rl&&r
6704
MC/DC50%1/2
$method_name eq "new" and not $self->show_new
$method_name eq "new" not $self->show_new
Truth table66%2/3
A and not(B)
A= $method_name eq "new"B= not $self->show_new
ABresult
0X0
100
111
119 67 ▶ next if $method_name eq 'DESTROY' && !$self->show_destroy;
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "DESTROY" and not $self->show_destroy 4 63 67
Condition66%2/3
$method_name eq "DESTROY" and not $self->show_destroy
!ll&&!rl&&r
6304
MC/DC50%1/2
$method_name eq "DESTROY" and not $self->show_destroy
$method_name eq "DESTROY" not $self->show_destroy
Truth table66%2/3
A and not(B)
A= $method_name eq "DESTROY"B= not $self->show_destroy
ABresult
0X0
100
111
120  
121 63 my $method = $meta->get_method($method_name);
122  
123 # ignore methods that weren't created in this class (i.e. they
124 # came from a role)
125 63 ▶ next if $method->original_package_name ne $meta->name;
Branch: 1 branch
BranchTrueFalseTotal
if $method->original_package_name ne $meta->name 22 41 63
126  
127 # ignore things that are just readers and writers since they're
128 # already listed as attributes (we probably want to add some
129 # configuration on this later)
130 next
131 41 ▶ if $method->isa('Class::MOP::Method::Accessor')
Branch: 1 branch
BranchTrueFalseTotal
if $method->isa("Class::MOP::Method::Accessor") and $method->accessor_type =~ /^[rw]/ 18 23 41
Condition66%2/3
$method->isa("Class::MOP::Method::Accessor") and $method->accessor_type =~ /^[rw]/
!ll&&!rl&&r
23018
MC/DC50%1/2
$method->isa("Class::MOP::Method::Accessor") and $method->accessor_type =~ /^[rw]/
$method->isa("Class::MOP::Method::Accessor") $method->accessor_type =~ /^[rw]/
Truth table66%2/3
A and B
A= $method->isa("Class::MOP::Method::Accessor")B= $method->accessor_type =~ /^[rw]/
ABresult
0X0
100
111
132 && $method->accessor_type =~ /^[rw]/;
133  
134 23 push @methods, $method_name;
135 }
136  
137 21 my @attributes;
138 21 ATTRIBUTE: for my $attr_name ( $meta->get_attribute_list ) {
139 38 my $attribute = $meta->get_attribute($attr_name);
140  
141 # roles know where they get their attributes from
142 38 ▶ if ( $attribute->can('original_role') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->can("original_role")) 20 18 38
143 20 ▶ if ( $attribute->original_role->name eq $meta->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->original_role->name eq $meta->name) 17 3 20
144 17 push @attributes, $attr_name;
145 }
146 20 next ATTRIBUTE;
147 }
148  
149 # otherwise we need to check each of our roles to see if they
150 # have the accessor of the same neme
151 18 for my $role ( $self->_roles_from($meta) ) {
152 18 ▶ if ( $role->get_attribute($attr_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role->get_attribute($attr_name)) 16 2 18
153 16 next ATTRIBUTE;
154 }
155 }
156 2 push @attributes, $attr_name;
157 }
158  
159 return (
160 21 methods => \@methods,
161 attributes => \@attributes,
162 );
163 }
164  
165 1 ▶sub _process_package {
Subroutine _process_package: called CC=6 SCAR=19.5
166 1 my $self = shift;
167 1 my $package = shift;
168 1 my $weight = shift;
169  
170 1 ▶ my $meta = try { $package->meta }
Subroutine __ANON__: called CC=1 SCAR=0.0
171 1 ▶ or die "$package does not have a ->meta method\n";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = &try(sub { $package->meta; } ) or die "$package does not have a ->meta method\n" 1 0 1
172  
173 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($meta) and $meta->isa("Moose::Meta::Class") || $meta->isa("Moose::Meta::Role") 0 1 1
Condition33%2/6
builtin::blessed($meta) and $meta->isa("Moose::Meta::Class") || $meta->isa("Moose::Meta::Role")
!ll&&!rl&&r
001
$meta->isa("Moose::Meta::Class") or $meta->isa("Moose::Meta::Role")
l!l&&r!l&&!r
100
MC/DC0%0/3
builtin::blessed($meta) and $meta->isa("Moose::Meta::Class") || $meta->isa("Moose::Meta::Role")
builtin::blessed($meta) $meta->isa("Moose::Meta::Class") $meta->isa("Moose::Meta::Role")
Truth table25%1/4
A and B or C
A= builtin::blessed($meta)B= $meta->isa("Moose::Meta::Class")C= $meta->isa("Moose::Meta::Role")
ABCresult
0XX0
1000
11X1
1011
174 "$package->meta is not a Moose::Meta::Class or a Moose::Meta::Role, it's a "
175 . ref($meta) . "\n"
176 unless blessed $meta
177 && ( $meta->isa('Moose::Meta::Class')
178 || $meta->isa('Moose::Meta::Role') );
179  
180 1 my $name = $self->_node_label_for($meta);
181 1 $self->_maybe_add_node_to_graph(
182 id => $name,
183 label => $name,
184 type => CLASS,
185 $self->_get_methods_and_attributes($meta),
186 );
187  
188 # We halve the weight each time we go up the tree. This makes the graph
189 # cleaner (straighter lines) nearest the node we start from.
190 1 ▶ $self->_follow_parents( $meta, $weight )
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Class") 1 0 1
191 if $meta->isa('Moose::Meta::Class');
192 1 $self->_follow_roles( $meta, $meta, $weight );
193  
194 1 return 0;
195 }
196  
197 4 ▶sub _follow_parents {
Subroutine _follow_parents: called CC=2 SCAR=6.9
198 4 my $self = shift;
199 4 my $meta = shift;
200 4 my $weight = shift;
201  
202 ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
203 4 my @parents = map { Class::MOP::class_of($_) } $meta->superclasses;
204 ## use critic
205  
206 4 for my $parent (@parents) {
207  
208 3 my $name = $self->_node_label_for($parent);
209 3 $self->_maybe_add_node_to_graph(
210 id => $name,
211 label => $name,
212 type => CLASS,
213 $self->_get_methods_and_attributes($parent),
214 );
215  
216 3 $self->_maybe_add_edge_to_graph(
217 from => $parent,
218 to => $meta,
219 type => CLASS,
220 weight => $weight,
221 );
222  
223 3 $self->_follow_roles( $parent, $parent, $weight );
224 3 $self->_follow_parents( $parent, $weight / 2 );
225 }
226  
227 4 return;
228 }
229  
230 24 ▶sub _follow_roles {
Subroutine _follow_roles: called CC=2 SCAR=6.9
231 24 my $self = shift;
232 24 my $to_meta = shift;
233 24 my $roles_from = shift;
234 24 my $weight = shift;
235  
236 24 for my $role ( $self->_roles_from($roles_from) ) {
237 20 $self->_record_role(
238 $to_meta,
239 $role,
240 $weight / 2
241 );
242 }
243  
244 }
245  
246 42 ▶sub _roles_from {
Subroutine _roles_from: called CC=2 SCAR=6.9
247 42 my $self = shift;
248 42 my $roles_from = shift;
249  
250 42 ▶ if ( $roles_from->isa('Moose::Meta::Class') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roles_from->isa("Moose::Meta::Class")) 22 20 42
251 22 return map { $_->role } $roles_from->role_applications;
252 }
253  
254 20 return @{ $roles_from->get_roles };
255 }
256  
257 20 ▶sub _record_role {
Subroutine _record_role: called CC=5 SCAR=16.1
258 20 my $self = shift;
259 20 my $to_meta = shift;
260 20 my $role = shift;
261 20 my $weight = shift;
262  
263 # For the purposes of this graph, Composite roles are essentially an
264 # implementation detail of Moose. We just want to see that Class A
265 # consumes Roles X, Y, & Z. The fact that this was done in a single "with"
266 # (or not) is not going to be included on the graph. We skip composite
267 # roles and simply graph the roles that they are composed of.
268 20 ▶ unless ( $role->isa('Moose::Meta::Role::Composite') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($role->isa("Moose::Meta::Role::Composite")) 17 3 20
269 17 my ( $label, $type );
270 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($role->isa('MooseX::Role::Parameterized::Meta::Role::Parameterized')) { } 4 13 17
271 $role->isa(
272 'MooseX::Role::Parameterized::Meta::Role::Parameterized')
273 ) {
274 4 $label = $self->_node_label_for( $role->genitor );
275 4 $type = ANON_ROLE;
276 }
277 else {
278 13 $label = $self->_node_label_for($role);
279 13 ▶ $type = (
Branch: 1 branch
BranchTrueFalseTotal
$role->meta->can('does_role') && $role->meta->does_role('MooseX::Role::Parameterized::Meta::Trait::Parameterizable') ? : 4 9 13
Condition66%2/3
$role->meta->can("does_role") and $role->meta->does_role("MooseX::Role::Parameterized::Meta::Trait::Parameterizable")
!ll&&!rl&&r
904
MC/DC50%1/2
$role->meta->can("does_role") and $role->meta->does_role("MooseX::Role::Parameterized::Meta::Trait::Parameterizable")
$role->meta->can("does_role") $role->meta->does_role("MooseX::Role::Parameterized::Meta::Trait::Parameterizable")
Truth table66%2/3
A and B
A= $role->meta->can("does_role")B= $role->meta->does_role("MooseX::Role::Parameterized::Meta::Trait::Parameterizable")
ABresult
0X0
100
111
280 $role->meta->can('does_role') && $role->meta->does_role(
281 'MooseX::Role::Parameterized::Meta::Trait::Parameterizable'
282 )
283 ) ? P_ROLE : ROLE;
284 }
285  
286 17 $self->_maybe_add_node_to_graph(
287 id => $self->_node_label_for($role),
288 label => $label,
289 type => $type,
290 $self->_get_methods_and_attributes($role),
291 );
292  
293 17 $self->_maybe_add_edge_to_graph(
294 from => $role,
295 to => $to_meta,
296 weight => $weight,
297 );
298  
299 17 $to_meta = $role;
300 }
301  
302 20 $self->_follow_roles( $to_meta, $role, $weight );
303  
304 20 return;
305 }
306  
307 # We need to dedeuplicate nodes - obviously more than one thing can point
308 # to any given node!
309 21 ▶sub _maybe_add_node_to_graph {
Subroutine _maybe_add_node_to_graph: called CC=4 SCAR=13.9
310 21 my $self = shift;
311 21 my %p = @_;
312  
313 21 ▶ return if $p{id} eq 'Moose::Object' && !$self->show_moose_object;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"id"} eq "Moose::Object" and not $self->show_moose_object 1 20 21
Condition66%2/3
$p{"id"} eq "Moose::Object" and not $self->show_moose_object
!ll&&!rl&&r
2001
MC/DC50%1/2
$p{"id"} eq "Moose::Object" and not $self->show_moose_object
$p{"id"} eq "Moose::Object" not $self->show_moose_object
Truth table66%2/3
A and not(B)
A= $p{"id"} eq "Moose::Object"B= not $self->show_moose_object
ABresult
0X0
100
111
314 20 ▶ return if $self->_already_seen_node( $p{id} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_already_seen_node($p{"id"}) 4 16 20
315  
316 16 $self->_renderer->add_package( map { $_ => $p{$_} }
317 qw( id methods attributes type label ) );
318  
319 16 $self->_seen_node( $p{id} );
320  
321 16 return;
322 }
323  
324 # We also need to deduplicate edges - it's possible for an edge to appear twice
325 # if something earlier in the graph consumes a role directly that it also
326 # consumes via another role indirectly. For example, if class A consumes roles B
327 # & C, but role B _also_ consumes role C. In that case, we end up visiting role
328 # C twice. That means that if C consumes some roles we'd end up seeing that
329 # relationship twice as well.
330 #
331 # The same could happen with a weird inheritance tree where a class and its
332 # parent both inherit from the same (other) parent class.
333 20 ▶sub _maybe_add_edge_to_graph {
Subroutine _maybe_add_edge_to_graph: called CC=6 SCAR=18.0
334 20 my $self = shift;
335 20 my %p = @_;
336  
337 @p{qw( from to )}
338 20 = map { $self->_node_label_for($_) } @p{qw( from to )};
339  
340 20 ▶ unless ( $self->show_moose_object ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->show_moose_object) 20 0 20
341 20 ▶ return if $p{from} eq 'Moose::Object';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"from"} eq "Moose::Object" 1 19 20
342 19 ▶ return if $p{to} eq 'Moose::Object';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"to"} eq "Moose::Object" 0 19 19
343 }
344  
345 # When a parameterized role consumes role inside its role{} block, we may
346 # end up trying to add an edge from the parameterized role to itself,
347 # which we can just ignore.
348 19 ▶ return if $p{from} eq $p{to};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"from"} eq $p{"to"} 0 19 19
349  
350 19 my $key = join ' - ', @p{qw( from to )};
351 19 ▶ return if $self->_already_seen_edge($key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_already_seen_edge($key) 1 18 19
352  
353 $self->_renderer->add_edge(
354 from => $p{from},
355 to => $p{to},
356 weight => $p{weight},
357 type => $p{type},
358 18 );
359  
360 18 $self->_seen_edge($key);
361  
362 18 return;
363 }
364  
365 78 ▶sub _node_label_for {
Subroutine _node_label_for: called CC=3 SCAR=12.0
366 78 my $self = shift;
367 78 my $meta = shift;
368  
369 78 ▶ return $meta unless blessed $meta && $meta->can('name');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($meta) and $meta->can("name") 0 78 78
Condition33%1/3
builtin::blessed($meta) and $meta->can("name")
!ll&&!rl&&r
0078
MC/DC0%0/2
builtin::blessed($meta) and $meta->can("name")
builtin::blessed($meta) $meta->can("name")
Truth table33%1/3
A and B
A= builtin::blessed($meta)B= $meta->can("name")
ABresult
0X0
100
111
370 78 return $meta->name;
371 }
372  
373 1 __PACKAGE__->meta->make_immutable;
374  
375 1 1;
376  
377 # ABSTRACT: Produce graphs showing meta-information about classes and roles
378  
379 __END__
↑ Summary lib/Meta/Grapher/Moose/Renderer/Plantuml.pm »