| 1 | package Perl::Minify; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use v5.16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use PPI::Document (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | our @EXPORT = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | our @EXPORT_OK = qw(minify minify_sub minify_error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 9 | our %EXPORT_TAGS = (all => [qw(minify minify_sub minify_error)]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | our $VERSION = '0.4.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # Error reporting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | our $ERROR = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | sub minify_error { return $ERROR } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
minify_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Keys we actually read from the user-supplied options hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | my @KNOWN_OPTS = qw(strip_comments strip_pod strip_whitespace strip_semicolons shorten_vars wrap optimize keep_name escape_newlines); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Keywords where a space directly before '{' is never significant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # ("sub foo {", "else {", "grep { ... } ..."). All verified valid Perl. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 9 | my %BLOCK_KEYWORD = map { $_ => 1 } qw(sub else elsif unless grep map sort); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Punctuation operators/structures where adjacent whitespace is never significant. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 9 | my %PUNCT_OP = map { $_ => 1 } ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | '+', '-', '*', '/', '%', '**', ',', ';', ':', '.', '=', '==', '!=', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | '<=', '>=', '<', '>', '!', '~', '|', '&', '^', '<<', '>>', '?', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ':=', '=>', '&&', '||', '//', '->', '+=', '-=', '*=', '/=', '.=', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | '<=>', '++', '--', '**=', '<<=', '>>=', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # Token classes whose content we consider "string/regex-like" and whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # interior whitespace must never be used as a wrap break point. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # NOTE: PPI::Token::QuoteLike::Words (qw()) is intentionally excluded: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # qw() items are whitespace-separated and Perl treats an inserted "\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # as equivalent to a space, so breaking inside qw() is always safe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | my @UNSAFE_TOKEN_CLASSES = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | PPI::Token::Quote | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | PPI::Token::QuoteLike::Command | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | PPI::Token::QuoteLike::Regexp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | PPI::Token::QuoteLike::Readline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | PPI::Token::Regexp::Match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | PPI::Token::Regexp::Substitute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | PPI::Token::Regexp::Transliterate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | PPI::Token::HereDoc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | PPI::Token::Data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | PPI::Token::Pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # Token classes where symbols cannot be renamed via PPI traversal because | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # they're embedded in string content. We'll handle these with post-processing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 9 | my @STRING_EMBEDDED_CLASSES = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | PPI::Token::Quote::Double | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | PPI::Token::Quote::Interpolate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | PPI::Token::QuoteLike::Command | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | PPI::Token::QuoteLike::Backtick | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | PPI::Token::QuoteLike::Regexp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | PPI::Token::Regexp::Match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | PPI::Token::Regexp::Substitute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | PPI::Token::Regexp::Transliterate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | PPI::Token::HereDoc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # Token classes that disqualify a symbol from being renamed at all. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | my @NO_RENAME_ANCESTOR_CLASSES = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | PPI::Token::Quote::Single | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | PPI::Token::Quote::Literal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | PPI::Token::QuoteLike::Words | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | PPI::Token::QuoteLike::Readline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Public API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 52 | ▶ | sub minify { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
minify: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 52 | my ($code, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 52 | $opts = _resolve_opts($opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 52 | $ERROR = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 52 | my $doc = PPI::Document->new(\$code); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 52 | ▶ | unless (defined $doc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $ERROR = 'Failed to parse Perl code - PPI syntax error'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 52 | ▶ | _strip_comments($doc) if $opts->{strip_comments}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 52 | ▶ | _strip_pod($doc) if $opts->{strip_pod}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 52 | ▶ | _strip_whitespace($doc) if $opts->{strip_whitespace}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 52 | ▶ | _strip_semicolons($doc) if $opts->{strip_semicolons}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 52 | ▶ | _optimize_constant_folding($doc) if $opts->{optimize}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 52 | ▶ | my $varmap = _shorten_vars($doc) if $opts->{shorten_vars}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 52 | ▶ | _escape_newlines($doc) if $opts->{escape_newlines}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 52 | my $result = $doc->serialize; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Post-process: replace variables in interpolated strings/regexes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 52 | ▶ | if ($varmap && %$varmap) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$varmap and %$varmap
MC/DC100%2/2
$varmap and %$varmap
$varmap
%$varmap
Truth table100%3/3
A and B
A= $varmapB= %$varmap
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 10 | $result = _replace_vars_in_interpolations($result, $doc, $varmap); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 52 | ▶ | $result = _wrap($result, $opts->{wrap}) if $opts->{wrap}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 52 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 10 | ▶ | sub minify_sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
minify_sub: called CC=17 SCAR=29.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | my ($code, $sub_name, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 10 | $opts = _resolve_opts($opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 10 | $ERROR = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 10 | my $doc = PPI::Document->new(\$code); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 10 | ▶ | unless (defined $doc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $ERROR = 'Failed to parse Perl code - PPI syntax error'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 10 | my $found = $doc->find('PPI::Statement::Sub'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 10 | ▶ | $found = [] unless ref $found eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 10 | my ($sub) = grep { $_->name eq $sub_name } @$found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 10 | ▶ | unless (defined $sub) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | $ERROR = "Subroutine '$sub_name' not found in code"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Clone the sub and optionally make it anonymous (in place modification) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 7 | my $sub_clone = $sub->clone; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | _make_sub_anonymous($sub_clone) unless $opts->{keep_name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Remove the sub from its parent to make it standalone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | ▶ | $sub_clone->remove if $sub_clone->parent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # Create a minimal document containing just this sub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 7 | my $new_doc = PPI::Document->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 7 | $new_doc->{children} = [ $sub_clone ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | $sub_clone->{parent} = $new_doc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | ▶ | weaken($sub_clone->{parent}) if ref($sub_clone->{parent}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 7 | ▶ | _strip_comments($new_doc) if $opts->{strip_comments}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 7 | ▶ | _strip_pod($new_doc) if $opts->{strip_pod}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | ▶ | _strip_whitespace($new_doc) if $opts->{strip_whitespace}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | ▶ | _strip_semicolons($new_doc) if $opts->{strip_semicolons}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | _optimize_constant_folding($new_doc) if $opts->{optimize}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | my $varmap = _shorten_vars($new_doc) if $opts->{shorten_vars}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 7 | ▶ | _escape_newlines($new_doc) if $opts->{escape_newlines}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 7 | my $result = $new_doc->serialize; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # Post-process: replace variables in interpolated strings/regexes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 7 | ▶ | if ($varmap && %$varmap) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$varmap and %$varmap
MC/DC50%1/2
$varmap and %$varmap
$varmap
%$varmap
Truth table66%2/3
A and B
A= $varmapB= %$varmap
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | $result = _replace_vars_in_interpolations($result, $new_doc, $varmap); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | ▶ | $result = _wrap($result, $opts->{wrap}) if $opts->{wrap}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 7 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # Option handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 62 | ▶ | sub _resolve_opts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_opts: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 62 | my ($opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 62 | ▶ | $opts = {} unless defined $opts && ref $opts eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $opts and ref $opts eq "HASH"
MC/DC50%1/2
defined $opts and ref $opts eq "HASH"
defined $opts
ref $opts eq "HASH"
Truth table66%2/3
A and B
A= defined $optsB= ref $opts eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 62 | my %defaults = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | strip_comments => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | strip_pod => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | strip_whitespace => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | strip_semicolons => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | shorten_vars => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | wrap => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | optimize => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | keep_name => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | escape_newlines => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 62 | my %out = %defaults; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 62 | for my $k (@KNOWN_OPTS) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 558 | ▶ | $out{$k} = $opts->{$k} if exists $opts->{$k}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 62 | return \%out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # Modifying a sub to make it anonymous (in place) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | sub _make_sub_anonymous { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_sub_anonymous: called CC=12 SCAR=27.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | my ($sub) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # PPI::Statement::Sub token layout: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # PPI::Token::Word 'sub' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # PPI::Token::Whitespace ' ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # PPI::Token::Word '<name>' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # PPI::Token::Whitespace ' ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # PPI::Structure::Block { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # We remove the name and its surrounding whitespace. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | my @children = $sub->children; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | my $saw_sub = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | for my $child (@children) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 3 | ▶ | if (!$saw_sub && $child->isa('PPI::Token::Word') && $child->content eq 'sub') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $saw_sub and $child->isa("PPI::Token::Word") and $child->content eq "sub"
not $saw_sub and $child->isa("PPI::Token::Word")
MC/DC33%1/3
not $saw_sub and $child->isa("PPI::Token::Word") and $child->content eq "sub"
not $saw_sub
$child->isa("PPI::Token::Word")
$child->content eq "sub"
Truth table50%2/4
not(A) and B and C
A= not $saw_subB= $child->isa("PPI::Token::Word")C= $child->content eq "sub"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | $saw_sub = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 2 | ▶ | if ($saw_sub) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # Remove whitespace and the name word after 'sub' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | ▶ | if ($child->isa('PPI::Token::Whitespace')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | $child->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } elsif ($child->isa('PPI::Token::Word')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # This is the sub name - delete it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | $child->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | last; # Done - the rest is the block and signature | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # Hit the block or something else - we're done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Ensure there's a single space between 'sub' and the block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | my @kids = $sub->children; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | if (@kids >= 2 && $kids[0]->isa('PPI::Token::Word') && $kids[0]->content eq 'sub') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@kids >= 2 and $kids[0]->isa("PPI::Token::Word") and $kids[0]->content eq "sub"
@kids >= 2 and $kids[0]->isa("PPI::Token::Word")
MC/DC0%0/3
@kids >= 2 and $kids[0]->isa("PPI::Token::Word") and $kids[0]->content eq "sub"
@kids >= 2
$kids[0]->isa("PPI::Token::Word")
$kids[0]->content eq "sub"
Truth table25%1/4
A and B and C
A= @kids >= 2B= $kids[0]->isa("PPI::Token::Word")C= $kids[0]->content eq "sub"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # Check if there's whitespace between sub and the next element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | if (!$kids[1]->isa('PPI::Token::Whitespace')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | # Insert a whitespace token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | my $ws = PPI::Token::Whitespace->new(' '); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $ws->insert_before($kids[1]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # Generic PPI helpers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 514 | ▶ | sub _find { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 514 | my ($doc, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 514 | my $found = $doc->find($class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 514 | ▶ | return ref $found ? $found : []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 462 | ▶ | sub _is_unsafe_class { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_unsafe_class: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 462 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 462 | for my $cls (@UNSAFE_TOKEN_CLASSES) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 4388 | ▶ | return 1 if $tok->isa($cls); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 433 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # Stripping passes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 58 | ▶ | sub _strip_comments { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strip_comments: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 58 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 58 | my $found = _find($doc, 'PPI::Token::Comment'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 58 | $_->delete for @$found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 58 | ▶ | sub _strip_pod { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strip_pod: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 58 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 58 | my $found = _find($doc, 'PPI::Token::Pod'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 58 | $_->delete for @$found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 57 | ▶ | sub _strip_whitespace { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strip_whitespace: called CC=27 SCAR=38.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 57 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 57 | my $found = _find($doc, 'PPI::Token::Whitespace'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 57 | ▶ | return unless @$found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 54 | for my $ws (@$found) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 1157 | my $prev = $ws->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 1157 | my $next = $ws->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # Leading or trailing whitespace: always delete. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 1157 | ▶ | unless ($prev && $next) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$prev and $next
MC/DC100%2/2
$prev and $next
$prev
$next
Truth table100%3/3
A and B
A= $prevB= $next
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 213 | $ws->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 213 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 944 | my $pc = $prev->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 944 | my $nc = $next->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # Rule 1: word|word -> single space (keep "use strict", "$str eq", etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 944 | ▶ | if ($pc =~ /\w$/ && $nc =~ /^\w/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pc =~ /\w$/u and $nc =~ /^\w/u
MC/DC100%2/2
$pc =~ /\w$/u and $nc =~ /^\w/u
$pc =~ /\w$/u
$nc =~ /^\w/u
Truth table100%3/3
A and B
A= $pc =~ /\w$/uB= $nc =~ /^\w/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 86 | $ws->{content} = ' '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 86 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # Rule 2: '.' concat operator next to a Number -> single space. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 858 | ▶ | if ($prev->isa('PPI::Token::Number') && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$prev->isa("PPI::Token::Number") and $next->isa("PPI::Token::Operator") and $nc eq "."
$prev->isa("PPI::Token::Number") and $next->isa("PPI::Token::Operator")
MC/DC0%0/3
$prev->isa("PPI::Token::Number") and $next->isa("PPI::Token::Operator") and $nc eq "."
$prev->isa("PPI::Token::Number")
$next->isa("PPI::Token::Operator")
$nc eq "."
Truth table50%2/4
A and B and C
A= $prev->isa("PPI::Token::Number")B= $next->isa("PPI::Token::Operator")C= $nc eq "."
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | $next->isa('PPI::Token::Operator') && $nc eq '.') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | $ws->{content} = ' '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 858 | ▶ | if ($prev->isa('PPI::Token::Operator') && $pc eq '.' && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$prev->isa("PPI::Token::Operator") and $pc eq "." and $next->isa("PPI::Token::Number")
$prev->isa("PPI::Token::Operator") and $pc eq "."
MC/DC0%0/3
$prev->isa("PPI::Token::Operator") and $pc eq "." and $next->isa("PPI::Token::Number")
$prev->isa("PPI::Token::Operator")
$pc eq "."
$next->isa("PPI::Token::Number")
Truth table75%3/4
A and B and C
A= $prev->isa("PPI::Token::Operator")B= $pc eq "."C= $next->isa("PPI::Token::Number")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | $next->isa('PPI::Token::Number')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | $ws->{content} = ' '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | # Rule 3: two consecutive symbols/magic need separation ($fh $data). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 858 | ▶ | if (_is_symbol_like($prev) && _is_symbol_like($next)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_symbol_like($prev) and _is_symbol_like($next)
MC/DC100%2/2
_is_symbol_like($prev) and _is_symbol_like($next)
_is_symbol_like($prev)
_is_symbol_like($next)
Truth table100%3/3
A and B
A= _is_symbol_like($prev)B= _is_symbol_like($next)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 3 | $ws->{content} = ' '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # Rule 4: letter-starting operator (x, eq, ne, lt, ...) after word-char. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 855 | ▶ | if ($nc =~ /^[a-z]/i && $next->isa('PPI::Token::Operator') && $pc =~ /\w$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$nc =~ /^[a-z]/iu and $next->isa("PPI::Token::Operator") and $pc =~ /\w$/u
$nc =~ /^[a-z]/iu and $next->isa("PPI::Token::Operator")
MC/DC0%0/3
$nc =~ /^[a-z]/iu and $next->isa("PPI::Token::Operator") and $pc =~ /\w$/u
$nc =~ /^[a-z]/iu
$next->isa("PPI::Token::Operator")
$pc =~ /\w$/u
Truth table75%3/4
A and B and C
A= $nc =~ /^[a-z]/iuB= $next->isa("PPI::Token::Operator")C= $pc =~ /\w$/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | $ws->{content} = ' '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # Rule 5 (new A): punctuation-operator/structure adjacency -> delete. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 855 | ▶ | if (_is_punct_op($prev) && _is_punct_op($next)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_punct_op($prev) and _is_punct_op($next)
MC/DC100%2/2
_is_punct_op($prev) and _is_punct_op($next)
_is_punct_op($prev)
_is_punct_op($next)
Truth table100%3/3
A and B
A= _is_punct_op($prev)B= _is_punct_op($next)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | $ws->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # Rule 6 (new B): trim space after opening bracket (unless next is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # word - protects $h{key}, $h{ bareword } shorthand). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 852 | ▶ | if (_is_open_bracket($prev)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | unless ($next->isa('PPI::Token::Word')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | $ws->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # Rule 7 (new B): trim space before closing bracket (always safe). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 852 | ▶ | if (_is_close_bracket($next)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | $ws->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # Rule 8: "keyword {" -> "keyword{" (sub name, else, elsif, unless, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # grep/map/sort with block). Space is never significant here and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # word cannot run into '{'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 852 | ▶ | if ($next->isa('PPI::Structure::Block') && $nc eq '{' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
$next->isa("PPI::Structure::Block") and $nc eq "{" and $prev->isa("PPI::Token::Word") and $BLOCK_KEYWORD{$pc}
$next->isa("PPI::Structure::Block") and $nc eq "{" and $prev->isa("PPI::Token::Word")
$next->isa("PPI::Structure::Block") and $nc eq "{"
MC/DC0%0/4
$next->isa("PPI::Structure::Block") and $nc eq "{" and $prev->isa("PPI::Token::Word") and $BLOCK_KEYWORD{$pc}
$next->isa("PPI::Structure::Block")
$nc eq "{"
$prev->isa("PPI::Token::Word")
$BLOCK_KEYWORD{$pc}
Truth table40%2/5
A and B and C and D
A= $next->isa("PPI::Structure::Block")B= $nc eq "{"C= $prev->isa("PPI::Token::Word")D= $BLOCK_KEYWORD{$pc}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | && $prev->isa('PPI::Token::Word') && $BLOCK_KEYWORD{ $pc }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | $ws->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # Default: delete unnecessary whitespace. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 852 | $ws->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | # A semicolon is optional before a closing block delimiter when the statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # already ends in an expression. Avoid keyword-only statements and labels: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | # removing their terminator can change the grammar (or make a label invalid). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 58 | ▶ | sub _strip_semicolons { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strip_semicolons: called CC=12 SCAR=25.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 58 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 58 | my $blocks = _find($doc, 'PPI::Structure::Block'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 58 | for my $block (@$blocks) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | my @children = grep { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 65 | ▶ | !$_->isa('PPI::Token::Whitespace') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_->isa('PPI::Token::Whitespace') || $_->isa('PPI::Token::Comment')
MC/DC50%1/2
$_->isa('PPI::Token::Whitespace') || $_->isa('PPI::Token::Comment')
$_->isa('PPI::Token::Whitespace')
$_->isa('PPI::Token::Comment')
Truth table66%2/3
A || B
A= $_->isa('PPI::Token::Whitespace')B= $_->isa('PPI::Token::Comment')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | && !$_->isa('PPI::Token::Comment') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } $block->children; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 65 | my $stmt = $children[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 65 | ▶ | next unless $stmt && $stmt->isa('PPI::Statement'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$stmt and $stmt->isa("PPI::Statement")
MC/DC50%1/2
$stmt and $stmt->isa("PPI::Statement")
$stmt
$stmt->isa("PPI::Statement")
Truth table66%2/3
A and B
A= $stmtB= $stmt->isa("PPI::Statement")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 64 | my @tokens = $stmt->tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 64 | my $semi = pop @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 64 | ▶ | next unless $semi && $semi->isa('PPI::Token::Structure') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$semi and $semi->isa("PPI::Token::Structure") and $semi->content eq ";"
$semi and $semi->isa("PPI::Token::Structure")
MC/DC66%2/3
$semi and $semi->isa("PPI::Token::Structure") and $semi->content eq ";"
$semi
$semi->isa("PPI::Token::Structure")
$semi->content eq ";"
Truth table75%3/4
A and B and C
A= $semiB= $semi->isa("PPI::Token::Structure")C= $semi->content eq ";"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | && $semi->content eq ';'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 48 | my $last = $tokens[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 48 | ▶ | next unless $last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 48 | ▶ | next if $last->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$last->isa("PPI::Token::Word") or $last->isa("PPI::Token::Operator") or $last->isa("PPI::Token::Comment")
$last->isa("PPI::Token::Word") or $last->isa("PPI::Token::Operator")
MC/DC66%2/3
$last->isa("PPI::Token::Word") or $last->isa("PPI::Token::Operator") or $last->isa("PPI::Token::Comment")
$last->isa("PPI::Token::Word")
$last->isa("PPI::Token::Operator")
$last->isa("PPI::Token::Comment")
Truth table75%3/4
A or B or C
A= $last->isa("PPI::Token::Word")B= $last->isa("PPI::Token::Operator")C= $last->isa("PPI::Token::Comment")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | || $last->isa('PPI::Token::Operator') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | || $last->isa('PPI::Token::Comment'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 44 | $semi->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 966 | ▶ | sub _is_symbol_like { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_symbol_like: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 966 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 966 | ▶ | return $tok->isa('PPI::Token::Symbol') || $tok->isa('PPI::Token::Magic'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$tok->isa('PPI::Token::Symbol') || $tok->isa('PPI::Token::Magic')
MC/DC50%1/2
$tok->isa('PPI::Token::Symbol') || $tok->isa('PPI::Token::Magic')
$tok->isa('PPI::Token::Symbol')
$tok->isa('PPI::Token::Magic')
Truth table66%2/3
A || B
A= $tok->isa('PPI::Token::Symbol')B= $tok->isa('PPI::Token::Magic')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 1023 | ▶ | sub _is_punct_op { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_punct_op: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 1023 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 1023 | ▶ | return 0 unless $tok->isa('PPI::Token::Operator') || $tok->isa('PPI::Token::Structure'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tok->isa("PPI::Token::Operator") or $tok->isa("PPI::Token::Structure")
MC/DC50%1/2
$tok->isa("PPI::Token::Operator") or $tok->isa("PPI::Token::Structure")
$tok->isa("PPI::Token::Operator")
$tok->isa("PPI::Token::Structure")
Truth table66%2/3
A or B
A= $tok->isa("PPI::Token::Operator")B= $tok->isa("PPI::Token::Structure")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 191 | my $c = $tok->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 191 | return exists $PUNCT_OP{$c}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 852 | ▶ | sub _is_open_bracket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_open_bracket: called CC=4 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 852 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 852 | ▶ | return 0 unless $tok->isa('PPI::Token::Structure'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | my $c = $tok->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | return $c eq '(' || $c eq '[' || $c eq '{'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$c eq '(' || $c eq '[' || $c eq '{'
$c eq "(" or $c eq "["
MC/DC0%0/3
$c eq '(' || $c eq '[' || $c eq '{'
$c eq "("
$c eq "["
$c eq '{'
Truth table0%0/4
A or B || C
A= $c eq "("B= $c eq "["C= $c eq '{'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 852 | ▶ | sub _is_close_bracket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_close_bracket: called CC=4 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 852 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 852 | ▶ | return 0 unless $tok->isa('PPI::Token::Structure'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | my $c = $tok->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | return $c eq ')' || $c eq ']' || $c eq '}'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$c eq ')' || $c eq ']' || $c eq '}'
$c eq ")" or $c eq "]"
MC/DC0%0/3
$c eq ')' || $c eq ']' || $c eq '}'
$c eq ")"
$c eq "]"
$c eq '}'
Truth table0%0/4
A or B || C
A= $c eq ")"B= $c eq "]"C= $c eq '}'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # Variable shortening | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 13 | ▶ | sub _shorten_vars { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_shorten_vars: called CC=25 SCAR=33.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 13 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 13 | my %map; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # Pass 1a: explicit my/our/state declarations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # NOTE: PPI occasionally misclassifies "open(my $fh, "<", $_[0])" as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # PPI::Statement::Variable too. _extract_decl_names handles this by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # stopping at the first non-LHS token, so only the real declared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | # names (e.g. $fh) get collected, never subsequent args like $_[0]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 13 | my $var_stmts = _find($doc, 'PPI::Statement::Variable'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 13 | for my $stmt (@$var_stmts) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # Only treat as a declaration if the first significant token is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # my/our/state. Skip "local" - that's localization, not a lexical | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # declaration, and the variables involved are usually magic ($/, etc). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 12 | my @sig = grep { !$_->isa('PPI::Token::Whitespace') } $stmt->children; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 12 | ▶ | next unless @sig && $sig[0]->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@sig and $sig[0]->isa("PPI::Token::Word") and $sig[0]->content =~ /^(my|our|state)$/u
@sig and $sig[0]->isa("PPI::Token::Word")
MC/DC0%0/3
@sig and $sig[0]->isa("PPI::Token::Word") and $sig[0]->content =~ /^(my|our|state)$/u
@sig
$sig[0]->isa("PPI::Token::Word")
$sig[0]->content =~ /^(my|our|state)$/u
Truth table25%1/4
A and B and C
A= @sigB= $sig[0]->isa("PPI::Token::Word")C= $sig[0]->content =~ /^(my|our|state)$/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | && $sig[0]->content =~ /^(my|our|state)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 12 | for my $name (_extract_decl_names($stmt)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 12 | ▶ | $map{$name} //= _next_short_name(\%map, $name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$map{$name} //= _next_short_name(\%map, $name)
MC/DC0%0/2
$map{$name} //= _next_short_name(\%map, $name)
$map{$name}
_next_short_name(\%map, $name)
Truth table66%2/3
A //= B
A= $map{$name}B= _next_short_name(\%map, $name)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # Pass 1b: inline my/our/state inside compound statements (for my $x ...). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 13 | my $compounds = _find($doc, 'PPI::Statement::Compound'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 13 | for my $stmt (@$compounds) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 2 | my @kids = $stmt->children; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 2 | ▶ | for (my $i = 0; $i < @kids; $i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 15 | my $c = $kids[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 15 | ▶ | next unless $c->isa('PPI::Token::Word') && $c->content =~ /^(my|our|state)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->isa("PPI::Token::Word") and $c->content =~ /^(my|our|state)$/u
MC/DC100%2/2
$c->isa("PPI::Token::Word") and $c->content =~ /^(my|our|state)$/u
$c->isa("PPI::Token::Word")
$c->content =~ /^(my|our|state)$/u
Truth table100%3/3
A and B
A= $c->isa("PPI::Token::Word")B= $c->content =~ /^(my|our|state)$/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | # Skip whitespace/comments to the first significant token after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | # the declaration keyword, so "for my $x ..." works even when the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | # token stream still contains spacing (strip_whitespace => 0). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 2 | my $j = $i + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 2 | ▶ | while ($j < @kids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$j < @kids and $kids[$j]->isa("PPI::Token::Whitespace") || $kids[$j]->isa("PPI::Token::Comment")
$kids[$j]->isa("PPI::Token::Whitespace") or $kids[$j]->isa("PPI::Token::Comment")
MC/DC33%1/3
$j < @kids and $kids[$j]->isa("PPI::Token::Whitespace") || $kids[$j]->isa("PPI::Token::Comment")
$j < @kids
$kids[$j]->isa("PPI::Token::Whitespace")
$kids[$j]->isa("PPI::Token::Comment")
Truth table50%2/4
A and B or C
A= $j < @kidsB= $kids[$j]->isa("PPI::Token::Whitespace")C= $kids[$j]->isa("PPI::Token::Comment")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | && ($kids[$j]->isa('PPI::Token::Whitespace') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | || $kids[$j]->isa('PPI::Token::Comment'))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 1 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 2 | ▶ | my $n = $kids[$j] // next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$kids[$j] // (next)
MC/DC0%0/1
$kids[$j] // (next)
$kids[$j]
Truth table50%1/2
A
A= $kids[$j]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 2 | ▶ | next unless $n->isa('PPI::Token::Symbol'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 2 | my $name = $n->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 2 | ▶ | next unless $name =~ /^[\$\@\%]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 2 | ▶ | $map{$name} //= _next_short_name(\%map, $name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$map{$name} //= _next_short_name(\%map, $name)
MC/DC0%0/2
$map{$name} //= _next_short_name(\%map, $name)
$map{$name}
_next_short_name(\%map, $name)
Truth table33%1/3
A //= B
A= $map{$name}B= _next_short_name(\%map, $name)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 13 | ▶ | return \%map unless %map; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # Pass 2: rename symbol references in PPI-parseable locations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # (skipping symbols in non-interpolating strings or other unsafe contexts). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 12 | my $symbols = _find($doc, 'PPI::Token::Symbol'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 12 | for my $sym (@$symbols) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 23 | ▶ | next if $sym->isa('PPI::Token::Magic'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 23 | ▶ | next if _symbol_in_unsafe_context($sym); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 23 | my $var = $sym->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 23 | ▶ | if (exists $map{$var}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 21 | $sym->{content} = $map{$var}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 21 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | # Cross-sigil fallback (e.g. %hash -> $hash{...}). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 2 | my $base = $var; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 2 | $base =~ s/^[\$\@\%]//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 2 | for my $sigil (qw/$ @ %/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 6 | my $alt = $sigil . $base; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 6 | ▶ | if (exists $map{$alt}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 1 | my $short = $map{$alt}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 1 | $short =~ s/^[\$\@\%]//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 1 | $sym->{content} = substr($var, 0, 1) . $short; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 12 | return \%map; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # Post-process replacement for variables in interpolated strings/regexes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | # PPI doesn't parse the interior of these tokens, so we must do text replacement. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | # We only replace within the content of interpolation-supporting tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 12 | ▶ | sub _replace_vars_in_interpolations { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_replace_vars_in_interpolations: called CC=10 SCAR=23.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 12 | my ($text, $doc, $map) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | # Find all tokens that support interpolation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 12 | my @interp_classes = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | PPI::Token::Quote::Double | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | PPI::Token::Quote::Interpolate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | PPI::Token::QuoteLike::Command | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | PPI::Token::QuoteLike::Backtick | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | PPI::Token::QuoteLike::Regexp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | PPI::Token::Regexp::Match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | PPI::Token::Regexp::Substitute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | PPI::Token::HereDoc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 12 | my @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 12 | for my $class (@interp_classes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 96 | my $found = $doc->find($class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 96 | ▶ | push @tokens, @$found if ref $found eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 12 | ▶ | return $text unless @tokens; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # Build a map of original content -> replacement content for each token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 4 | my %replacements; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 4 | for my $tok (@tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 4 | my $orig_content = $tok->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 4 | my $new_content = $orig_content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | # Sort variables by length (longest first) to avoid partial replacements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | my @vars = sort { length($b) <=> length($a) } keys %$map; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 4 | for my $var (@vars) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 4 | my $replacement = $map->{$var}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 4 | my $sigil = substr($var, 0, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 4 | my $name = substr($var, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 4 | my $short_name = substr($replacement, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | # Match the variable with word boundaries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # Pattern: $varname followed by non-word-char or { or [ or end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 4 | my $pattern = qr/\Q$var\E(?=\{|\[|[^\w]|\z)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | 4 | $new_content =~ s/$pattern/$replacement/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | # Handle cross-sigil: if we renamed %hash, also rename $hash{...} and @hash[...] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 4 | ▶ | if ($sigil eq '%') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 1 | $new_content =~ s/\$\Q$name\E(?=\{)/'$' . $short_name/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | $new_content =~ s/\@\Q$name\E(?=\[)/'@' . $short_name/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | } elsif ($sigil eq '@') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | $new_content =~ s/\$\Q$name\E(?=\[)/'$' . $short_name/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 4 | ▶ | if ($new_content ne $orig_content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 4 | $replacements{$orig_content} = $new_content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | # Apply replacements to the serialized text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | # We need to be careful to replace exact matches only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 4 | for my $orig (keys %replacements) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 4 | my $new = $replacements{$orig}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | # Use quotemeta to escape special regex characters in the original content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 4 | my $quoted = quotemeta($orig); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 4 | $text =~ s/$quoted/$new/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 4 | return $text; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 23 | ▶ | sub _symbol_in_unsafe_context { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_symbol_in_unsafe_context: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 23 | my ($sym) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 23 | my $cur = $sym; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 23 | ▶ | while (defined $cur) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 81 | for my $cls (@NO_RENAME_ANCESTOR_CLASSES) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 324 | ▶ | return 1 if $cur->isa($cls); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 81 | $cur = $cur->parent; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 23 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 12 | ▶ | sub _extract_decl_names { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_decl_names: called CC=16 SCAR=32.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 12 | my ($stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 12 | my @names; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 12 | my $found_decl_word = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 12 | for my $child ($stmt->children) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | # Stop at the assignment operator - everything after is RHS, not a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | # declared name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 36 | ▶ | last if $child->isa('PPI::Token::Operator') && $child->content eq '='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$child->isa("PPI::Token::Operator") and $child->content eq "="
MC/DC50%1/2
$child->isa("PPI::Token::Operator") and $child->content eq "="
$child->isa("PPI::Token::Operator")
$child->content eq "="
Truth table66%2/3
A and B
A= $child->isa("PPI::Token::Operator")B= $child->content eq "="
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 28 | ▶ | next if $child->isa('PPI::Token::Whitespace'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 28 | ▶ | next if $child->isa('PPI::Token::Comment'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 28 | ▶ | if (!$found_decl_word) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 12 | ▶ | if ($child->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$child->isa("PPI::Token::Word") and $child->content =~ /^(my|our|state)$/u
MC/DC0%0/2
$child->isa("PPI::Token::Word") and $child->content =~ /^(my|our|state)$/u
$child->isa("PPI::Token::Word")
$child->content =~ /^(my|our|state)$/u
Truth table33%1/3
A and B
A= $child->isa("PPI::Token::Word")B= $child->content =~ /^(my|our|state)$/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | && $child->content =~ /^(my|our|state)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 12 | $found_decl_word = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 12 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | # We are in the LHS. Collect symbols and recurse into list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | # structures. A ',' separator is allowed; anything else (a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # String, Word, or non-list Operator) means the LHS is over - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | # stop. This guards against false-positive PPI::Statement::Variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | # nodes like "my $fh, "<", $_[0]" produced by open(my $fh, ...). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 16 | ▶ | if ($child->isa('PPI::Token::Symbol')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 12 | my $var = $child->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 12 | ▶ | push @names, $var if $var =~ /^[\$\@\%]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 12 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 4 | ▶ | if ($child->isa('PPI::Token::Operator') && $child->content eq ',') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$child->isa("PPI::Token::Operator") and $child->content eq ","
MC/DC0%0/2
$child->isa("PPI::Token::Operator") and $child->content eq ","
$child->isa("PPI::Token::Operator")
$child->content eq ","
Truth table33%1/3
A and B
A= $child->isa("PPI::Token::Operator")B= $child->content eq ","
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 4 | ▶ | if ($child->isa('PPI::Structure::List') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$child->isa("PPI::Structure::List") or $child->isa("PPI::Structure::Constructor") or $child->isa("PPI::Structure::Block")
$child->isa("PPI::Structure::List") or $child->isa("PPI::Structure::Constructor")
MC/DC0%0/3
$child->isa("PPI::Structure::List") or $child->isa("PPI::Structure::Constructor") or $child->isa("PPI::Structure::Block")
$child->isa("PPI::Structure::List")
$child->isa("PPI::Structure::Constructor")
$child->isa("PPI::Structure::Block")
Truth table25%1/4
A or B or C
A= $child->isa("PPI::Structure::List")B= $child->isa("PPI::Structure::Constructor")C= $child->isa("PPI::Structure::Block")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | || $child->isa('PPI::Structure::Constructor') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | || $child->isa('PPI::Structure::Block')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | _extract_names_r($child, \@names); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 12 | return @names; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | ▶ | sub _extract_names_r { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_names_r: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | my ($elem, $names) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | for my $child ($elem->children) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | ▶ | if ($child->isa('PPI::Token::Symbol')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | my $var = $child->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | ▶ | push @$names, $var if $var =~ /^[\$\@\%]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | } elsif ($child->isa('PPI::Node')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | _extract_names_r($child, $names); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 13 | ▶ | sub _next_short_name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_next_short_name: called CC=8 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 13 | my ($map, $orig) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 13 | my $sigil = substr($orig, 0, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | # Build a hash of taken names for O(1) lookup instead of O(N) linear search | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 13 | ▶ | my %taken = map { defined $_ ? ($_ => 1) : () } values %$map; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | # Single letters - avoid $a, $b (used in sort blocks). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 13 | for my $letter ('c' .. 'z', 'a', 'b') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 14 | my $name = $sigil . $letter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 14 | ▶ | return $name unless $taken{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | # Two-letter names. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 0 | for my $l1 ('a' .. 'z') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | for my $l2 ('a' .. 'z') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 0 | my $name = $sigil . $l1 . $l2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | ▶ | return $name unless $taken{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | # Numeric fallback. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 0 | my $name = $sigil . 'v' . $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | ▶ | return $name unless $taken{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | # Optimization passes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 9 | ▶ | sub _optimize_constant_folding { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_optimize_constant_folding: called CC=19 SCAR=29.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 9 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | # Find simple binary operations with constant operands. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | # We work on SIGNIFICANT children (schildren) so spacing doesn't hide | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | # Number/Operator/Number triples, and we fold in place (mutate the Number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # token's content + remove the operator and its neighbouring whitespace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | # rather than inserting new tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 9 | my $statements = _find($doc, 'PPI::Statement'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 9 | for my $stmt (@$statements) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 13 | my $folded = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 13 | ▶ | while ($folded) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 23 | $folded = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 23 | my @sig = $stmt->schildren; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 23 | ▶ | for (my $i = 0; $i < $#sig - 1; $i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 82 | ▶ | next unless $sig[$i]->isa('PPI::Token::Number'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 14 | ▶ | next unless $sig[$i+1]->isa('PPI::Token::Operator'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 14 | ▶ | next unless $sig[$i+2]->isa('PPI::Token::Number'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 14 | my $left = $sig[$i]->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 14 | my $op = $sig[$i+1]->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 14 | my $right = $sig[$i+2]->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | # Only handle safe arithmetic operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 14 | ▶ | next unless $op =~ /^[+\-*]$/; # Division excluded for safety | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | # Precedence guard for chained folds: a +/- pair must not sit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | # next to a */ or ** operator, otherwise left-to-right folding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | # would evaluate out of order (e.g. 2 - 3 * 4 folding 2-3). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 13 | my $before_t = $sig[$i]->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 13 | my $after_t = $sig[$i+2]->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 13 | ▶ | my $before = $before_t && $before_t->isa('PPI::Token::Operator') ? $before_t->content : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$before_t and $before_t->isa("PPI::Token::Operator")
MC/DC0%0/2
$before_t and $before_t->isa("PPI::Token::Operator")
$before_t
$before_t->isa("PPI::Token::Operator")
Truth table33%1/3
A and B
A= $before_tB= $before_t->isa("PPI::Token::Operator")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | 13 | ▶ | my $after = $after_t && $after_t->isa('PPI::Token::Operator') ? $after_t->content : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$after_t and $after_t->isa("PPI::Token::Operator")
MC/DC50%1/2
$after_t and $after_t->isa("PPI::Token::Operator")
$after_t
$after_t->isa("PPI::Token::Operator")
Truth table66%2/3
A and B
A= $after_tB= $after_t->isa("PPI::Token::Operator")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 13 | my $high = qr{^[/*]$|^\*\*$}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 13 | ▶ | if ($op =~ /^[+\-]$/ && ($before =~ $high || $after =~ $high)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$op =~ /^[+\-]$/u and $before =~ /$high/u || $after =~ /$high/u
$before =~ /$high/u or $after =~ /$high/u
MC/DC100%3/3
$op =~ /^[+\-]$/u and $before =~ /$high/u || $after =~ /$high/u
$op =~ /^[+\-]$/u
$before =~ /$high/u
$after =~ /$high/u
Truth table100%4/4
A and B or C
A= $op =~ /^[+\-]$/uB= $before =~ /$high/uC= $after =~ /$high/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 10 | ▶ | if ($op eq '*' && $before eq '/') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$op eq "*" and $before eq "/"
MC/DC0%0/2
$op eq "*" and $before eq "/"
$op eq "*"
$before eq "/"
Truth table66%2/3
A and B
A= $op eq "*"B= $before eq "/"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 10 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 10 | ▶ | if ($op eq '+') { $result = $left + $right; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 1 | elsif ($op eq '-') { $result = $left - $right; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 3 | else { $result = $left * $right; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | # Fold in place: rewrite the left number with the result and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | # drop the operator and right operand. Whitespace cleanup is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | # left to the whitespace stripping pass (which runs after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | # optimize), so chained folds re-rescan cleanly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 10 | $sig[$i]->{content} = $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 10 | $sig[$i+1]->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 10 | $sig[$i+2]->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | # Successful fold - rescan the statement for more folds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 10 | $folded = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 10 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | # Escape literal LF/TAB/CR inside string literals as \n \t \r. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | # Double-quoted and qq strings: literal control chars in the raw body are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | # replaced in place. "\<LF>" becomes "\n" (the backslash must not be doubled). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | # Single-quoted and q() strings: decoded, and if the value contains a control | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | # char, re-emitted as a double-quoted string with "\\", '"', '$', '@' escaped. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | # Token class is left unchanged on purpose: _replace_vars_in_interpolations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | # matches by class, and must not rename variables inside the escaped text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 9 | my %CTRL_ESCAPE = ("\n" => '\n', "\t" => '\t', "\r" => '\r'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 59 | ▶ | sub _escape_newlines { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_newlines: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 59 | my ($doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 59 | for my $tok (@{ _find($doc, 'PPI::Token::Quote::Single') }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 59 | @{ _find($doc, 'PPI::Token::Quote::Literal') }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | 5 | _escape_single_token($tok); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 59 | for my $tok (@{ _find($doc, 'PPI::Token::Quote::Double') }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 59 | @{ _find($doc, 'PPI::Token::Quote::Interpolate') }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 59 | _escape_double_token($tok); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | # Returns (prefix, open, body, close) where body is the raw text between the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | # delimiters (as PPI's ->string reports it), or an empty list if unparseable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 64 | ▶ | sub _split_quote { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_quote: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 64 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 64 | my $content = $tok->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 64 | my $body = $tok->string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 64 | ▶ | return () unless defined $body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | # Layout: prefix . open . body . close, with single-char delimiters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 64 | my $body_pos = length($content) - 1 - length($body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | 64 | ▶ | return () if $body_pos < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 64 | ▶ | return () unless substr($content, $body_pos, length $body) eq $body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 64 | my $head = substr($content, 0, $body_pos); # prefix + open delimiter | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 64 | my $open = substr($head, -1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 64 | my $prefix = substr($head, 0, -1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 64 | my $close = substr($content, -1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | 64 | return ($prefix, $open, $body, $close); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | 5 | ▶ | sub _escape_single_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_single_token: called CC=9 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 5 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | 5 | my ($prefix, $open, $body, $close) = _split_quote($tok); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 5 | ▶ | return unless defined $body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | # Decode: only \\ and an escaped delimiter are escapes in single quotes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 5 | my $val = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 5 | my $len = length $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | 5 | ▶ | for (my $i = 0; $i < $len; ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 25 | my $c = substr($body, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | 25 | ▶ | if ($c eq '\\' && $i + 1 < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c eq "\\" and $i + 1 < $len
MC/DC0%0/2
$c eq "\\" and $i + 1 < $len
$c eq "\\"
$i + 1 < $len
Truth table33%1/3
A and B
A= $c eq "\\"B= $i + 1 < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 0 | my $n = substr($body, $i + 1, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 0 | ▶ | if ($n eq '\\' || $n eq $open || $n eq $close) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$n eq "\\" or $n eq $open or $n eq $close
$n eq "\\" or $n eq $open
MC/DC0%0/3
$n eq "\\" or $n eq $open or $n eq $close
$n eq "\\"
$n eq $open
$n eq $close
Truth table0%0/4
A or B or C
A= $n eq "\\"B= $n eq $openC= $n eq $close
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 0 | $val .= $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 0 | $i += 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | 25 | $val .= $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 25 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | 5 | ▶ | return unless $val =~ /[\n\t\r]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 0 | (my $enc = $val) =~ s/([\\"\$\@])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 0 | $enc =~ s/([\n\t\r])/$CTRL_ESCAPE{$1}/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 0 | $tok->{content} = qq{"$enc"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | 59 | ▶ | sub _escape_double_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_double_token: called CC=9 SCAR=39.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | 59 | my ($tok) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 59 | my ($prefix, $open, $body, $close) = _split_quote($tok); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | 59 | ▶ | return unless defined $body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 59 | ▶ | return unless $body =~ /[\n\t\r]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | # A control char inside an interpolated expression ("@{[ ... ]}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | # "${ ... }", "$h{...}", "$a[...]", "->{...}") is Perl code, and "\n" there | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | # is a syntax error. Leave such strings alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | 0 | ▶ | return if $body =~ /[\$\@]\{|[\$\@][\w:]+[\[\{]|->[\[\{]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | 0 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | 0 | my $len = length $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | 0 | ▶ | for (my $i = 0; $i < $len; ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 0 | my $c = substr($body, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 0 | ▶ | if ($c eq '\\' && $i + 1 < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c eq "\\" and $i + 1 < $len
MC/DC0%0/2
$c eq "\\" and $i + 1 < $len
$c eq "\\"
$i + 1 < $len
Truth table0%0/3
A and B
A= $c eq "\\"B= $i + 1 < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | 0 | my $n = substr($body, $i + 1, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 0 | ▶ | $out .= exists $CTRL_ESCAPE{$n} ? $CTRL_ESCAPE{$n} : "$c$n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | $i += 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | 0 | ▶ | $out .= exists $CTRL_ESCAPE{$c} ? $CTRL_ESCAPE{$c} : $c; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 0 | $tok->{content} = $prefix . $open . $out . $close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | # Word wrap (string/regex aware) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | 7 | ▶ | sub _wrap { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wrap: called CC=37 SCAR=36.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | 7 | my ($text, $width) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | 7 | ▶ | return $text unless $width > 0 && length($text) > $width; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$width > 0 and length $text > $width
MC/DC50%1/2
$width > 0 and length $text > $width
$width > 0
length $text > $width
Truth table66%2/3
A and B
A= $width > 0B= length $text > $width
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | # Build per-token offset map + unsafe ranges + semicolon offsets. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | # Derive tokens from $text itself (a fresh parse) rather than the caller's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | # $doc, because $text may have been post-processed by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | # _replace_vars_in_interpolations(), which changes string lengths and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | # invalidates offsets computed against the original document. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 6 | my $wrap_doc = PPI::Document->new(\$text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | 6 | ▶ | my $all_tokens = $wrap_doc ? $wrap_doc->find('PPI::Token') : []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 6 | ▶ | $all_tokens = ref $all_tokens ? $all_tokens : []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 6 | my @unsafe_ranges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | my @semi_offsets; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | 6 | my $off = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | 6 | for my $tok (@$all_tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | 462 | my $len = length($tok->content); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | 462 | ▶ | if (_is_unsafe_class($tok)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 29 | push @unsafe_ranges, [ $off, $off + $len ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | 462 | ▶ | if ($tok->isa('PPI::Token::Structure') && $tok->content eq ';') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tok->isa("PPI::Token::Structure") and $tok->content eq ";"
MC/DC100%2/2
$tok->isa("PPI::Token::Structure") and $tok->content eq ";"
$tok->isa("PPI::Token::Structure")
$tok->content eq ";"
Truth table100%3/3
A and B
A= $tok->isa("PPI::Token::Structure")B= $tok->content eq ";"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | 41 | push @semi_offsets, $off + $len - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | 462 | $off += $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | 6 | @unsafe_ranges = sort { $a->[0] <=> $b->[0] } @unsafe_ranges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | # Split at semicolons into statements (each includes its trailing ';'). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | 6 | my @stmts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 6 | my $prev = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | 6 | for my $pos (@semi_offsets) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 41 | push @stmts, substr($text, $prev, $pos - $prev + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 41 | $prev = $pos + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 6 | ▶ | if ($prev < length($text)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 2 | my $rest = substr($text, $prev); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 2 | ▶ | push @stmts, $rest if length($rest); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | # Pack statements into lines greedily. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | 6 | my @lines; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | 6 | my $line = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | 6 | for my $stmt (@stmts) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 43 | $stmt =~ s/\n+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | 43 | ▶ | next if $stmt eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 43 | ▶ | if ($line eq '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | 6 | $line = $stmt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | 37 | my $cand = $line . $stmt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | 37 | ▶ | if (length($cand) <= $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 13 | $line = $cand; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | 24 | push @lines, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 24 | $line = $stmt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | 6 | ▶ | push @lines, $line if $line ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | # Split overlong lines at safe spaces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | 6 | my @final; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | 6 | for my $l (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | 30 | ▶ | if (length($l) > $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 10 | my $split_result = _split_line_safe($l, $width, \@unsafe_ranges); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | 10 | push @final, split /\n/, $split_result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | 20 | push @final, $l; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | # Repack: if a line is short and the next line would fit when combined, merge them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | # Also rebalance: if a line is very short, try to move content from the previous line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | 6 | my @repacked; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | 6 | for my $i (0 .. $#final) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | 39 | ▶ | if (!@repacked) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | 6 | push @repacked, $final[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | 33 | my $curr_line = $final[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | 33 | my $prev_line = $repacked[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | # Try to append current line to previous if both fit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | # Add a space separator only when needed (prev ends with word-char AND | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | # curr starts with word-char), e.g. inside qw() where splitting consumed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | # the separating space. In most other cases (; boundary, ) boundary), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | # no space is needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 33 | ▶ | my $sep = ($prev_line =~ /\w$/ && $curr_line =~ /^\w/) ? ' ' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$prev_line =~ /\w$/u and $curr_line =~ /^\w/u
MC/DC50%1/2
$prev_line =~ /\w$/u and $curr_line =~ /^\w/u
$prev_line =~ /\w$/u
$curr_line =~ /^\w/u
Truth table66%2/3
A and B
A= $prev_line =~ /\w$/uB= $curr_line =~ /^\w/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | 33 | my $cand = $prev_line . $sep . $curr_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | 33 | ▶ | if (length($cand) <= $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | 1 | $repacked[-1] = $cand; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | ▶ | } elsif (length($curr_line) < $width / 3 && length($prev_line) > $width * 0.75) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
length $curr_line < $width / 3 and length $prev_line > $width * "0.75"
MC/DC50%1/2
length $curr_line < $width / 3 and length $prev_line > $width * "0.75"
length $curr_line < $width / 3
length $prev_line > $width * "0.75"
Truth table66%2/3
A and B
A= length $curr_line < $width / 3B= length $prev_line > $width * "0.75"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | # Current line is quite short and previous line is quite full. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | # Try to rebalance by moving content from previous to current. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | 4 | my $last_space = rindex($prev_line, ' '); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 4 | ▶ | if ($last_space > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | # Check if this space is inside a string literal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | 4 | my @prev_unsafe = _local_unsafe_ranges($prev_line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 4 | my $space_is_safe = !_in_any_range($last_space, \@prev_unsafe); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 4 | ▶ | if ($space_is_safe) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | 3 | my $move_part = substr($prev_line, $last_space + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | 3 | my $new_curr = $move_part . ' ' . $curr_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | # Only rebalance if new current line fits within width | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | # and is at least as long as the current line (no worse than before) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | 3 | ▶ | if (length($new_curr) <= $width && length($new_curr) >= length($curr_line)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $new_curr <= $width and length $new_curr >= length $curr_line
MC/DC0%0/2
length $new_curr <= $width and length $new_curr >= length $curr_line
length $new_curr <= $width
length $new_curr >= length $curr_line
Truth table33%1/3
A and B
A= length $new_curr <= $widthB= length $new_curr >= length $curr_line
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | 3 | $repacked[-1] = substr($prev_line, 0, $last_space); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | 3 | push @repacked, $new_curr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | 0 | push @repacked, $curr_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | 1 | push @repacked, $curr_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | 0 | push @repacked, $curr_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | # Try splitting curr_line at its first semicolon (that is not at the very | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | # end). This handles the case where greedy packing merged multiple statements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | # onto one line, making it too long to merge with prev_line. Splitting at a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | # statement boundary keeps code intact and avoids breaking inside qw(). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | 28 | my $semi_pos = index($curr_line, ';'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | 28 | ▶ | if ($semi_pos > 0 && $semi_pos < length($curr_line) - 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$semi_pos > 0 and $semi_pos < length($curr_line) - 1
MC/DC100%2/2
$semi_pos > 0 and $semi_pos < length($curr_line) - 1
$semi_pos > 0
$semi_pos < length($curr_line) - 1
Truth table100%3/3
A and B
A= $semi_pos > 0B= $semi_pos < length($curr_line) - 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | 7 | my $prefix = substr($curr_line, 0, $semi_pos + 1); # include the ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | 7 | my $suffix = substr($curr_line, $semi_pos + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | 7 | my $cand_split = $prev_line . $sep . $prefix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | 7 | ▶ | if (length($cand_split) <= $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | 1 | $repacked[-1] = $cand_split; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | 1 | push @repacked, $suffix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | 27 | push @repacked, $curr_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | # Join lines, handling string literals that span line breaks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | 6 | my @result_lines; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | 6 | for my $i (0 .. $#repacked) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 38 | ▶ | if ($i == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 6 | push @result_lines, $repacked[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | 32 | my $prev = $result_lines[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | 32 | my $curr = $repacked[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | # Check if prev ends with an unclosed string that continues in curr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | 32 | my $prev_ends_in_string = _line_ends_in_unclosed_string($prev); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | 32 | my $curr_starts_continuation = _line_starts_with_string_continuation($curr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | 32 | ▶ | if ($prev_ends_in_string && $curr_starts_continuation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$prev_ends_in_string and $curr_starts_continuation
MC/DC0%0/2
$prev_ends_in_string and $curr_starts_continuation
$prev_ends_in_string
$curr_starts_continuation
Truth table33%1/3
A and B
A= $prev_ends_in_stringB= $curr_starts_continuation
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | # Need to close the string, add concat, newline, and reopen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | 0 | my $quote = $prev_ends_in_string; # '"' or "'" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | 0 | $result_lines[-1] = $prev . $quote . '.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | 0 | push @result_lines, $quote . $curr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | 32 | push @result_lines, $curr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | 6 | return join "\n", @result_lines; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | # Find byte offset of the first occurrence of $line within $text starting at $start. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | # Used to map a packed line back into the serialized text so we can consult | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | # unsafe_ranges. We track absolute positions as we build segments. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | 10 | ▶ | sub _split_line_safe { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_line_safe: called CC=13 SCAR=25.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | 10 | my ($line, $width, $ranges) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | # Enumerate space offsets within $line; mark each as safe/unsafe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | 10 | my @safe_offsets; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | 10 | ▶ | for (my $i = 0; $i < length($line); $i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | 922 | ▶ | next unless substr($line, $i, 1) eq ' '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | # In _wrap we don't carry the absolute text offset here, but the unsafe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | # information we care about is localized: a space that sits inside a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | # string/regex literal on this very line. Re-detect that by walking the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | # line with a lightweight state machine rather than relying on the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | # serialized-text mask. (The mask is hard to align after newline joins.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | 55 | push @safe_offsets, $i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | # Filter out safe offsets that fall inside any unsafe substring of $line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | 10 | my @locally_unsafe = _local_unsafe_ranges($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | 10 | @safe_offsets = grep { !_in_any_range($_, \@locally_unsafe) } @safe_offsets; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | 10 | ▶ | return $line unless @safe_offsets; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | # Greedy pack at safe spaces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | 7 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | 7 | my $start = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | 7 | for my $si (@safe_offsets) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | 35 | my $segment = substr($line, $start, $si - $start); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | 35 | ▶ | if (!@out) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | 7 | push @out, $segment; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | 7 | $start = $si + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | 7 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | 28 | my $cand = $out[-1] . ' ' . $segment; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | 28 | ▶ | if (length($cand) <= $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | 24 | $out[-1] = $cand; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | 4 | push @out, $segment; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | 28 | $start = $si + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | 7 | my $tail = substr($line, $start); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | 7 | ▶ | if (!@out) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | 0 | push @out, $tail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | } elsif (length($out[-1]) + 1 + length($tail) <= $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | 2 | $out[-1] .= ' ' . $tail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | 5 | push @out, $tail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | # Join segments, but handle string literals that span across breaks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | 7 | my @final_out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | 7 | for my $i (0 .. $#out) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | 16 | ▶ | if ($i == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | 7 | push @final_out, $out[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | 7 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | 9 | my $prev = $final_out[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | 9 | my $curr = $out[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | # Check if prev ends with an unclosed string that continues in curr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | 9 | my $prev_ends_in_string = _line_ends_in_unclosed_string($prev); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | 9 | my $curr_starts_string = _line_starts_with_string_continuation($curr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | 9 | ▶ | if ($prev_ends_in_string && $curr_starts_string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$prev_ends_in_string and $curr_starts_string
MC/DC0%0/2
$prev_ends_in_string and $curr_starts_string
$prev_ends_in_string
$curr_starts_string
Truth table33%1/3
A and B
A= $prev_ends_in_stringB= $curr_starts_string
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | # Need to close the string, add concat, and reopen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | # Determine quote type from prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | 0 | my $quote = $prev_ends_in_string; # '"' or "'" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | 0 | $final_out[-1] = $prev . $quote . '.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | 0 | push @final_out, $quote . $curr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | 9 | push @final_out, $curr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | 7 | return join "\n", @final_out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | # Walk $line with a quote/regex-aware state machine and return [start,end) ranges | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | # whose bytes fall inside a string/regex literal. Conservative: errs on marking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | # MORE bytes unsafe, which means fewer breaks -> safer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | 14 | ▶ | sub _local_unsafe_ranges { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_local_unsafe_ranges: called CC=49 SCAR=70.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | 14 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | 14 | my @ranges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | # We track opens for: " ' / q() qq() qw() qx() m// s/// tr/// y/// and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | # escape sequences such as \e \x . The safe approach is to flag any byte | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | # that is inside a pairing quote/regex construct. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | 14 | my $len = length($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | 14 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | 14 | ▶ | while ($i < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | 884 | my $ch = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | # Double- or single-quoted string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | 884 | ▶ | if ($ch eq '"' || $ch eq "'") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ch eq "\"" or $ch eq "'"
MC/DC100%2/2
$ch eq "\"" or $ch eq "'"
$ch eq "\""
$ch eq "'"
Truth table100%3/3
A or B
A= $ch eq "\""B= $ch eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | 12 | my $q = $ch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | 12 | my $s = $i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | 12 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | 12 | ▶ | while ($i < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | 307 | my $c = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | 307 | ▶ | if ($c eq '\\' && $i + 1 < $len) { $i += 2; next; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c eq "\\" and $i + 1 < $len
MC/DC50%1/2
$c eq "\\" and $i + 1 < $len
$c eq "\\"
$i + 1 < $len
Truth table66%2/3
A and B
A= $c eq "\\"B= $i + 1 < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | 299 | ▶ | if ($c eq $q) { $i++; last; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | 287 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | 12 | push @ranges, [ $s, $i ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | 12 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | # Regex match m// or substitution s/// or transliteration tr///y/// -> flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | # entire body up to closing delimiters. Conservative: scan to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | # matching delimiter, allowing paired delims ( s{...}{...} ). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | 872 | ▶ | if ($ch =~ m{[msy]} && $i + 1 < $len && substr($line, $i + 1, 1) =~ /[ \t]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$ch =~ /[msy]/u and $i + 1 < $len and substr($line, $i + 1, 1) =~ /[ \t]/u
$ch =~ /[msy]/u and $i + 1 < $len
MC/DC0%0/3
$ch =~ /[msy]/u and $i + 1 < $len and substr($line, $i + 1, 1) =~ /[ \t]/u
$ch =~ /[msy]/u
$i + 1 < $len
substr($line, $i + 1, 1) =~ /[ \t]/u
Truth table50%2/4
A and B and C
A= $ch =~ /[msy]/uB= $i + 1 < $lenC= substr($line, $i + 1, 1) =~ /[ \t]/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | # Skip whitespace, then look for opening delim. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | 0 | my $j = $i + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | 0 | ▶ | while ($j < $len && substr($line, $j, 1) =~ /[ \t]/) { $j++; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$j < $len and substr($line, $j, 1) =~ /[ \t]/u
MC/DC0%0/2
$j < $len and substr($line, $j, 1) =~ /[ \t]/u
$j < $len
substr($line, $j, 1) =~ /[ \t]/u
Truth table0%0/3
A and B
A= $j < $lenB= substr($line, $j, 1) =~ /[ \t]/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | 0 | my $body_start = $j; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | 0 | ▶ | my $delim = $j < $len ? substr($line, $j, 1) : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | 0 | ▶ | if ($delim =~ /[{\[(<]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | 0 | ▶ | my $close = $delim eq '{' ? '}' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | $delim eq '[' ? ']' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | $delim eq '(' ? ')' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | $delim eq '<' ? '>' : $delim; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | 0 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | 0 | ▶ | while ($j < $len && $depth > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$j < $len and $depth > 0
MC/DC0%0/2
$j < $len and $depth > 0
$j < $len
$depth > 0
Truth table0%0/3
A and B
A= $j < $lenB= $depth > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | 0 | my $c = substr($line, $j, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | 0 | ▶ | if ($c eq $delim) { $depth++; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | 0 | elsif ($c eq $close) { $depth--; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | 0 | push @ranges, [ $i, $j ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | 0 | $i = $j; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | } elsif ($delim ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | 0 | ▶ | while ($j < $len && substr($line, $j, 1) ne $delim) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$j < $len and substr($line, $j, 1) ne $delim
MC/DC0%0/2
$j < $len and substr($line, $j, 1) ne $delim
$j < $len
substr($line, $j, 1) ne $delim
Truth table0%0/3
A and B
A= $j < $lenB= substr($line, $j, 1) ne $delim
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | 0 | ▶ | $j++ if substr($line, $j, 1) eq '\\' && $j + 1 < $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
substr($line, $j, 1) eq "\\" and $j + 1 < $len
MC/DC0%0/2
substr($line, $j, 1) eq "\\" and $j + 1 < $len
substr($line, $j, 1) eq "\\"
$j + 1 < $len
Truth table0%0/3
A and B
A= substr($line, $j, 1) eq "\\"B= $j + 1 < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | 0 | ▶ | $j++ if $j < $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | # For s/// and tr/// there is a second section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | 0 | ▶ | if ($ch eq 's' || $ch eq 'y' || ($ch eq 't' && substr($line,$i,2) eq 'tr')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$ch eq "s" or $ch eq "y" or $ch eq "t" and substr($line, $i, 2) eq "tr"
$ch eq "s" or $ch eq "y"
$ch eq "t" and substr($line, $i, 2) eq "tr"
MC/DC0%0/4
$ch eq "s" or $ch eq "y" or $ch eq "t" and substr($line, $i, 2) eq "tr"
$ch eq "s"
$ch eq "y"
$ch eq "t"
substr($line, $i, 2) eq "tr"
Truth table0%0/5
A or B or C and D
A= $ch eq "s"B= $ch eq "y"C= $ch eq "t"D= substr($line, $i, 2) eq "tr"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | 0 | ▶ | if ($j <= $len && $j < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$j <= $len and $j < $len
MC/DC0%0/2
$j <= $len and $j < $len
$j <= $len
$j < $len
Truth table0%0/3
A and B
A= $j <= $lenB= $j < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | 0 | my $d2 = substr($line, $j, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | 0 | ▶ | if ($d2 =~ /[{\[(<]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | 0 | ▶ | my $close2 = $d2 eq '{' ? '}' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | $d2 eq '[' ? ']' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | $d2 eq '(' ? ')' : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | $d2 eq '<' ? '>' : $d2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | 0 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | 0 | ▶ | while ($j < $len && $depth > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$j < $len and $depth > 0
MC/DC0%0/2
$j < $len and $depth > 0
$j < $len
$depth > 0
Truth table0%0/3
A and B
A= $j < $lenB= $depth > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | 0 | my $c = substr($line, $j, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | 0 | ▶ | if ($c eq $d2) { $depth++; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | 0 | elsif ($c eq $close2) { $depth--; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | 0 | ▶ | while ($j < $len && substr($line, $j, 1) ne $d2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$j < $len and substr($line, $j, 1) ne $d2
MC/DC0%0/2
$j < $len and substr($line, $j, 1) ne $d2
$j < $len
substr($line, $j, 1) ne $d2
Truth table0%0/3
A and B
A= $j < $lenB= substr($line, $j, 1) ne $d2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | 0 | ▶ | $j++ if substr($line, $j, 1) eq '\\' && $j + 1 < $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
substr($line, $j, 1) eq "\\" and $j + 1 < $len
MC/DC0%0/2
substr($line, $j, 1) eq "\\" and $j + 1 < $len
substr($line, $j, 1) eq "\\"
$j + 1 < $len
Truth table0%0/3
A and B
A= substr($line, $j, 1) eq "\\"B= $j + 1 < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | 0 | $j++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | 0 | ▶ | $j++ if $j < $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | 0 | push @ranges, [ $i, $j ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | 0 | $i = $j; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | # Bare regex /.../ - hard to distinguish from division reliably; only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | # treat as regex if followed by content that obviously is one. Skip | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | # this heuristic rather than risk mis-flagging division. (Default: not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | # flagged - relies on _is_unsafe_class from PPI mask, which already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | # covers PPI::Token::Regexp::Match in the token-walk pass.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | # qw( ... ) is intentionally NOT flagged as unsafe: qw() items are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | # whitespace-separated and an inserted "\n" is treated identically | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | # to a space, so splitting inside qw() is always safe. Skip the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | # qw-block detection entirely so its interior spaces remain breakable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | 872 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | 14 | return @ranges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | 59 | ▶ | sub _in_any_range { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_in_any_range: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | 59 | my ($pos, $ranges) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | 59 | for my $r (@$ranges) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | 76 | ▶ | return 1 if $pos >= $r->[0] && $pos < $r->[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pos >= $r->[0] and $pos < $r->[1]
MC/DC100%2/2
$pos >= $r->[0] and $pos < $r->[1]
$pos >= $r->[0]
$pos < $r->[1]
Truth table100%3/3
A and B
A= $pos >= $r->[0]B= $pos < $r->[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | 38 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | # Check if a line ends with an unclosed string literal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | # Returns the quote character ('"' or "'") if unclosed, undef otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | 41 | ▶ | sub _line_ends_in_unclosed_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_line_ends_in_unclosed_string: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | 41 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | # Count unescaped quotes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | 41 | my $in_string = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | 41 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | 41 | ▶ | while ($i < length($line)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | 1806 | my $ch = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | 1806 | ▶ | if (!$in_string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | 1603 | ▶ | if ($ch eq '"' || $ch eq "'") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ch eq "\"" or $ch eq "'"
MC/DC100%2/2
$ch eq "\"" or $ch eq "'"
$ch eq "\""
$ch eq "'"
Truth table100%3/3
A or B
A= $ch eq "\""B= $ch eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | 22 | $in_string = $ch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | 203 | ▶ | if ($ch eq '\\') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | 8 | $i++; # Skip next char | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | } elsif ($ch eq $in_string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | 22 | $in_string = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | 1806 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | 41 | ▶ | return $in_string || undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$in_string || undef
MC/DC0%0/1
$in_string || undef
$in_string
Truth table50%1/2
A
A= $in_string
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | # Check if a line starts with content that would be inside a string if the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | # previous line had an unclosed string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | 41 | ▶ | sub _line_starts_with_string_continuation { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_line_starts_with_string_continuation: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | 41 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | # If the line starts with characters that would be valid inside a string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | # followed by a closing quote, it's likely a continuation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | # This is a heuristic: we check if there's a quote near the start. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | 41 | ▶ | return 1 if $line =~ /^[^\\"']*["']/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | 24 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||