Babble/Plugin/CoreSignatures.pm

filter: Statementstmt 100% 72 / 72 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 4 totaltotal 93.5% 102 / 109 CC 11 SCAR 10.5
CC
11
Cov
99%
__ANON__
7.0
__ANON__
2.0
__ANON__
2.0
CRAP 2.9
« Babble/Grammar.pm ↑ Summary Babble/Plugin/DefinedOr.pm »
1 package Babble::Plugin::CoreSignatures;
2  
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶sub extend_grammar { } # PPR::X can already parse everything we need
Subroutine extend_grammar: not called CC=1 SCAR=0.0
Pod undocumented
7  
8 # .......bbbbbSSSSSSSa
9 # sub foo :Bar ($baz) {
10  
11 # .......bSSSSSSSaaaaa
12 # sub foo ($baz) :Bar {
13  
14 1 ▶sub transform_to_signatures {
Subroutine transform_to_signatures: called CC=1 SCAR=0.0
Pod undocumented
15 1 my ($self, $top) = @_;
16 4 ▶ my $tf = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
17 4 my $s = (my $m = shift)->submatches;
18 4 ▶ if ((my $after = $s->{after}->text) =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $after = $s->{"after"}->text) =~ /\S/) 2 2 4
19 2 $s->{after}->replace_text('');
20 2 $s->{before}->replace_text($s->{before}->text.$after);
21 }
22 1 };
23 1 $self->_transform_signatures($top, $tf);
24 }
25  
26 1 ▶sub transform_to_oldsignatures {
Subroutine transform_to_oldsignatures: called CC=1 SCAR=0.0
Pod undocumented
27 1 my ($self, $top) = @_;
28 4 ▶ my $tf = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
29 4 my $s = (my $m = shift)->submatches;
30 4 ▶ if ((my $before = $s->{before}->text) =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $before = $s->{"before"}->text) =~ /\S/) 2 2 4
31 2 $s->{before}->replace_text('');
32 2 $s->{after}->replace_text($before.$s->{after}->text);
33 }
34 1 };
35 1 $self->_transform_signatures($top, $tf);
36 }
37  
38 9 ▶sub transform_to_plain {
Subroutine transform_to_plain: called CC=1 SCAR=0.0
Pod undocumented
39 9 my ($self, $top) = @_;
40 9 $top->remove_use_argument(experimental => 'signatures');
41 9 $top->remove_use_argument('Mojo::Base' => '-signatures', 1);
42 10 ▶ my $tf = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
43 10 my $s = (my $m = shift)->submatches;
44  
45 # shift attributes after first before we go hunting for :prototype
46 10 ▶ if ((my $before = $s->{before}->text) =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $before = $s->{"before"}->text) =~ /\S/) 8 2 10
47 8 $s->{before}->replace_text('');
48 8 $s->{after}->replace_text($before.$s->{after}->text);
49 }
50  
51 10 my $proto = '';
52 {
53 10 my $try = $s->{after};
54 10 local $try->{top_rule} = 'Attributes';
55 10 my $grammar = $m->grammar->clone;
56 10 $grammar->add_rule(Attribute =>
57 '(?&PerlOWS) :? (?&PerlOWS)
58 (?&PerlIdentifier)
59 (?: (?= \( ) (?&PPR_X_quotelike_body) )?+'
60 )->replace_rule(Attributes =>
61 '(?=(?&PerlOWS):)(?&PerlAttribute)
62 (?&PerlAttribute)*'
63 );
64 10 local $try->{grammar} = $grammar;
65 10 ▶ my $each; $each = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
66 14 my ($attr) = @_;
67 14 ▶ if ($attr->text =~ /prototype(\(.*?\))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->text =~ /prototype(\(.*?\))/) 7 7 14
68 7 $proto = $1;
69 7 $attr->replace_text('');
70 2 ▶ $each = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
71 2 my ($attr) = @_;
72 2 ▶ $attr->transform_text(sub { s/^(\s*)/${1}:/ }) unless $attr->text =~ /^\s*:/;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->text =~ /^\s*:/ 2 0 2
73 2 ▶ $each = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
74 7 };
75 }
76 10 };
77 10 ▶ $try->each_match_of(Attribute => sub { $each->(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
78 10 undef($each);
79 }
80  
81 10 s/\A\s*\(//, s/\)\s*\Z// for my $sig_orig = $s->{sig}->text;
82 10 my $grammar_re = $m->grammar_regexp;
83 10 my @sig_parts = grep defined($_),
84 $sig_orig =~ /((?&PerlAssignment)) ${grammar_re}/xg;
85  
86 10 my (@sig_text, @defaults);
87  
88 10 foreach my $idx (0..$#sig_parts) {
89 9 my $part = $sig_parts[$idx];
90 9 ▶ if ($part =~ s/^(\S+?)\s*=\s*(.*?)(,$|$)/$1$3/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ s/^(\S+?)\s*=\s*(.*?)(,$|$)/$1$3/) 1 8 9
91 1 push @defaults, "$1 = $2 if \@_ <= $idx;";
92 }
93 9 push @sig_text, $part;
94 }
95  
96 10 ▶ my $sig_text =
Branch: 1 branch
BranchTrueFalseTotal
@sig_text ? : 8 2 10
97 @sig_text
98 ? 'my ('.(join ', ', @sig_text).') = @_;'
99 : '';
100 10 my $code = join ' ', $sig_text, @defaults;
101 10 ▶ $s->{body}->transform_text(sub { s/^{/{ ${code}/ });
Subroutine __ANON__: called CC=1 SCAR=0.0
102 10 ▶ if ($proto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto) { } 7 3 10
103 7 ▶ $s->{sig}->transform_text(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 7 s/\A(\s*)\(.*\)(\s*)\Z/${1}${proto}${2}/;
105 7 });
106 } else {
107 3 $s->{sig}->replace_text('');
108 }
109 9 };
110 9 $self->_transform_signatures($top, $tf);
111 }
112  
113 11 ▶sub _transform_signatures {
Subroutine _transform_signatures: called CC=1 SCAR=0.0
114 11 my ($self, $top, $tf) = @_;
115 11 my @common = (
116 '(?:', # 5.20, 5.28+
117 [ before => '(?: (?&PerlOWS) (?>(?&PerlAttributes)) )?+' ],
118 [ sig => '(?&PerlOWS) (?&PerlParenthesesList)' ], # not optional for us
119 [ after => '(?&PerlOWS)' ],
120 '|', # 5.22 - 5.26
121 [ before => '(?&PerlOWS)' ],
122 [ sig => '(?&PerlParenthesesList) (?&PerlOWS)' ], # not optional for us
123 [ after => '(?: (?>(?&PerlAttributes)) (?&PerlOWS) )?+' ],
124 ')',
125 [ body => '(?&PerlBlock)' ],
126 );
127 11 $top->each_match_within('SubroutineDeclaration' => [
128 'sub \b (?&PerlOWS) (?&PerlOldQualifiedIdentifier)',
129 @common,
130 ], $tf);
131 11 $top->each_match_within('AnonymousSubroutine' => [
132 'sub \b',
133 @common,
134 ], $tf);
135 }
136  
137 1 1;
138 __END__
« Babble/Grammar.pm ↑ Summary Babble/Plugin/DefinedOr.pm »