Free/Compiler.pm

filter: Statementstmt 88.6% 70 / 79 Branchbran 52.9% 18 / 34 Conditioncond 83.3% 15 / 18 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 79.6% 125 / 157 CC 25 SCAR 23.2
CC
25
Cov
79%
component_content_call_end
21.1
component_call
7.4
perl_line
4.0
CRAP 10.2
« Free/Request.pm ↑ Summary Free/Resolver.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package HTML::MasonX::Free::Compiler;
4 4 $HTML::MasonX::Free::Compiler::VERSION = '0.006';
5 # ABSTRACT: an HTML::Mason compiler that can reject more input
6 4 ▶use parent 'HTML::Mason::Compiler::ToObject';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 OVERVIEW
9 #pod
10 #pod This is an alternate compiler for HTML::Mason. It's meant to fill in for the
11 #pod default, L<HTML::Mason::Compiler::ToObject>. (Don't trust things telling you
12 #pod that the default is HTML::Mason::Compiler. If you're using Mason, you're
13 #pod almost certainly have ToObject doing the work.)
14 #pod
15 #pod By default, it I<should> behave just like the normal compiler, but more options
16 #pod can be provided to make it stricter.
17 #pod
18 #pod Right now, there's just one extra option, but there will be more.
19 #pod
20 #pod =attr allow_stray_content
21 #pod
22 #pod If false, any text outside of a block (like a method or doc block), other than
23 #pod blank lines, will be fatal. Similar, any Perl lines other than comments will
24 #pod be fatal.
25 #pod
26 #pod =attr default_method_to_call
27 #pod
28 #pod If set, this is the name of a method that will be dropped in place whenever the
29 #pod user is trying to call a component without a method. For example, if you set
30 #pod it to "main" then this:
31 #pod
32 #pod <& /foo/bar &>
33 #pod
34 #pod ...will be treated like this:
35 #pod
36 #pod <& /foo/bar:main &>
37 #pod
38 #pod To keep this consistent with the top-level called performed by the mason
39 #pod interpreter, you should probably also use L<HTML::MasonX::Free::Component> as
40 #pod your component class.
41 #pod
42 #pod =cut
43  
44 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 ▶use HTML::Mason::Exceptions(abbr => [qw(param_error)]);
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 4 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 ▶Params::Validate::validation_options(on_fail => sub {param_error join '', @_});
Subroutine __ANON__: not called CC=1 SCAR=1.2
50  
51 BEGIN {
52 4 ▶ __PACKAGE__->valid_params(
Subroutine BEGIN: called CC=1 SCAR=0.0
53 allow_stray_content => {
54 parse => 'boolean',
55 type => SCALAR,
56 default => 1,
57 descr => "Whether to allow content outside blocks, or die",
58 },
59 default_method_to_call => {
60 parse => 'string',
61 type => SCALAR,
62 optional => 1,
63 descr => "A method to always call instead of calling a comp directly",
64 },
65 );
66 }
67  
68 sub text {
69 45 ▶ my ($self, %arg) = @_;
Subroutine text: called CC=4 SCAR=13.9
Pod documented
70 45 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"} and $arg{"text"} =~ /\S/) 1 44 45
Condition100%6/6
$self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"} and $arg{"text"} =~ /\S/
!ll&&!rl&&r
3771
$self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"}
!ll&&!rl&&r
3438
MC/DC100%3/3
$self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"} and $arg{"text"} =~ /\S/
$self->{"current_compile"}{"in_main"} not $self->{"allow_stray_content"} $arg{"text"} =~ /\S/
Truth table100%4/4
A and not(B) and C
A= $self->{"current_compile"}{"in_main"}B= not $self->{"allow_stray_content"}C= $arg{"text"} =~ /\S/
ABCresult
0XX0
10X0
1100
1111
71 $self->{current_compile}{in_main}
72 and ! $self->{allow_stray_content}
73 and $arg{text} =~ /\S/
74 ) {
75 1 $self->lexer->throw_syntax_error(
76 "text outside of block: <<'END_TEXT'\n$arg{text}END_TEXT"
77 );
78 }
79 44 $self->SUPER::text(%arg);
80 }
81  
82 sub perl_line {
83 4 ▶ my ($self, %arg) = @_;
Subroutine perl_line: called CC=4 SCAR=13.9
Pod documented
84  
85 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"} and not $arg{"line"} =~ /\A\s*#/) 1 3 4
Condition83%5/6
$self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"} and not $arg{"line"} =~ /\A\s*#/
!ll&&!rl&&r
211
$self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"}
!ll&&!rl&&r
022
MC/DC66%2/3
$self->{"current_compile"}{"in_main"} and not $self->{"allow_stray_content"} and not $arg{"line"} =~ /\A\s*#/
$self->{"current_compile"}{"in_main"} not $self->{"allow_stray_content"} not $arg{"line"} =~ /\A\s*#/
Truth table75%3/4
A and not(B) and not(C)
A= $self->{"current_compile"}{"in_main"}B= not $self->{"allow_stray_content"}C= not $arg{"line"} =~ /\A\s*#/
ABCresult
0XX0
10X0
1100
1111
86 $self->{current_compile}{in_main}
87 and ! $self->{allow_stray_content}
88 and $arg{line} !~ /\A\s*#/
89 ) {
90 1 $self->lexer->throw_syntax_error(
91 "perl outside of block: $arg{line}\n"
92 );
93 }
94 3 $self->SUPER::perl_line(%arg);
95 }
96  
97 # BEGIN DIRECT THEFT FROM HTML-Mason 1.50
98 sub component_call
99 {
100 11 ▶ my $self = shift;
Subroutine component_call: called CC=7 SCAR=20.1
Pod documented
101 11 my %p = @_;
102  
103 11 my ($prespace, $call, $postspace) = ($p{call} =~ /(\s*)(.*)(\s*)/s);
104 11 ▶ if ( $call =~ m,^[\w/.],)
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ m[^[\w/.]]) 11 0 11
105 {
106 11 my $comma = index($call, ',');
107 11 ▶ $comma = length $call if $comma == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $comma == -1 11 0 11
108 11 (my $comp = substr($call, 0, $comma)) =~ s/\s+$//;
109 11 ▶ if (defined $self->{default_method_to_call} and $comp !~ /:/) { ##
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"default_method_to_call"} and not $comp =~ /:/) 2 9 11
Condition66%2/3
defined $self->{"default_method_to_call"} and not $comp =~ /:/
!ll&&!rl&&r
092
MC/DC50%1/2
defined $self->{"default_method_to_call"} and not $comp =~ /:/
defined $self->{"default_method_to_call"} not $comp =~ /:/
Truth table66%2/3
A and not(B)
A= defined $self->{"default_method_to_call"}B= not $comp =~ /:/
ABresult
0X0
100
111
110 2 $comp = "$comp:$self->{default_method_to_call}"; ##
111 } ##
112 11 $call = "'$comp'" . substr($call, $comma);
113 }
114 11 my $code = "\$m->comp( $prespace $call $postspace \n); ";
115 11 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 0 11 11
116 11 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
117  
118 11 $self->_add_body_code($code);
119  
120 11 $self->{current_compile}{last_body_code_type} = 'component_call';
121 }
122  
123 sub component_content_call_end
124 {
125 2 ▶ my $self = shift;
Subroutine component_content_call_end: called CC=13 SCAR=30.5
Pod documented
126 2 my $c = $self->{current_compile};
127 2 my %p = @_;
128  
129 $self->lexer->throw_syntax_error("Found component with content ending tag but no beginning tag")
130 2 ▶ unless @{ $c->{comp_with_content_stack} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$c->{"comp_with_content_stack"};} 0 2 2
131  
132 2 my $call = pop @{ $c->{comp_with_content_stack} };
133 2 my $call_end = $p{call_end};
134 2 for ($call_end) { s/^\s+//; s/\s+$//; }
135  
136 2 my $comp = undef;
137 2 ▶ if ( $call =~ m,^[\w/.],)
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ m[^[\w/.]]) 2 0 2
138 {
139 2 my $comma = index($call, ',');
140 2 ▶ $comma = length $call if $comma == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $comma == -1 2 0 2
141 2 ($comp = substr($call, 0, $comma)) =~ s/\s+$//;
142 2 ▶ if (defined $self->{default_method_to_call} and $comp !~ /:/) { ##
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"default_method_to_call"} and not $comp =~ /:/) 1 1 2
Condition66%2/3
defined $self->{"default_method_to_call"} and not $comp =~ /:/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $self->{"default_method_to_call"} and not $comp =~ /:/
defined $self->{"default_method_to_call"} not $comp =~ /:/
Truth table66%2/3
A and not(B)
A= defined $self->{"default_method_to_call"}B= not $comp =~ /:/
ABresult
0X0
100
111
143 1 $comp = "$comp:$self->{default_method_to_call}"; ##
144 } ##
145 2 $call = "'$comp'" . substr($call, $comma);
146 }
147 2 ▶ if ($call_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_end) 0 2 2
148 0 ▶ if ($call_end !~ m,^[\w/.],) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($call_end =~ m[^[\w/.]]) 0 0 0
149 0 $self->lexer->throw_syntax_error("Cannot use an expression inside component with content ending tag; use a bare component name or </&> instead");
150 }
151 0 ▶ if (!defined($comp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $comp) 0 0 0
152 0 $self->lexer->throw_syntax_error("Cannot match an expression as a component name; use </&> instead");
153 }
154 0 ▶ if ($call_end ne $comp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_end ne $comp) 0 0 0
155 0 $self->lexer->throw_syntax_error("Component name in ending tag ($call_end) does not match component name in beginning tag ($comp)");
156 }
157 }
158  
159 2 my $code = "} }, $call\n );\n";
160  
161 2 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 0 2 2
162 2 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
163  
164 2 $self->_add_body_code($code);
165  
166 2 $c->{last_body_code_type} = 'component_content_call_end';
167 }
168 # END DIRECT THEFT FROM HTML-Mason 1.50
169  
170 4 1;
171  
172 __END__
173  
174 =pod
175  
176 =encoding UTF-8
177  
178 =head1 NAME
179  
180 HTML::MasonX::Free::Compiler - an HTML::Mason compiler that can reject more input
181  
182 =head1 VERSION
183  
184 version 0.006
185  
186 =head1 OVERVIEW
187  
188 This is an alternate compiler for HTML::Mason. It's meant to fill in for the
189 default, L<HTML::Mason::Compiler::ToObject>. (Don't trust things telling you
190 that the default is HTML::Mason::Compiler. If you're using Mason, you're
191 almost certainly have ToObject doing the work.)
192  
193 By default, it I<should> behave just like the normal compiler, but more options
194 can be provided to make it stricter.
195  
196 Right now, there's just one extra option, but there will be more.
197  
198 =head1 ATTRIBUTES
199  
200 =head2 allow_stray_content
201  
202 If false, any text outside of a block (like a method or doc block), other than
203 blank lines, will be fatal. Similar, any Perl lines other than comments will
204 be fatal.
205  
206 =head2 default_method_to_call
207  
208 If set, this is the name of a method that will be dropped in place whenever the
209 user is trying to call a component without a method. For example, if you set
210 it to "main" then this:
211  
212 <& /foo/bar &>
213  
214 ...will be treated like this:
215  
216 <& /foo/bar:main &>
217  
218 To keep this consistent with the top-level called performed by the mason
219 interpreter, you should probably also use L<HTML::MasonX::Free::Component> as
220 your component class.
221  
222 =head1 AUTHOR
223  
224 Ricardo Signes <rjbs@cpan.org>
225  
226 =head1 COPYRIGHT AND LICENSE
227  
228 This software is copyright (c) 2016 by Ricardo Signes.
229  
230 This is free software; you can redistribute it and/or modify it under
231 the same terms as the Perl 5 programming language system itself.
232  
233 =cut
« Free/Request.pm ↑ Summary Free/Resolver.pm »