lib/CodeGen/Protection/Role.pm

filter: Statementstmt 89.6% 104 / 116 Branchbran 70.8% 17 / 24 Conditioncond 46.4% 13 / 28 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 1 totaltotal 75.6% 155 / 205 CC 23 SCAR 15.5
CC
23
Cov
80%
_get_version
11.3
_extract_before_and_after
8.0
BUILD
4.6
CRAP 4.7
↑ Summary lib/CodeGen/Protection/Format/Perl.pm »
1 package CodeGen::Protection::Role;
2  
3 # ABSTRACT: Role to help rewrite parts of documents
4  
5 6 ▶use v5.08.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use CodeGen::Protection::Types qw(NonEmptyStr Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 requires qw(
12 _tidy
13 _start_marker_format
14 _end_marker_format
15 VERSION
16 );
17  
18 6 our $VERSION = '0.06';
19  
20 6 has existing_code => (
21 is => 'ro',
22 isa => NonEmptyStr,
23 predicate => 1,
24 );
25  
26 6 has protected_code => (
27 is => 'ro',
28 isa => NonEmptyStr,
29 required => 1,
30 );
31  
32 6 has name => (
33 is => 'ro',
34 isa => NonEmptyStr,
35 default => 'document',
36 );
37  
38 6 has overwrite => (
39 is => 'ro',
40 isa => Bool,
41 default => 0,
42 );
43  
44 6 has rewritten => (
45 is => 'rwp',
46 isa => NonEmptyStr,
47 );
48  
49 6 has tidy => (
50 is => 'ro',
51 isa => NonEmptyStr,
52 );
53  
54 has document_type => (
55 is => 'ro',
56 isa => NonEmptyStr,
57 20 ▶ builder => sub {
Subroutine _build_document_type: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
58 20 my $self = shift;
59 20 my $class = ref $self;
60 20 $class =~ s/^CodeGen::Protection::Format:://;
61 20 return $class;
62 },
63 6 );
64  
65 27 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=15.2
Pod undocumented
66 27 my $self = shift;
67 27 ▶ if ( $self->has_existing_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_existing_code) { } 21 6 27
68 21 $self->_rewrite;
69 }
70 else {
71 6 my $protected_code = $self->protected_code;
72 6 my $regex = $self->_regex_to_match_rewritten_document;
73 6 ▶ if ( !$self->has_existing_code && $protected_code =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_existing_code and $protected_code =~ /$regex/) 0 6 6
Condition33%1/3
not $self->has_existing_code and $protected_code =~ /$regex/
!ll&&!rl&&r
060
MC/DC0%0/2
not $self->has_existing_code and $protected_code =~ /$regex/
not $self->has_existing_code $protected_code =~ /$regex/
Truth table33%1/3
not(A) and B
A= not $self->has_existing_codeB= $protected_code =~ /$regex/
ABresult
0X0
100
111
74 0 my $type = $self->document_type;
75 0 my $name = $self->name;
76 0 croak(
77 "We re in 'Creation' mode, but the $type code passed in already has start/end markers for $name."
78 );
79 }
80 $protected_code
81 6 = $self->_remove_all_leading_and_trailing_blank_lines(
82 $protected_code);
83 6 $self->_set_rewritten( $self->_add_checksums($protected_code) );
84 }
85 }
86  
87 21 ▶sub _rewrite {
Subroutine _rewrite: called CC=2 SCAR=6.9
88 21 my ($self) = @_;
89  
90 21 my $extract_re = $self->_regex_to_match_rewritten_document;
91  
92 21 my $replacement = $self->protected_code;
93 21 ▶ if ( $replacement =~ $extract_re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement =~ /$extract_re/s) 6 15 21
94  
95 # we have a full document with start and end rewrite tags, so let's
96 # just extract that
97 6 $replacement = $self->_extract_body;
98 }
99  
100 21 my $body = $self->_add_checksums($replacement);
101 21 $body = $self->_remove_all_leading_and_trailing_blank_lines($body);
102 21 my ( $before, $after ) = $self->_extract_before_and_after;
103 15 $self->_set_rewritten("$before$body$after");
104 }
105  
106 21 ▶sub _extract_before_and_after {
Subroutine _extract_before_and_after: called CC=8 SCAR=20.8
107 21 my ( $self, $text ) = @_;
108 21 ▶ $text ||= $self->existing_code;
Condition33%1/3
$text ||= $self->existing_code
l!l&&r!l&&!r
0210
MC/DC0%0/2
$text ||= $self->existing_code
$text $self->existing_code
Truth table33%1/3
A ||= B
A= $textB= $self->existing_code
ABresult
1X1
011
000
109  
110 21 my $extract_re = $self->_regex_to_match_rewritten_document;
111 21 my $type = $self->document_type;
112 21 my $name = $self->name;
113 21 ▶ if ( $text !~ $extract_re ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /$extract_re/s) 3 18 21
114 3 croak(
115 "Could not find the $type start and end markers in existing_code for $name."
116 );
117 }
118 18 my $digest_start = $2;
119 18 my $digest_end = $4;
120  
121 18 ▶ unless ( $digest_start eq $digest_end ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($digest_start eq $digest_end) 1 17 18
122 1 croak(
123 "Start digest ($digest_start) does not match end digest ($digest_end) for $type $name"
124 );
125 }
126  
127 17 my $expected = $self->_get_checksum($3);
128 17 ▶ if ( !$self->overwrite && $digest_start ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->overwrite and $digest_start ne $expected) 2 15 17
Condition100%3/3
not $self->overwrite and $digest_start ne $expected
!ll&&!rl&&r
1142
MC/DC100%2/2
not $self->overwrite and $digest_start ne $expected
not $self->overwrite $digest_start ne $expected
Truth table100%3/3
not(A) and B
A= not $self->overwriteB= $digest_start ne $expected
ABresult
0X0
100
111
129 2 croak(
130 "Checksum ($digest_start) did not match expected checksum ($expected). Set 'overwrite' to true to ignore this for $type $name"
131 );
132 }
133 15 ▶ my $before = $1 || '';
Condition100%2/2
$1 || ''
l!l
141
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
134 15 ▶ my $after = $5 || '';
Condition100%2/2
$5 || ''
l!l
141
MC/DC100%1/1
$5 || ''
$5
Truth table100%2/2
A
A= $5
Aresult
11
00
135 15 return ( $before, $after );
136 }
137  
138 6 ▶sub _extract_body {
Subroutine _extract_body: called CC=4 SCAR=15.0
139 6 my ( $self, $text ) = @_;
140 6 ▶ $text ||= $self->protected_code;
Condition33%1/3
$text ||= $self->protected_code
l!l&&r!l&&!r
060
MC/DC0%0/2
$text ||= $self->protected_code
$text $self->protected_code
Truth table33%1/3
A ||= B
A= $textB= $self->protected_code
ABresult
1X1
011
000
141  
142 6 my $extract_re = $self->_regex_to_match_rewritten_document;
143 6 my $name = $self->name;
144 6 my $type = $self->document_type;
145 6 ▶ if ( $text !~ $extract_re ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /$extract_re/) 0 6 6
146 0 croak(
147 "Could not find the $type start and end markers in protected_code for $name"
148 );
149 }
150 6 my $digest_start = $2;
151 6 my $digest_end = $4;
152  
153 6 ▶ unless ( $digest_start eq $digest_end ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($digest_start eq $digest_end) 0 6 6
154 0 croak(
155 "Start digest ($digest_start) does not match end digest ($digest_end) for $type $name"
156 );
157 }
158  
159 6 return $self->_remove_all_leading_and_trailing_blank_lines($3);
160 }
161  
162 #
163 # Internal method. Returns a regex that can use used to match a "rewritten"
164 # document. If the regex matches, we have a rewritten document. You can
165 # extract parts via:
166 #
167 # my $regex = $self->_regex_to_match_rewritten_document;
168 # if ( $document =~ $regex ) {
169 # my $before = $1;
170 # my $digest_start = $2{digest_start}; # checksum from start tag
171 # my $body = $3{body}; # between start and end tags
172 # my $digest_end = $4{digest_end}; # checksum from end tag
173 # my $after = $5{after};
174 # }
175 #
176 # This is not an attribute because we need to be able to call it as a class
177 # method
178 #
179  
180 54 ▶sub _regex_to_match_rewritten_document {
Subroutine _regex_to_match_rewritten_document: called CC=2 SCAR=7.1
181 54 my $self = shift;
182 54 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
5400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
183  
184 54 my $digest_start_re = qr/([0-9a-f]{32})/;
185 54 my $digest_end_re = qr/([0-9a-f]{32})/;
186 54 my $start_marker_re = sprintf $class->_start_marker_format => $class,
187 $class->_version_re,
188 $digest_start_re;
189 54 my $end_marker_re = sprintf $class->_end_marker_format => $class,
190 $class->_version_re,
191 $digest_end_re;
192  
193 # don't use the /x modifier to make this prettier unless you call
194 # quotemeta on the start and end markers
195 return # $1 $2 $3 $4 $5
196 54 qr/^(.*?)$start_marker_re(.*?)$end_marker_re(.*?)$/s;
197 }
198  
199 44 ▶sub _get_checksum {
Subroutine _get_checksum: called CC=1 SCAR=0.0
200 44 my ( $class, $text ) = @_;
201 44 return md5_hex(
202 $class->_remove_all_leading_and_trailing_blank_lines($text) );
203 }
204  
205 27 ▶sub _add_checksums {
Subroutine _add_checksums: called CC=2 SCAR=7.1
206 27 my ( $self, $text ) = @_;
207 27 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
208 27 $text = $self->_remove_all_leading_and_trailing_blank_lines(
209 $self->_tidy($text) );
210 27 my $checksum = $self->_get_checksum($text);
211 27 my $start = sprintf $self->_start_marker_format => $class,
212 $self->_get_version,
213 $checksum;
214 27 my $end = sprintf $self->_end_marker_format => $class, $self->_get_version,
215 $checksum;
216  
217 27 return <<"END";
218 $start
219  
220 $text
221  
222 $end
223 END
224 }
225  
226 178 ▶sub _version_re {
Subroutine _version_re: called CC=1 SCAR=0.0
227 178 return qr/[0-9]+\.[0-9]+/;
228 }
229  
230 104 ▶sub _remove_all_leading_and_trailing_blank_lines {
Subroutine _remove_all_leading_and_trailing_blank_lines: called CC=3 SCAR=11.2
231 104 my ( $self, $perl ) = @_;
232  
233 # note: we're not using trim() because if they pass in code that
234 # starts with indentation, we'll break it
235 104 my @lines = split /\n/ => $perl;
236 104 ▶ while ( $lines[0] =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /^\s*$/) 48 104 152
237 48 shift @lines;
238 }
239 104 ▶ while ( $lines[-1] =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[-1] =~ /^\s*$/) 0 104 104
240 0 pop @lines;
241 }
242 104 return return join "\n" => @lines;
243 }
244  
245 54 ▶sub _get_version {
Subroutine _get_version: called CC=5 SCAR=24.2
246 54 my $self = shift;
247 54 my $version_re = $self->_version_re;
248 54 my $version = $self->VERSION;
249 54 ▶ if ( defined $version && $version =~ /$version_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version and $version =~ /$version_re/) 54 0 54
Condition33%1/3
defined $version and $version =~ /$version_re/
!ll&&!rl&&r
0054
MC/DC0%0/2
defined $version and $version =~ /$version_re/
defined $version $version =~ /$version_re/
Truth table33%1/3
A and B
A= defined $versionB= $version =~ /$version_re/
ABresult
0X0
100
111
250 54 return $version;
251 }
252 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
253 0 ▶ if ( !defined $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version) { } 0 0 0
254 0 croak("$class does not define a VERSION");
255 }
256 else {
257 0 croak("$class version '$version' does not match '$version_re'");
258 }
259 }
260  
261 0 ▶sub _tidy {
Subroutine _tidy: not called CC=1 SCAR=6.9
262  
263 # by default, we do not tidy code unless it's overridden in the child
264 0 my ( $self, $code ) = @_;
265 0 return $code;
266 }
267  
268 6 1;
269  
270 __END__
↑ Summary lib/CodeGen/Protection/Format/Perl.pm »