blib/lib/Variable/Declaration.pm

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