lib/Template/EmbeddedPerl/Arguments.pm

filter: Statementstmt 99.2% 138 / 139 Branchbran 76.0% 76 / 100 Conditioncond 55.5% 30 / 54 MC/DCmcdc 34.3% 11 / 32 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 78.9% 266 / 337 CC 73 SCAR 27.5
CC
73
Cov
83%
_is_wholly_consumed
23.8
_generate_bindings
19.9
rewrite
19.0
CRAP 15.6
↑ Summary lib/Template/EmbeddedPerl/SafeString.pm »
1 package Template::EmbeddedPerl::Arguments;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 my %RESERVED_ARGUMENT = map { $_ => 1 } qw(__named_args __context _O self);
9  
10 366 ▶sub rewrite {
Subroutine rewrite: called CC=18 SCAR=29.5
Pod undocumented
11 366 my ($class, $template, %args) = @_;
12 366 ▶ my $comment_mark = exists $args{comment_mark} ? $args{comment_mark} : '#';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'comment_mark'} ? : 364 2 366
13 366 ▶ my $line_start = exists $args{line_start} ? $args{line_start} : '%';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'line_start'} ? : 364 2 366
14 366 ▶ my $open_tag = exists $args{open_tag} ? $args{open_tag} : '<%';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'open_tag'} ? : 364 2 366
15 366 ▶ my $close_tag = exists $args{close_tag} ? $args{close_tag} : '%>';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'close_tag'} ? : 364 2 366
16 366 my @lines = $template =~ /.*?(?:\n|\z)/g;
17 366 ▶ pop @lines if @lines && $lines[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @lines and $lines[-1] eq "" 366 0 366
Condition33%1/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
00366
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table33%1/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
18  
19 my @directive_lines = grep {
20 366 $lines[$_] =~ /^[ \t]*\Q$line_start\E[ \t]+args(?:[ \t]|\n|\z)/
21 } 0 .. $#lines;
22 366 ▶ return ($template, 0) unless @directive_lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @directive_lines 299 67 366
23  
24 67 my $start = $directive_lines[0];
25 67 for my $line_number (0 .. $start - 1) {
26 9 ▶ next if $lines[$line_number] =~ /^[ \t]*(?:\n|\z)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$line_number] =~ /^[ \t]*(?:\n|\z)$/ 2 7 9
27 7 ▶ next if defined($comment_mark) && length($comment_mark)
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment_mark and length $comment_mark and $lines[$line_number] =~ /^[ \t]*\Q$comment_mark\E[^\n]*(?:\n|\z)$/ 4 3 7
Condition50%3/6
defined $comment_mark and length $comment_mark and $lines[$line_number] =~ /^[ \t]*\Q$comment_mark\E[^\n]*(?:\n|\z)$/
!ll&&!rl&&r
034
defined $comment_mark and length $comment_mark
!ll&&!rl&&r
007
MC/DC33%1/3
defined $comment_mark and length $comment_mark and $lines[$line_number] =~ /^[ \t]*\Q$comment_mark\E[^\n]*(?:\n|\z)$/
defined $comment_mark length $comment_mark $lines[$line_number] =~ /^[ \t]*\Q$comment_mark\E[^\n]*(?:\n|\z)$/
Truth table50%2/4
A and B and C
A= defined $comment_markB= length $comment_markC= $lines[$line_number] =~ /^[ \t]*\Q$comment_mark\E[^\n]*(?:\n|\z)$/
ABCresult
0XX0
10X0
1100
1111
28 && $lines[$line_number] =~ /^[ \t]*\Q$comment_mark\E[^\n]*(?:\n|\z)$/;
29 3 _error('args must be the first executable directive', $start + 1);
30 }
31  
32 64 my ($declaration, $end) = _collect_declaration(\@lines, $start, $line_start);
33 62 for my $line_number ($end + 1 .. $#lines) {
34 170 ▶ next unless $lines[$line_number]
Branch: 1 branch
BranchTrueFalseTotal
unless $lines[$line_number] =~ /^[ \t]*\Q$line_start\E[ \t]+args(?:[ \t]|\n|\z)/ 169 1 170
35 =~ /^[ \t]*\Q$line_start\E[ \t]+args(?:[ \t]|\n|\z)/;
36 1 _error('args directive may only appear once', $line_number + 1);
37 }
38  
39 61 my $expected_newlines = $declaration =~ tr/\n//;
40 61 ▶ my $line_boundary = $declaration =~ s/\n\z// ? "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$declaration =~ s/\n\z// ? : 61 0 61
41 61 my $generated = _generate_bindings($declaration, $start + 1);
42 51 my $actual_newlines = $generated =~ tr/\n//;
43 51 ▶ my $generated_newlines = $expected_newlines - ($line_boundary eq "\n" ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$line_boundary eq "\n" ? : 51 0 51
44 51 ▶ _error('args rewrite could not preserve source lines', $start + 1)
Branch: 1 branch
BranchTrueFalseTotal
if $actual_newlines > $generated_newlines 0 51 51
45 if $actual_newlines > $generated_newlines;
46 51 $generated .= "\n" x ($generated_newlines - $actual_newlines);
47  
48 51 splice @lines, $start, $end - $start + 1,
49 "${open_tag}${generated}-${close_tag}${line_boundary}";
50 51 return (join('', @lines), 1);
51 }
52  
53 64 ▶sub _collect_declaration {
Subroutine _collect_declaration: called CC=5 SCAR=16.1
54 64 my ($lines, $start, $line_start) = @_;
55 64 my $declaration = $lines->[$start];
56 64 $declaration =~ s/^[ \t]*\Q$line_start\E[ \t]+args\b//;
57  
58 64 my $end = $start;
59 64 ▶ while (!_is_complete($declaration)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_complete($declaration)) 18 62 80
60 18 $end++;
61 18 ▶ if ($end > $#$lines
Branch: 1 branch
BranchTrueFalseTotal
if ($end > $lines->$#* or not $lines->[$end] =~ /^[ \t]*\Q$line_start\E(?!\Q$line_start\E)/) 2 16 18
Condition66%2/3
$end > $lines->$#* or not $lines->[$end] =~ /^[ \t]*\Q$line_start\E(?!\Q$line_start\E)/
l!l&&r!l&&!r
2016
MC/DC50%1/2
$end > $lines->$#* or not $lines->[$end] =~ /^[ \t]*\Q$line_start\E(?!\Q$line_start\E)/
$end > $lines->$#* not $lines->[$end] =~ /^[ \t]*\Q$line_start\E(?!\Q$line_start\E)/
Truth table66%2/3
A or not(B)
A= $end > $lines->$#*B= not $lines->[$end] =~ /^[ \t]*\Q$line_start\E(?!\Q$line_start\E)/
ABresult
1X1
011
000
62 || $lines->[$end] !~ /^[ \t]*\Q$line_start\E(?!\Q$line_start\E)/) {
63 2 ▶ my $message = $declaration =~ /=\s*(?:\n)?\z/
Branch: 1 branch
BranchTrueFalseTotal
$declaration =~ /=\s*(?:\n)?\z/ ? : 1 1 2
64 ? 'args default expression is incomplete'
65 : 'incomplete args directive';
66 2 _error($message, $start + 1);
67 }
68  
69 16 my $continuation = $lines->[$end];
70 16 $continuation =~ s/^[ \t]*\Q$line_start\E//;
71 16 $declaration .= $continuation;
72 }
73  
74 62 return ($declaration, $end);
75 }
76  
77 80 ▶sub _is_complete {
Subroutine _is_complete: called CC=9 SCAR=22.3
78 80 my ($declaration) = @_;
79 80 my ($document, $list, $expression) = _parse_declaration($declaration);
80 80 ▶ return 0 unless $document && $list && $expression;
Branch: 1 branch
BranchTrueFalseTotal
unless $document and $list and $expression 1 79 80
Condition66%4/6
$document and $list and $expression
!ll&&!rl&&r
1079
$document and $list
!ll&&!rl&&r
1079
MC/DC33%1/3
$document and $list and $expression
$document $list $expression
Truth table50%2/4
A and B and C
A= $documentB= $listC= $expression
ABCresult
0XX0
10X0
1100
1111
81  
82 79 my $structures = $document->find('PPI::Structure');
83 79 ▶ return 0 if $structures && grep { !$_->finish } @$structures;
Branch: 1 branch
BranchTrueFalseTotal
if $structures and grep {not $_->finish;} @$structures 14 65 79
Condition66%2/3
$structures and grep {not $_->finish;} @$structures
!ll&&!rl&&r
06514
MC/DC50%1/2
$structures and grep {not $_->finish;} @$structures
$structures grep {not $_->finish;} @$structures
Truth table66%2/3
A and B
A= $structuresB= grep {not $_->finish;} @$structures
ABresult
0X0
100
111
84 65 ▶ return 0 if $document->find('PPI::Statement::UnmatchedBrace');
Branch: 1 branch
BranchTrueFalseTotal
if $document->find("PPI::Statement::UnmatchedBrace") 0 65 65
85  
86 65 my @tokens = grep { $_->significant } $expression->tokens;
87 65 ▶ return 0 unless @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 65 65
88 65 ▶ return 0 if $tokens[-1]->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
if $tokens[-1]->isa("PPI::Token::Operator") 3 62 65
89 62 return 1;
90 }
91  
92 61 ▶sub _generate_bindings {
Subroutine _generate_bindings: called CC=19 SCAR=29.9
93 61 my ($declaration, $line) = @_;
94 61 my ($document, $list, $expression) = _parse_declaration($declaration);
95 61 ▶ _error('invalid args directive', $line)
Branch: 1 branch
BranchTrueFalseTotal
unless $document and $list and $expression and _is_wholly_consumed($document, $list, $expression) 3 58 61
Condition44%4/9
$document and $list and $expression and _is_wholly_consumed($document, $list, $expression)
!ll&&!rl&&r
0358
$document and $list and $expression
!ll&&!rl&&r
0061
$document and $list
!ll&&!rl&&r
0061
MC/DC25%1/4
$document and $list and $expression and _is_wholly_consumed($document, $list, $expression)
$document $list $expression _is_wholly_consumed($document, $list, $expression)
Truth table40%2/5
A and B and C and D
A= $documentB= $listC= $expressionD= _is_wholly_consumed($document, $list, $expression)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
96 unless $document
97 && $list
98 && $expression
99 && _is_wholly_consumed($document, $list, $expression);
100  
101 58 my @parts;
102 my @part;
103 58 for my $element ($expression->children) {
104 272 ▶ if ($element->isa('PPI::Token::Operator') && $element->content eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->isa("PPI::Token::Operator") and $element->content eq ",") 24 248 272
Condition100%3/3
$element->isa("PPI::Token::Operator") and $element->content eq ","
!ll&&!rl&&r
2163224
MC/DC100%2/2
$element->isa("PPI::Token::Operator") and $element->content eq ","
$element->isa("PPI::Token::Operator") $element->content eq ","
Truth table100%3/3
A and B
A= $element->isa("PPI::Token::Operator")B= $element->content eq ","
ABresult
0X0
100
111
105 24 push @parts, [@part];
106 24 @part = ();
107 24 next;
108 }
109 248 push @part, $element;
110 }
111 58 push @parts, [@part];
112  
113 58 my %seen;
114 58 my $generated = 'my $__named_args=$__context->named_arguments(\@_);';
115 58 for my $part (@parts) {
116 82 my @significant = grep { $_->significant } @$part;
117 82 ▶ _error('args directive requires a scalar argument', $line) unless @significant;
Branch: 1 branch
BranchTrueFalseTotal
unless @significant 0 82 82
118  
119 82 my $symbol = shift @significant;
120 82 ▶ _error('args directive accepts only scalar arguments', $line)
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol->isa("PPI::Token::Symbol") and $symbol->content =~ /^\$([A-Za-z_]\w*)\z/ 2 80 82
Condition66%2/3
$symbol->isa("PPI::Token::Symbol") and $symbol->content =~ /^\$([A-Za-z_]\w*)\z/
!ll&&!rl&&r
0280
MC/DC50%1/2
$symbol->isa("PPI::Token::Symbol") and $symbol->content =~ /^\$([A-Za-z_]\w*)\z/
$symbol->isa("PPI::Token::Symbol") $symbol->content =~ /^\$([A-Za-z_]\w*)\z/
Truth table66%2/3
A and B
A= $symbol->isa("PPI::Token::Symbol")B= $symbol->content =~ /^\$([A-Za-z_]\w*)\z/
ABresult
0X0
100
111
121 unless $symbol->isa('PPI::Token::Symbol')
122 && $symbol->content =~ /^\$([A-Za-z_]\w*)\z/;
123 80 my $name = $1;
124 _error("Template argument '$name' uses a reserved compiler identifier", $line)
125 80 ▶ if $RESERVED_ARGUMENT{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $RESERVED_ARGUMENT{$name} 4 76 80
126 76 ▶ _error("Duplicate args declaration '$name'", $line) if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 1 75 76
127  
128 75 my ($symbol_index) = grep { $part->[$_] == $symbol } 0 .. $#$part;
129 75 my $leading = join '', map { $_->content } @$part[0 .. $symbol_index - 1];
130 75 $generated .= $leading;
131  
132 75 ▶ if (!@significant) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@significant) 43 32 75
133 43 $generated .= "my \$$name=\$__context->take_required_argument(\$__named_args,'$name');";
134 43 next;
135 }
136  
137 32 my $equals = shift @significant;
138 32 ▶ _error("invalid declaration for template argument '$name'", $line)
Branch: 1 branch
BranchTrueFalseTotal
unless $equals->isa("PPI::Token::Operator") and $equals->content eq "=" 0 32 32
Condition33%1/3
$equals->isa("PPI::Token::Operator") and $equals->content eq "="
!ll&&!rl&&r
0032
MC/DC0%0/2
$equals->isa("PPI::Token::Operator") and $equals->content eq "="
$equals->isa("PPI::Token::Operator") $equals->content eq "="
Truth table33%1/3
A and B
A= $equals->isa("PPI::Token::Operator")B= $equals->content eq "="
ABresult
0X0
100
111
139 unless $equals->isa('PPI::Token::Operator') && $equals->content eq '=';
140 32 ▶ _error("template argument '$name' requires a default expression", $line)
Branch: 1 branch
BranchTrueFalseTotal
unless @significant 0 32 32
141 unless @significant;
142  
143 32 my ($equals_index) = grep { $part->[$_] == $equals } 0 .. $#$part;
144 32 my @default_elements = @$part[$equals_index + 1 .. $#$part];
145 32 my $default = join '', map { $_->content } @default_elements;
146 32 my @default_significant = grep { $_->significant } @default_elements;
147 32 ▶ my $factory = _is_anonymous_sub(@default_significant)
Branch: 1 branch
BranchTrueFalseTotal
_is_anonymous_sub(@default_significant) ? : 6 26 32
148 ? $default
149 : "sub {$default}";
150 32 $generated .= "my \$$name=\$__context->take_optional_argument(\$__named_args,'$name',$factory);";
151 }
152  
153 51 $generated .= '$__context->assert_no_arguments($__named_args);';
154 51 return $generated;
155 }
156  
157 32 ▶sub _is_anonymous_sub {
Subroutine _is_anonymous_sub: called CC=7 SCAR=20.5
158 32 my (@elements) = @_;
159 32 ▶ return 0 unless @elements == 2 || @elements == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @elements == 2 or @elements == 3 25 7 32
Condition66%2/3
@elements == 2 or @elements == 3
l!l&&r!l&&!r
7025
MC/DC50%1/2
@elements == 2 or @elements == 3
@elements == 2 @elements == 3
Truth table66%2/3
A or B
A= @elements == 2B= @elements == 3
ABresult
1X1
011
000
160 7 ▶ return 0 unless $elements[0]->isa('PPI::Token::Word') && $elements[0]->content eq 'sub';
Branch: 1 branch
BranchTrueFalseTotal
unless $elements[0]->isa("PPI::Token::Word") and $elements[0]->content eq "sub" 1 6 7
Condition66%2/3
$elements[0]->isa("PPI::Token::Word") and $elements[0]->content eq "sub"
!ll&&!rl&&r
016
MC/DC50%1/2
$elements[0]->isa("PPI::Token::Word") and $elements[0]->content eq "sub"
$elements[0]->isa("PPI::Token::Word") $elements[0]->content eq "sub"
Truth table66%2/3
A and B
A= $elements[0]->isa("PPI::Token::Word")B= $elements[0]->content eq "sub"
ABresult
0X0
100
111
161 6 ▶ return 0 unless $elements[-1]->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $elements[-1]->isa("PPI::Structure::Block") 0 6 6
162 6 ▶ return 1 if @elements == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @elements == 2 6 0 6
163 0 return $elements[1]->isa('PPI::Structure::List');
164 }
165  
166 61 ▶sub _is_wholly_consumed {
Subroutine _is_wholly_consumed: called CC=16 SCAR=31.7
167 61 my ($document, $list, $expression) = @_;
168 61 ▶ my $statement = $document->schild(0) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $statement = $document->schild(0) or return 0 61 0 61
169 61 ▶ return 0 unless $statement->isa('PPI::Statement::Variable');
Branch: 1 branch
BranchTrueFalseTotal
unless $statement->isa("PPI::Statement::Variable") 0 61 61
170 61 ▶ return 0 if $document->schild(1);
Branch: 1 branch
BranchTrueFalseTotal
if $document->schild(1) 0 61 61
171  
172 61 ▶ my $keyword = $statement->schild(0) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $keyword = $statement->schild(0) or return 0 61 0 61
173 61 ▶ return 0 unless $keyword->isa('PPI::Token::Word') && $keyword->content eq 'my';
Branch: 1 branch
BranchTrueFalseTotal
unless $keyword->isa("PPI::Token::Word") and $keyword->content eq "my" 0 61 61
Condition33%1/3
$keyword->isa("PPI::Token::Word") and $keyword->content eq "my"
!ll&&!rl&&r
0061
MC/DC0%0/2
$keyword->isa("PPI::Token::Word") and $keyword->content eq "my"
$keyword->isa("PPI::Token::Word") $keyword->content eq "my"
Truth table33%1/3
A and B
A= $keyword->isa("PPI::Token::Word")B= $keyword->content eq "my"
ABresult
0X0
100
111
174 61 ▶ return 0 unless $statement->schild(1) == $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $statement->schild(1) == $list 0 61 61
175  
176 61 ▶ my $terminator = $statement->schild(2) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $terminator = $statement->schild(2) or return 0 61 0 61
177 61 ▶ return 0 unless $terminator->isa('PPI::Token::Structure')
Branch: 1 branch
BranchTrueFalseTotal
unless $terminator->isa("PPI::Token::Structure") and $terminator->content eq ";" 0 61 61
Condition33%1/3
$terminator->isa("PPI::Token::Structure") and $terminator->content eq ";"
!ll&&!rl&&r
0061
MC/DC0%0/2
$terminator->isa("PPI::Token::Structure") and $terminator->content eq ";"
$terminator->isa("PPI::Token::Structure") $terminator->content eq ";"
Truth table33%1/3
A and B
A= $terminator->isa("PPI::Token::Structure")B= $terminator->content eq ";"
ABresult
0X0
100
111
178 && $terminator->content eq ';';
179 61 ▶ return 0 if $statement->schild(3);
Branch: 1 branch
BranchTrueFalseTotal
if $statement->schild(3) 0 61 61
180  
181 61 ▶ return 0 unless $list->schild(0) == $expression;
Branch: 1 branch
BranchTrueFalseTotal
unless $list->schild(0) == $expression 0 61 61
182 61 ▶ return 0 if $list->schild(1);
Branch: 1 branch
BranchTrueFalseTotal
if $list->schild(1) 2 59 61
183 return 0 if grep {
184 59 ▶ $_->isa('PPI::Token::Structure') && $_->content eq ';'
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->isa("PPI::Token::Structure") and $_->content eq ";";} $expression->children 1 58 59
Condition66%2/3
$_->isa('PPI::Token::Structure') && $_->content eq ';'
!ll&&!rl&&r
27701
MC/DC50%1/2
$_->isa('PPI::Token::Structure') && $_->content eq ';'
$_->isa('PPI::Token::Structure') $_->content eq ';'
Truth table66%2/3
A && B
A= $_->isa('PPI::Token::Structure')B= $_->content eq ';'
ABresult
0X0
100
111
185 } $expression->children;
186 58 return 1;
187 }
188  
189 141 ▶sub _parse_declaration {
Subroutine _parse_declaration: called CC=5 SCAR=16.5
190 141 my ($declaration) = @_;
191 141 my $source = "my ($declaration);";
192 141 ▶ my $document = PPI::Document->new(\$source) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $document = "PPI::Document"->new(\$source) or return 141 0 141
193 141 ▶ my $statement = $document->schild(0) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $statement = $document->schild(0) or return 141 0 141
194 141 ▶ my $list = $statement->find_first('PPI::Structure::List') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $statement->find_first("PPI::Structure::List") or return 141 0 141
195 141 ▶ my $expression = $list->schild(0) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $expression = $list->schild(0) or return 140 1 141
196 140 return ($document, $list, $expression);
197 }
198  
199 16 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
200 16 my ($message, $line) = @_;
201 16 die "$message at template line $line\n";
202 }
203  
204 24 1;
↑ Summary lib/Template/EmbeddedPerl/SafeString.pm »