blib/lib/Data/Formula.pm

filter: Statementstmt 99.3% 147 / 148 Branchbran 88.6% 39 / 44 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 96.3% 26 / 27 Podpod 50.0% 1 / 2 totaltotal 95.5% 217 / 227 CC 27 SCAR 14.2
CC
27
Cov
96%
calculate
11.0
_build__tokens
5.0
rpn_standard_operator
4.0
CRAP 4.1
↑ Summary
1 package Data::Formula;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.02';
14 1 our @CARP_NOT;
15  
16 1 my %operators = (
17 '+' => {
18 method => 'plus',
19 calc => 'plus',
20 prio => 10,
21 },
22 '-' => {
23 method => 'minus',
24 calc => 'minus',
25 prio => 10,
26 },
27 '*' => {
28 method => 'multiply',
29 calc => 'multiply',
30 prio => 50,
31 },
32 '/' => {
33 method => 'divide',
34 calc => 'divide',
35 prio => 50,
36 },
37 '(' => {method => 'bracket_left',},
38 ')' => {method => 'bracket_right',},
39 );
40  
41 11 ▶has 'variables' => (is => 'rw', isa => 'ArrayRef', default => sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
42 0 ▶has 'formula' => (is => 'ro', isa => 'Str', default => sub {[]});
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 1 has '_tokens' => (is => 'ro', isa => 'ArrayRef', lazy_build => 1,);
44 1 has '_rpn' => (is => 'ro', isa => 'ArrayRef', lazy_build => 1,);
45 1 has '_op_indent' => (is => 'rw', isa => 'Int', default => 0,);
46 1 has 'used_variables' => (is => 'ro', isa => 'ArrayRef', lazy_build => 1,);
47  
48 1 has 'on_error' => (
49 is => 'rw',
50 predicate => 'has_on_error',
51 clearer => 'clear_on_error',
52 );
53 1 has 'on_missing_token' => (
54 is => 'rw',
55 predicate => 'has_on_missing_token',
56 clearer => 'clear_on_missing_token',
57 );
58  
59 27 ▶sub _indented_operator {
Subroutine _indented_operator: called CC=1 SCAR=0.0
60 27 my ($self, $op) = @_;
61 return {
62 name => $op,
63 27 %{$operators{$op}},
64 27 prio => ($operators{$op}->{prio} + ($self->_op_indent * 100)),
65 };
66 }
67  
68 13 ▶sub _build__rpn {
Subroutine _build__rpn: called CC=3 SCAR=11.0
69 13 my ($self) = @_;
70  
71 13 my $rpn = [];
72 13 my $ops = [];
73 13 foreach my $token (@{$self->_tokens}) {
74 77 ▶ if ($operators{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operators{$token}) { } 37 40 77
75 37 my $rpn_method = '_rpn_method_' . $operators{$token}->{method};
76 37 ($rpn, $ops) = $self->$rpn_method($rpn, $ops);
77 }
78 else {
79 40 push(@$rpn, $token);
80 }
81 }
82  
83 13 return [@$rpn, reverse(@$ops)];
84 }
85  
86 7 ▶sub _rpn_method_plus {
Subroutine _rpn_method_plus: called CC=1 SCAR=0.0
87 7 my ($self, $rpn, $ops) = @_;
88 7 return $self->rpn_standard_operator('+', $rpn, $ops);
89 }
90  
91 8 ▶sub _rpn_method_minus {
Subroutine _rpn_method_minus: called CC=1 SCAR=0.0
92 8 my ($self, $rpn, $ops) = @_;
93 8 return $self->rpn_standard_operator('-', $rpn, $ops);
94 }
95  
96 3 ▶sub _rpn_method_multiply {
Subroutine _rpn_method_multiply: called CC=1 SCAR=0.0
97 3 my ($self, $rpn, $ops) = @_;
98 3 return $self->rpn_standard_operator('*', $rpn, $ops);
99 }
100  
101 9 ▶sub _rpn_method_divide {
Subroutine _rpn_method_divide: called CC=1 SCAR=0.0
102 9 my ($self, $rpn, $ops) = @_;
103 9 return $self->rpn_standard_operator('/', $rpn, $ops);
104 }
105  
106 27 ▶sub rpn_standard_operator {
Subroutine rpn_standard_operator: called CC=4 SCAR=13.9
Pod undocumented
107 27 my ($self, $cur_op, $rpn, $ops) = @_;
108 27 my $prio = $operators{$cur_op}->{prio} + ($self->_op_indent * 100);
109 27 ▶ if (@$ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ops) 14 13 27
110 14 ▶ while (@$ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ops) 18 6 24
111 18 my $prev_op = $ops->[-1];
112 18 ▶ if ($prev_op->{prio} >= $prio) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_op->{'prio'} >= $prio) { } 10 8 18
113 10 push(@$rpn, pop(@$ops));
114 }
115 else {
116 8 last;
117 }
118 }
119 }
120 27 push(@$ops, $self->_indented_operator($cur_op));
121  
122 27 return ($rpn, $ops);
123 }
124  
125 5 ▶sub _rpn_method_bracket_left {
Subroutine _rpn_method_bracket_left: called CC=1 SCAR=0.0
126 5 my ($self, $rpn, $ops) = @_;
127  
128 5 $self->_op_indent($self->_op_indent + 1);
129  
130 5 return ($rpn, $ops);
131 }
132  
133 5 ▶sub _rpn_method_bracket_right {
Subroutine _rpn_method_bracket_right: called CC=1 SCAR=0.0
134 5 my ($self, $rpn, $ops) = @_;
135  
136 5 $self->_op_indent($self->_op_indent - 1);
137  
138 5 return ($rpn, $ops);
139 }
140  
141 2 ▶sub _build_used_variables {
Subroutine _build_used_variables: called CC=1 SCAR=0.0
142 2 my ($self, @rpn) = @_;
143  
144 return [
145 10 grep {$_ !~ m/^[0-9]+$/}
146 2 grep {!$operators{$_}} @{$self->_tokens}
147 ];
148 }
149  
150 13 ▶sub _build__tokens {
Subroutine _build__tokens: called CC=5 SCAR=16.1
151 13 my ($self) = @_;
152  
153 13 my @tokens;
154 13 my $formula = $self->formula;
155 13 $formula =~ s/\s//g;
156  
157 13 my $op_regexp = join('', map {q{\\} . $_} keys %operators);
158 13 my $op_regexp_with_variable = '^([^' . $op_regexp . ']*?)([' . $op_regexp . '])';
159 13 ▶ while ($formula =~ m/$op_regexp_with_variable/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formula =~ /$op_regexp_with_variable/) 37 13 50
160 37 my $variable = $1;
161 37 my $operator = $2;
162 37 ▶ push(@tokens, $variable) if length($variable);
Branch: 1 branch
BranchTrueFalseTotal
if length $variable 31 6 37
163 37 push(@tokens, $operator);
164 37 $formula = substr($formula, length($variable . $operator));
165 }
166 13 ▶ if (length($formula)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $formula) 9 4 13
167 9 push(@tokens, $formula);
168 }
169  
170 13 ▶ return [map {$_ =~ m/^[0-9]+$/ ? $_ + 0 : $_} @tokens];
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^[0-9]+$/ ? : 2 75 77
171 }
172  
173 7 ▶sub _rpn_calc_plus {
Subroutine _rpn_calc_plus: called CC=2 SCAR=7.0
174 7 my ($self, $rpn) = @_;
175  
176 7 ▶ die 'not enough parameters left on stack'
Branch: 1 branch
BranchTrueFalseTotal
unless @$rpn > 1 0 7 7
177 unless @$rpn > 1;
178  
179 7 my $val2 = pop(@$rpn);
180 7 my $val1 = pop(@$rpn);
181  
182 7 push(@$rpn, $val1 + $val2);
183 7 return $rpn;
184 }
185  
186 8 ▶sub _rpn_calc_minus {
Subroutine _rpn_calc_minus: called CC=2 SCAR=7.0
187 8 my ($self, $rpn) = @_;
188  
189 8 ▶ die 'not enough parameters left on stack'
Branch: 1 branch
BranchTrueFalseTotal
unless @$rpn > 1 0 8 8
190 unless @$rpn > 1;
191  
192 8 my $val2 = pop(@$rpn);
193 8 my $val1 = pop(@$rpn);
194  
195 8 push(@$rpn, $val1 - $val2);
196 8 return $rpn;
197 }
198  
199 3 ▶sub _rpn_calc_multiply {
Subroutine _rpn_calc_multiply: called CC=2 SCAR=7.0
200 3 my ($self, $rpn) = @_;
201  
202 3 ▶ die 'not enough parameters left on stack'
Branch: 1 branch
BranchTrueFalseTotal
unless @$rpn > 1 0 3 3
203 unless @$rpn > 1;
204  
205 3 my $val2 = pop(@$rpn);
206 3 my $val1 = pop(@$rpn);
207  
208 3 push(@$rpn, $val1 * $val2);
209 3 return $rpn;
210 }
211  
212 8 ▶sub _rpn_calc_divide {
Subroutine _rpn_calc_divide: called CC=3 SCAR=11.0
213 8 my ($self, $rpn) = @_;
214  
215 8 ▶ die 'not enough parameters left on stack'
Branch: 1 branch
BranchTrueFalseTotal
unless @$rpn > 1 0 8 8
216 unless @$rpn > 1;
217  
218 8 my $val2 = pop(@$rpn);
219 8 my $val1 = pop(@$rpn);
220  
221 8 ▶ die "Illegal division by zero\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $val2 5 3 8
222 unless $val2;
223  
224 3 push(@$rpn, $val1 / $val2);
225 3 return $rpn;
226 }
227  
228 13 ▶sub calculate {
Subroutine calculate: called CC=11 SCAR=24.0
Pod documented
229 13 my ($self, %variables) = @_;
230  
231 13 ▶ if (@{$self->variables} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->variables;} == 0) 11 2 13
232 11 $self->variables([keys %variables]);
233 }
234  
235 13 my $rpn = [];
236 13 my $ops = [];
237 13 foreach my $token (@{$self->_rpn}) {
238 66 ▶ if (ref($token) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token eq 'HASH') { } 26 40 66
239 26 my $rpn_method = '_rpn_calc_' . $token->{calc};
240 26 ▶ ($rpn) = eval {$self->$rpn_method($rpn)} // [];
Condition100%2/2
eval { do { $self->$rpn_method($rpn) } } // []
l!l
215
MC/DC100%1/1
eval { do { $self->$rpn_method($rpn) } } // []
eval { do { $self->$rpn_method($rpn) } }
Truth table100%2/2
A
A= eval { do { $self->$rpn_method($rpn) } }
Aresult
11
00
241 26 ▶ $self->_report_error($rpn, $@)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 5 21 26
242 if $@;
243 }
244 else {
245 40 ▶ if (exists($variables{$token})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $variables{$token}) { } 34 6 40
elsif ($token =~ /^[+\-]?\d*\.?\d*$/) { } 2 4 6
246 34 ▶ push(@$rpn, $variables{$token} // 0);
Condition50%1/2
$variables{$token} // 0
l!l
340
MC/DC0%0/1
$variables{$token} // 0
$variables{$token}
Truth table50%1/2
A
A= $variables{$token}
Aresult
11
00
247 }
248 elsif ($token =~ /^[+\-]?\d*\.?\d*$/) {
249 2 push(@$rpn, $token);
250 }
251 else {
252 4 ▶ if (my $on_missing = $self->has_on_missing_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_missing = $self->has_on_missing_token) { } 1 3 4
253 1 ▶ push(@$rpn, (ref($on_missing) eq 'CODE' ? $on_missing->($token) : $on_missing));
Branch: 1 branch
BranchTrueFalseTotal
ref $on_missing eq 'CODE' ? : 0 1 1
254 }
255 else {
256 3 $self->_report_error($rpn,
257 '"' . $token . '" is not a literal number, not a valid token');
258 }
259 }
260 }
261 }
262  
263 11 return @$rpn[0];
264 }
265  
266 8 ▶sub _report_error {
Subroutine _report_error: called CC=3 SCAR=11.0
267 8 my ($self, $rpn, $err) = @_;
268 8 local @CARP_NOT = __PACKAGE__;
269 8 chomp($err);
270 8 ▶ if ($self->has_on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_on_error) { } 6 2 8
271 6 my $on_err = $self->on_error;
272 6 ▶ push(@$rpn, (ref($on_err) eq 'CODE' ? $on_err->($err) : $on_err));
Branch: 1 branch
BranchTrueFalseTotal
ref $on_err eq 'CODE' ? : 1 5 6
273 }
274 else {
275 2 croak($err);
276 }
277 }
278  
279 1 1;
280  
281 __END__
↑ Summary