blib/lib/Syntax/Feature/Try.pm

filter: Statementstmt 100% 72 / 72 Branchbran 96.1% 25 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 3 totaltotal 96.9% 125 / 129 CC 18 SCAR 12.6
CC
18
Cov
99%
_exception_match_args
7.0
_statement
6.0
register_exception_matcher
3.0
CRAP 3.5
↑ Summary src/try-catch-stack.c »
1 package Syntax::Feature::Try;
2  
3 27 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 our $VERSION = '1.005';
12 27 XSLoader::load();
13 }
14  
15 27 my @custom_exception_matchers;
16  
17 57 ▶sub install {
Subroutine install: called CC=1 SCAR=0.0
Pod undocumented
18 57 $^H{+HINTKEY_ENABLED} = 1;
19 }
20  
21 1 ▶sub uninstall {
Subroutine uninstall: called CC=1 SCAR=0.0
Pod undocumented
22 1 $^H{+HINTKEY_ENABLED} = 0;
23 }
24  
25 7 ▶sub register_exception_matcher {
Subroutine register_exception_matcher: called CC=3 SCAR=11.0
Pod undocumented
26 7 my ($code_ref) = @_;
27  
28 7 ▶ if (ref($code_ref) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code_ref ne "CODE") 3 4 7
29 3 croak "Invalid parameter: expected CODE reference.";
30 }
31  
32 4 ▶ if (not grep { $_ == $code_ref } @custom_exception_matchers) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_ == $code_ref;} @custom_exception_matchers)) 2 2 4
33 2 push @custom_exception_matchers, $code_ref;
34 }
35 }
36  
37 # only for tests:
38 3 ▶sub _custom_exception_matchers { @custom_exception_matchers }
Subroutine _custom_exception_matchers: called CC=1 SCAR=0.0
39  
40 # TODO convert "our" to "my" variables
41 27 our $is_end_of_block;
42 27 our $return_values;
43  
44 270 ▶sub _statement {
Subroutine _statement: called CC=6 SCAR=17.9
45 270 my ($try_block, $catch_list, $finally_block) = @_;
46  
47 270 my $stm_handler = bless {finally => $finally_block}, __PACKAGE__;
48  
49 270 local $@;
50 270 local $is_end_of_block;
51 270 my $exception = run_block($stm_handler, $try_block, 1);
52 269 ▶ if ($exception and $catch_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception and $catch_list) 137 132 269
Condition100%3/3
$exception and $catch_list
!ll&&!rl&&r
1284137
MC/DC100%2/2
$exception and $catch_list
$exception $catch_list
Truth table100%3/3
A and B
A= $exceptionB= $catch_list
ABresult
0X0
100
111
53 137 my $catch_block = _get_exception_handler($exception, $catch_list);
54 137 ▶ if ($catch_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($catch_block) 136 1 137
55 136 local $is_end_of_block;
56 136 $exception = run_block($stm_handler, $catch_block, 1, $exception);
57 }
58 }
59  
60 269 ▶ if ($finally_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finally_block) 166 103 269
61 166 delete $stm_handler->{finally};
62 166 local $is_end_of_block;
63 166 run_block($stm_handler, $finally_block);
64 }
65  
66 267 ▶ if ($exception) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception) 11 256 267
67 11 _rethrow($exception);
68 }
69  
70 256 $return_values = $stm_handler->{return};
71 256 return $stm_handler->{return};
72 }
73  
74 270 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
75 270 my ($self) = @_;
76 270 local $is_end_of_block;
77 270 ▶ run_block($self, $self->{finally}) if $self->{finally};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finally"} 1 269 270
78 }
79  
80 137 ▶sub _get_exception_handler {
Subroutine _get_exception_handler: called CC=3 SCAR=11.0
81 137 my ($exception, $catch_list) = @_;
82  
83 137 foreach my $item (@{ $catch_list }) {
84 176 my ($block_ref, @args) = @$item;
85 176 ▶ return $block_ref if _exception_match_args($exception, @args);
Branch: 1 branch
BranchTrueFalseTotal
if _exception_match_args($exception, @args) 136 40 176
86 }
87 }
88  
89 176 ▶sub _exception_match_args {
Subroutine _exception_match_args: called CC=7 SCAR=19.5
90 176 my ($exception, $className) = @_;
91  
92 176 ▶ return 1 if not defined $className; # without args catch all exceptions
Branch: 1 branch
BranchTrueFalseTotal
unless defined $className 23 153 176
93  
94 153 foreach my $matcher (@custom_exception_matchers) {
95 10 my $result = $matcher->($exception, $className);
96 10 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 7 3 10
97 }
98  
99 146 ▶ if (Moose::Util::TypeConstraints->can('find_type_constraint')) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Moose::Util::TypeConstraints"->can("find_type_constraint")) 7 139 146
100 7 my $type = Moose::Util::TypeConstraints::find_type_constraint($className);
101 7 ▶ return $type->check($exception) if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 7 0 7
102 }
103  
104 139 ▶ return blessed($exception) && $exception->isa($className);
Condition100%3/3
builtin::blessed($exception) && $exception->isa($className)
!ll&&!rl&&r
922108
MC/DC100%2/2
builtin::blessed($exception) && $exception->isa($className)
builtin::blessed($exception) $exception->isa($className)
Truth table100%3/3
A && B
A= builtin::blessed($exception)B= $exception->isa($className)
ABresult
0X0
100
111
105 }
106  
107 11 ▶sub _rethrow {
Subroutine _rethrow: called CC=1 SCAR=0.0
108 11 die (@_);
109 }
110  
111 272 ▶sub _set_is_end_of_block {
Subroutine _set_is_end_of_block: called CC=1 SCAR=0.0
112 272 $is_end_of_block = 1;
113 }
114  
115 142 ▶sub _get_return_value {
Subroutine _get_return_value: called CC=2 SCAR=6.9
116 142 my $return = $return_values;
117 142 undef $return_values;
118  
119 142 ▶ return wantarray ? @$return : $return->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 46 96 142
120 }
121  
122 27 1;
123  
124 __END__
↑ Summary src/try-catch-stack.c »