blib/lib/Variable/Declaration.pm

filter: Statementstmt 100% 114 / 114 Branchbran 100% 36 / 36 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 6 totaltotal 97.0% 194 / 200 CC 26 SCAR 12.4
CC
26
Cov
100%
_valid
9.0
_render_declaration
4.0
_lines_declaration
4.0
CRAP 3.5
↑ Summary Build »
1 package Variable::Declaration;
2 11 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = "0.04";
7  
8 11 ▶use Keyword::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use PPR;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Data::Lock ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Type::Tie ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 our $LEVEL;
16 11 our $DEFAULT_LEVEL = 2;
17  
18 sub import {
19 18 ▶ shift;
Subroutine import: called CC=3 SCAR=11.0
20 18 my %args = @_;
21 18 my $caller = caller;
22  
23 $LEVEL = exists $args{level} ? $args{level}
24 18 ▶ : exists $ENV{'Variable::Declaration::LEVEL'} ? $ENV{'Variable::Declaration::LEVEL'}
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'level'} ? : 4 14 18
exists $ENV{'Variable::Declaration::LEVEL'} ? : 3 11 14
25 : $DEFAULT_LEVEL;
26  
27 18 feature->import::into($caller, 'state');
28  
29 18 Keyword::Simple::define 'let' => \&define_let;
30 18 Keyword::Simple::define 'static' => \&define_static;
31 18 Keyword::Simple::define 'const' => \&define_const;
32 }
33  
34 sub unimport {
35 1 ▶ Keyword::Simple::undefine 'let';
Subroutine unimport: called CC=1 SCAR=0.0
36 1 Keyword::Simple::undefine 'static';
37 1 Keyword::Simple::undefine 'const';
38 }
39  
40 17 ▶sub define_let { define_declaration(let => 'my', @_) }
Subroutine define_let: called CC=1 SCAR=0.0
Pod undocumented
41 12 ▶sub define_static { define_declaration(static => 'state', @_) }
Subroutine define_static: called CC=1 SCAR=0.0
Pod undocumented
42 13 ▶sub define_const { define_declaration(const => 'my', @_) }
Subroutine define_const: called CC=1 SCAR=0.0
Pod undocumented
43  
44 sub define_declaration {
45 42 ▶ my ($declaration, $perl_declaration, $ref) = @_;
Subroutine define_declaration: called CC=1 SCAR=0.0
Pod undocumented
46  
47 42 my $match = _valid($declaration => _parse($$ref));
48 22 my $tv = _parse_type_varlist($match->{type_varlist});
49 22 my $args = +{ declaration => $declaration, perl_declaration => $perl_declaration, %$match, %$tv, level => $LEVEL };
50  
51 22 substr($$ref, 0, length $match->{statement}) = _render_declaration($args);
52 }
53  
54 21 ▶sub croak { Carp::croak @_ }
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
55  
56 1 ▶sub data_lock { Data::Lock::dlock @_ }
Subroutine data_lock: called CC=1 SCAR=0.0
Pod undocumented
57  
58 sub type_tie(\[$@%]@);
59 {
60 11 *type_tie = \&Type::Tie::ttie;
61 }
62  
63 sub _valid {
64 42 ▶ my ($declaration, $match) = @_;
Subroutine _valid: called CC=9 SCAR=22.0
65  
66 croak "variable declaration is required'"
67 42 ▶ unless $match->{type_varlist};
Branch: 1 branch
BranchTrueFalseTotal
unless $match->{"type_varlist"} 9 33 42
68  
69 33 my ($eq, $assign) = ($match->{eq}, $match->{assign});
70 33 ▶ if ($declaration eq 'const') {
Branch: 1 branch
BranchTrueFalseTotal
if ($declaration eq 'const') { } 10 23 33
71 10 ▶ croak "'const' declaration must be assigned"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eq and defined $assign 7 3 10
Condition100%3/3
defined $eq and defined $assign
!ll&&!rl&&r
613
MC/DC100%2/2
defined $eq and defined $assign
defined $eq defined $assign
Truth table100%3/3
A and B
A= defined $eqB= defined $assign
ABresult
0X0
100
111
72 unless defined $eq && defined $assign;
73 }
74 else {
75 23 ▶ croak "illegal expression"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eq and defined $assign or not defined $eq || defined $assign 4 19 23
Condition100%9/9
defined $eq and defined $assign or not defined $eq || defined $assign
l!l&&r!l&&!r
5144
defined $eq and defined $assign
!ll&&!rl&&r
1625
defined $eq || defined $assign
l!l&&r!l&&!r
2214
MC/DC100%4/4
defined $eq and defined $assign or not defined $eq || defined $assign
defined $eq defined $assign defined $eq defined $assign
Truth table57%4/7
A and B or not(C || D)
A= defined $eqB= defined $assignC= defined $eqD= defined $assign
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
76 unless (defined $eq && defined $assign) or (!defined $eq && !defined $assign);
77 }
78  
79 22 return $match;
80 }
81  
82 sub _render_declaration {
83 33 ▶ my $args = shift;
Subroutine _render_declaration: called CC=4 SCAR=13.9
84 33 my @lines;
85 33 push @lines => _lines_declaration($args);
86 33 ▶ push @lines => _lines_type_check($args) if $args->{level} >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"level"} >= 1 31 2 33
87 33 ▶ push @lines => _lines_type_tie($args) if $args->{level} == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"level"} == 2 27 6 33
88 33 ▶ push @lines => _lines_data_lock($args) if $args->{declaration} eq 'const';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"declaration"} eq "const" 4 29 33
89 33 return join ";", @lines;
90 }
91  
92 sub _lines_declaration {
93 33 ▶ my $args = shift;
Subroutine _lines_declaration: called CC=4 SCAR=13.9
94 33 my $s = $args->{perl_declaration};
95 33 $s .= do {
96 33 my $s = join ', ', map { $_->{var} } @{$args->{type_vars}};
97 33 ▶ $args->{is_list_context} ? " ($s)" : " $s";
Branch: 1 branch
BranchTrueFalseTotal
$args->{'is_list_context'} ? : 6 27 33
98 };
99 33 ▶ $s .= $args->{attributes} if $args->{attributes};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"attributes"} 3 30 33
100 33 ▶ $s .= " = @{[$args->{assign}]}" if defined $args->{assign};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"assign"} 12 21 33
101 33 return ($s);
102 }
103  
104 sub _lines_type_tie {
105 27 ▶ my $args = shift;
Subroutine _lines_type_tie: called CC=3 SCAR=11.0
106 27 my @lines;
107 27 for (@{$args->{type_vars}}) {
108 30 my ($type, $var) = ($_->{type}, $_->{var});
109 30 ▶ next unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 23 7 30
110 7 push @lines => sprintf('Variable::Declaration::type_tie(%s, %s, %s)', $var, $type, $var);
111 }
112 27 return @lines;
113 }
114  
115 sub _lines_type_check {
116 31 ▶ my $args = shift;
Subroutine _lines_type_check: called CC=3 SCAR=11.0
117 31 my @lines;
118 31 for (@{$args->{type_vars}}) {
119 34 my ($type, $var) = ($_->{type}, $_->{var});
120 34 ▶ next unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 24 10 34
121 10 push @lines => sprintf('Variable::Declaration::croak(%s->get_message(%s)) unless %s->check(%s)', $type, $var, $type, $var)
122 }
123 31 return @lines;
124 }
125  
126 sub _lines_data_lock {
127 4 ▶ my $args = shift;
Subroutine _lines_data_lock: called CC=2 SCAR=6.9
128 4 my @lines;
129 4 for my $type_var (@{$args->{type_vars}}) {
130 4 push @lines => "Variable::Declaration::data_lock($type_var->{var})";
131 }
132 4 return @lines;
133 }
134  
135 sub _parse {
136 76 ▶ my $src = shift;
Subroutine _parse: called CC=2 SCAR=6.9
137  
138 76 ▶ return unless $src =~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $src =~ / \A (?<statement> (?&PerlOWS) (?<assign_to> (?<type_varlist> (?&PerlIdentifier)? (?&PerlOWS) (?&PerlVariable) | (?&PerlParenthesesList) ) (?&PerlOWS) (?<attributes>(?&PerlAttributes))? (?&PerlOWS) ) (?<eq>=)? (?&PerlOWS) (?<assign>(?&PerlConditionalExpression))? ) $PPR::GRAMMAR /ux 9 67 76
139 \A
140 (?<statement>
141 (?&PerlOWS)
142 (?<assign_to>
143 (?<type_varlist>
144 (?&PerlIdentifier)? (?&PerlOWS)
145 (?&PerlVariable)
146 | (?&PerlParenthesesList)
147 ) (?&PerlOWS)
148 (?<attributes>(?&PerlAttributes))? (?&PerlOWS)
149 )
150 (?<eq>=)? (?&PerlOWS)
151 (?<assign>(?&PerlConditionalExpression))?
152 ) $PPR::GRAMMAR }x;
153  
154 return +{
155 statement => $+{statement},
156 type_varlist => $+{type_varlist},
157 assign_to => $+{assign_to},
158 eq => $+{eq},
159 assign => $+{assign},
160 attributes => $+{attributes},
161 }
162 67 }
163  
164 sub _parse_type_varlist {
165 34 ▶ my $expression = shift;
Subroutine _parse_type_varlist: called CC=3 SCAR=11.0
166  
167 34 ▶ if ($expression =~ m{ (?<list>(?&PerlParenthesesList)) $PPR::GRAMMAR }x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expression =~ / (?<list>(?&PerlParenthesesList)) $PPR::GRAMMAR /ux) { } 9 25 34
elsif (my $type_var = _parse_type_var($expression)) { } 22 3 25
168 9 my ($type_vars) = $+{list} =~ m/\A\((.+)\)\Z/;
169 9 my @list = split ',', $type_vars;
170 return +{
171 is_list_context => 1,
172 9 type_vars => [ map { _parse_type_var($_) } @list ],
173 }
174 }
175 elsif (my $type_var = _parse_type_var($expression)) {
176 return +{
177 22 is_list_context => 0,
178 type_vars => [ $type_var ],
179 }
180 }
181 else {
182 3 return;
183 }
184 }
185  
186 sub _parse_type_var {
187 68 ▶ my $expression = shift;
Subroutine _parse_type_var: called CC=2 SCAR=6.9
188  
189 68 ▶ return unless $expression =~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $expression =~ / \A (?&PerlOWS) (?<type>(?&PerlIdentifier))? (?&PerlOWS) (?<var>(?:(?&PerlVariable))) \Z $PPR::GRAMMAR /ux 14 54 68
190 \A
191 (?&PerlOWS)
192 (?<type>(?&PerlIdentifier))? (?&PerlOWS)
193 (?<var>(?:(?&PerlVariable)))
194 \Z
195 $PPR::GRAMMAR
196 }x;
197  
198 return +{
199 type => $+{type},
200 var => $+{var},
201 }
202 54 }
203  
204 11 1;
205 __END__
↑ Summary Build »