Carton/Tree.pm

filter: Statementstmt 10.3% 9 / 87 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 9.0% 2 / 22 Podpod 0.0% 0 / 3 totaltotal 7.5% 11 / 145 CC 19 SCAR 28.7
CC
19
Cov
8%
finalize
56.0
_walk_down
30.0
spec
12.0
CRAP 17.6
« Carton.pm ↑ Summary Carton/CLI.pm »
1 package Carton::TreeNode;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 my %cache;
6  
7 0 ▶sub cached {
Subroutine cached: not called CC=1 SCAR=6.9
8 0 my($class, $key) = @_;
9 0 return $cache{$key};
10 }
11  
12 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
13 0 my($class, $key, $pool) = @_;
14  
15 0 ▶ my $meta = delete $pool->{$key} || {};
Condition0%0/2
delete $pool->{$key} || {}
l!l
00
MC/DC0%0/1
delete $pool->{$key} || {}
delete $pool->{$key}
Truth table0%0/2
A
A= delete $pool->{$key}
Aresult
11
00
16  
17 0 my $self = bless [ $key, $meta, [] ], $class;
18 0 $cache{$key} = $self;
19  
20 0 return $self;
21 }
22  
23 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
24 0 my $self = shift;
25 0 ▶ $self->walk_down(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 my($node, $depth) = @_;
27 0 print " " x $depth;
28 0 print $node->key, "\n";
29 0 });
30 }
31  
32 0 ▶sub walk_down {
Subroutine walk_down: not called CC=1 SCAR=6.9
33 0 my($self, $cb) = @_;
34 0 $self->_walk_down($cb, undef, 0);
35 }
36  
37 0 ▶sub _walk_down {
Subroutine _walk_down: not called CC=5 SCAR=34.0
38 0 my($self, $pre_cb, $post_cb, $depth) = @_;
39  
40 0 my @child = $self->children;
41 0 for my $child ($self->children) {
42 0 local $Carton::Tree::Abort = 0;
43 0 ▶ if ($pre_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_cb) 0 0 0
44 0 $pre_cb->($child, $depth, $self);
45 }
46  
47 0 ▶ unless ($Carton::Tree::Abort) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Carton::Tree::Abort) 0 0 0
48 0 $child->_walk_down($pre_cb, $post_cb, $depth + 1);
49 }
50  
51 0 ▶ if ($post_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_cb) 0 0 0
52 0 $post_cb->($child, $depth, $self);
53 }
54 }
55 }
56  
57 0 ▶sub abort {
Subroutine abort: not called CC=1 SCAR=6.9
58 0 $Carton::Tree::Abort = 1;
59 }
60  
61 0 ▶sub key { $_[0]->[0] }
Subroutine key: not called CC=1 SCAR=6.9
62 0 ▶sub metadata { $_[0]->[1] }
Subroutine metadata: not called CC=1 SCAR=6.9
63  
64 0 ▶sub spec {
Subroutine spec: not called CC=3 SCAR=24.8
65 0 my $self = shift;
66  
67 0 my $meta = $self->metadata;
68 0 ▶ my $version = $meta->{provides}{$meta->{name}}{version} || $meta->{version};
Condition0%0/3
$meta->{'provides'}{$meta->{'name'}}{'version'} || $meta->{'version'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta->{'provides'}{$meta->{'name'}}{'version'} || $meta->{'version'}
$meta->{'provides'}{$meta->{'name'}}{'version'} $meta->{'version'}
Truth table0%0/3
A || B
A= $meta->{'provides'}{$meta->{'name'}}{'version'}B= $meta->{'version'}
ABresult
1X1
011
000
69 0 ▶ $meta->{name} . ($version ? '~' . $version : '');
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 0 0 0
70 }
71  
72 0 ▶sub children { @{$_[0]->[2]} }
Subroutine children: not called CC=1 SCAR=6.9
73  
74 0 ▶sub add_child {
Subroutine add_child: not called CC=1 SCAR=6.9
75 0 my $self = shift;
76 0 push @{$self->[2]}, @_;
77 }
78  
79 0 ▶sub remove_child {
Subroutine remove_child: not called CC=3 SCAR=24.8
80 0 my($self, $rm) = @_;
81  
82 0 my @new;
83 0 for my $child (@{$self->[2]}) {
84 0 ▶ if ($rm->key eq $child->key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rm->key eq $child->key) { } 0 0 0
85 0 undef $child;
86 } else {
87 0 push @new, $child;
88 }
89 }
90  
91 0 $self->[2] = \@new;
92 }
93  
94 0 ▶sub is {
Subroutine is: not called CC=1 SCAR=6.9
95 0 my($self, $node) = @_;
96 0 $self->key eq $node->key;
97 }
98  
99 package Carton::Tree;
100 2 our @ISA = qw(Carton::TreeNode);
101  
102 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
103 0 bless [0, {}, []], shift;
104 }
105  
106 0 ▶sub finalize {
Subroutine finalize: not called CC=7 SCAR=40.3
Pod undocumented
107 0 my($self, $want_root) = @_;
108  
109 0 ▶ $want_root ||= {};
Condition0%0/2
$want_root ||= {}
l!l
00
MC/DC0%0/1
$want_root ||= {}
$want_root
Truth table0%0/2
A
A= $want_root
Aresult
11
00
110  
111 0 my %subtree;
112 my @ancestor;
113  
114 0 ▶ my $down = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
115 0 my($node, $depth, $parent) = @_;
116  
117 0 ▶ if (grep $node->is($_), @ancestor) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep $node->is($_), @ancestor) 0 0 0
118 0 $parent->remove_child($node);
119 0 return $self->abort;
120 }
121  
122 0 ▶ $subtree{$node->key} = 1 if $depth > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 0 0 0
123  
124 0 push @ancestor, $node;
125 0 return 1;
126 0 };
127  
128 0 ▶ my $up = sub { pop @ancestor };
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 $self->_walk_down($down, $up, 0);
130  
131 # normalize: remove root nodes that are sub-tree of another
132 0 for my $child ($self->children) {
133 0 ▶ if ($subtree{$child->key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtree{$child->key}) 0 0 0
134 0 $self->remove_child($child);
135 }
136 }
137  
138 # Ugh, but if the build file is there, restore the links to sub-tree as a root elements
139 0 my %curr_root = map { ($_->key => 1) } $self->children;
140 0 for my $key (keys %$want_root) {
141 0 ▶ my $node = $self->find_child($key) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->find_child($key) or next 0 0 0
142 0 ▶ unless ($curr_root{$node->key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($curr_root{$node->key}) 0 0 0
143 0 $self->add_child($node);
144 }
145 }
146  
147 0 %cache = ();
148 }
149  
150 0 ▶sub find_child {
Subroutine find_child: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my($self, $key) = @_;
152  
153 0 my $child;
154 0 ▶ $self->walk_down(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 ▶ if ($_[0]->key eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->key eq $key) 0 0 0
156 0 $child = $_[0];
157 0 return $self->abort;
158 }
159 0 });
160  
161 0 return $child;
162 }
163  
164 2 1;
« Carton.pm ↑ Summary Carton/CLI.pm »