blib/lib/Text/Xslate/PP/Method.pm

filter: Statementstmt 100% 119 / 119 Branchbran 74.0% 37 / 50 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 2 totaltotal 90.8% 179 / 197 CC 28 SCAR 11.9
CC
28
Cov
92%
tx_methodcall
8.0
_array_sort
4.0
_array_reduce
4.0
CRAP 3.3
« blib/lib/Text/Xslate/Bridge/Star.pm ↑ Summary blib/lib/Text/Xslate/PP/Type/Raw.pm »
1 package Text::Xslate::PP::Method;
2 # xs/xslate-methods.xs in pure Perl
3 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 190 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 190 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 190 require Text::Xslate::PP;
11 190 require Text::Xslate::PP::State;
12 190 require Text::Xslate::PP::Type::Pair;
13  
14 190 if(!Text::Xslate::PP::_PP_ERROR_VERBOSE()) {
15 190 our @CARP_NOT = qw(
16 Text::Xslate::PP::Opcode
17 );
18 }
19  
20 190 our $_st;
21 190 *_st = *Text::Xslate::PP::_current_st;
22  
23 190 our $_context;
24  
25 2 ▶sub _array_first {
Subroutine _array_first: called CC=2 SCAR=7.1
26 2 my($array_ref) = @_;
27 2 ▶ return $_st->bad_arg('first') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
28 2 return $array_ref->[0];
29 }
30  
31 2 ▶sub _array_last {
Subroutine _array_last: called CC=2 SCAR=7.1
32 2 my($array_ref) = @_;
33 2 ▶ return $_st->bad_arg('last') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
34 2 return $array_ref->[-1];
35 }
36  
37 7 ▶sub _array_size {
Subroutine _array_size: called CC=2 SCAR=6.9
38 7 my($array_ref) = @_;
39 7 ▶ return $_st->bad_arg('size') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 1 6 7
40 6 return scalar @{$array_ref};
41 }
42  
43 66 ▶sub _array_join {
Subroutine _array_join: called CC=2 SCAR=7.0
44 66 my($array_ref, $sep) = @_;
45 66 ▶ return $_st->bad_arg('join') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 66 66
46 66 return join $sep, @{$array_ref};
47 }
48  
49 15 ▶sub _array_reverse {
Subroutine _array_reverse: called CC=2 SCAR=7.0
50 15 my($array_ref) = @_;
51 15 ▶ return $_st->bad_arg('reverse') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 15 15
52 15 return [ reverse @{$array_ref} ];
53 }
54  
55 15 ▶sub _array_sort {
Subroutine _array_sort: called CC=4 SCAR=13.9
56 15 my($array_ref, $callback) = @_;
57 15 ▶ return $_st->bad_arg('sort') if !(@_ == 1 or @_ == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 or @_ == 2 0 15 15
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
5100
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
58 15 ▶ if(@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 5 10 15
59 5 return [ sort @{$array_ref} ];
60 }
61 else {
62 return [ sort {
63 83 push @{ $_st->{ SP } }, [ $a, $b ];
64 83 $_st->proccall($callback, $_context) + 0; # need to numify
65 10 } @{$array_ref} ];
66 }
67 }
68  
69 24 ▶sub _array_map {
Subroutine _array_map: called CC=2 SCAR=7.0
70 24 my($array_ref, $callback) = @_;
71 24 ▶ return $_st->bad_arg('map') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 24 24
72 return [ map {
73 63 push @{ $_st->{ SP } }, [ $_ ];
74 63 $_st->proccall($callback, $_context);
75 24 } @{$array_ref} ];
76 }
77  
78 10 ▶sub _array_reduce {
Subroutine _array_reduce: called CC=4 SCAR=13.9
79 10 my($array_ref, $callback) = @_;
80 10 ▶ return $_st->bad_arg('reduce') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 10 10
81 10 ▶ return $array_ref->[0] if @{$array_ref} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$array_ref;} < 2 2 8 10
82  
83 8 my $x = $array_ref->[0];
84 8 ▶ for(my $i = 1; $i < @{$array_ref}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$array_ref;}) 64 8 72
85 64 push @{ $_st->{ SP } }, [ $x, $array_ref->[$i] ];
86 64 $x = $_st->proccall($callback, $_context);
87 }
88 8 return $x;
89 }
90  
91 5 ▶sub _array_merge {
Subroutine _array_merge: called CC=3 SCAR=11.0
92 5 my($array_ref, $value) = @_;
93 5 ▶ return $_st->bad_arg('merge') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 5 5
94 5 ▶ return [ @{$array_ref}, ref($value) eq 'ARRAY' ? @{$value} : $value ];
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 2 3 5
95 }
96  
97 3 ▶sub _hash_size {
Subroutine _hash_size: called CC=2 SCAR=7.0
98 3 my($hash_ref) = @_;
99 3 ▶ return $_st->bad_arg('size') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 3 3
100 3 return scalar keys %{$hash_ref};
101 }
102  
103 41 ▶sub _hash_keys {
Subroutine _hash_keys: called CC=2 SCAR=7.0
104 41 my($hash_ref) = @_;
105 41 ▶ return $_st->bad_arg('keys') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 41 41
106 41 return [sort { $a cmp $b } keys %{$hash_ref}];
107 }
108  
109 8 ▶sub _hash_values {
Subroutine _hash_values: called CC=2 SCAR=7.0
110 8 my($hash_ref) = @_;
111 8 ▶ return $_st->bad_arg('values') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 8 8
112 8 return [map { $hash_ref->{$_} } @{ _hash_keys($hash_ref) } ];
113 }
114  
115 13 ▶sub _hash_kv {
Subroutine _hash_kv: called CC=2 SCAR=7.0
116 13 my($hash_ref) = @_;
117 13 ▶ $_st->bad_arg('kv') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 13 13
118 return [
119 25 map { Text::Xslate::PP::Type::Pair->new(key => $_, value => $hash_ref->{$_}) }
120 13 @{ _hash_keys($hash_ref) }
121 ];
122 }
123  
124 4 ▶sub _hash_merge {
Subroutine _hash_merge: called CC=2 SCAR=7.0
125 4 my($hash_ref, $other_hash_ref) = @_;
126 4 ▶ $_st->bad_arg('merge') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 4 4
127  
128 4 return { %{$hash_ref}, %{$other_hash_ref} };
129 }
130  
131 190 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
132 190 our %builtin_method = (
133 'array::first' => \&_array_first,
134 'array::last' => \&_array_last,
135 'array::size' => \&_array_size,
136 'array::join' => \&_array_join,
137 'array::reverse' => \&_array_reverse,
138 'array::sort' => \&_array_sort,
139 'array::map' => \&_array_map,
140 'array::reduce' => \&_array_reduce,
141 'array::merge' => \&_array_merge,
142  
143 'hash::size' => \&_hash_size,
144 'hash::keys' => \&_hash_keys,
145 'hash::values' => \&_hash_values,
146 'hash::kv' => \&_hash_kv,
147 'hash::merge' => \&_hash_merge,
148 );
149 }
150  
151 298 ▶sub tx_register_builtin_methods {
Subroutine tx_register_builtin_methods: called CC=2 SCAR=6.9
Pod undocumented
152 298 my($hv) = @_;
153 298 our %builtin_method;
154 298 foreach my $name(keys %builtin_method) {
155 4172 $hv->{$name} = $builtin_method{$name};
156 }
157 }
158  
159 290 ▶sub tx_methodcall {
Subroutine tx_methodcall: called CC=8 SCAR=20.8
Pod undocumented
160 290 my($st, $context, $method, $invocant, @args) = @_;
161  
162 290 ▶ if(Scalar::Util::blessed($invocant)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($invocant)) 41 249 290
163 41 my $retval = eval { $invocant->$method(@args) };
164 41 ▶ $st->error($context, "%s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 39 41
165 40 return $retval;
166 }
167  
168 249 ▶ my $type = ref($invocant) eq 'ARRAY' ? 'array::'
Branch: 3 branches
BranchTrueFalseTotal
ref $invocant eq 'ARRAY' ? : 169 80 249
ref $invocant eq 'HASH' ? : 54 26 80
defined $invocant ? : 20 6 26
169 : ref($invocant) eq 'HASH' ? 'hash::'
170 : defined($invocant) ? 'scalar::'
171 : 'nil::';
172 249 my $fq_name = $type . $method;
173  
174 249 ▶ if(my $body = $st->symbol->{$fq_name}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $st->symbol->{$fq_name}) 241 8 249
175 241 push @{ $st->{ SP } }, [ $invocant, @args ]; # re-pushmark
176 241 local $_context = $context;
177 241 return $st->proccall($body, $context);
178 }
179 8 ▶ if(!defined $invocant) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $invocant) 6 2 8
180 6 $st->warn($context, "Use of nil to invoke method %s", $method);
181 6 return undef;
182 }
183  
184 2 $st->error($context, "Undefined method %s called for %s",
185 $method, $invocant);
186  
187 2 return undef;
188 }
189  
190 190 1;
191 __END__
« blib/lib/Text/Xslate/Bridge/Star.pm ↑ Summary blib/lib/Text/Xslate/PP/Type/Raw.pm »