blib/lib/Mojolicious/Plugin/MoreUtilHelpers.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 85.7% 36 / 42 Conditioncond 84.3% 27 / 32 MC/DCmcdc 68.4% 13 / 19 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 91.0% 192 / 211 CC 37 SCAR 19.5
CC
37
Cov
93%
__ANON__
13.0
__ANON__
9.1
__ANON__
6.0
CRAP 7.0
↑ Summary
1 package Mojolicious::Plugin::MoreUtilHelpers;
2  
3 7 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Lingua::EN::Inflect;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our $VERSION = '0.06';
11  
12 7 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
13 7 my ($self, $app, $defaults) = @_;
14  
15 5 ▶ $app->helper(count => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
16 5 my ($c, $item, $type) = @_;
17 5 my $count = $item;
18 5 ▶ return unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 5 5
19  
20 5 ▶ my $tr = sub { lc( (split /::/, ref(shift))[-1] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 5 ▶ if(ref($item) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq "ARRAY") 5 0 5
23 5 $count = @$item;
24 5 ▶ $type = $tr->($item->[0]) unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 2 3 5
25 }
26  
27 5 ▶ $type ||= $tr->($item);
Condition33%1/3
$type ||= &$tr($item)
l!l&&r!l&&!r
500
MC/DC0%0/2
$type ||= &$tr($item)
$type &$tr($item)
Truth table33%1/3
A ||= B
A= $typeB= &$tr($item)
ABresult
1X1
011
000
28 5 return "$count " . Lingua::EN::Inflect::PL($type, $count);
29 7 });
30  
31 5 ▶ $app->helper(paragraphs => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
32 5 my ($c, $text) = @_;
33 5 ▶ return unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 5 5
34  
35 5 my $html = join '', map $c->tag('p', $_), split /^\s*\015?\012/m, $text;
36 5 return $c->b($html);
37 7 });
38  
39  
40 7 my $maxwords = $defaults->{maxwords};
41 7 ▶ $app->helper(maxwords => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
42 7 my $c = shift;
43 7 my $text = shift;
44 7 ▶ my $n = shift // $maxwords->{max};
Condition66%2/3
shift() // $maxwords->{"max"}
l!l&&r!l&&!r
610
MC/DC0%0/2
shift() // $maxwords->{"max"}
shift() $maxwords->{"max"}
Truth table66%2/3
A // B
A= shift()B= $maxwords->{"max"}
ABresult
1X1
011
000
45  
46 7 ▶ return $text unless $text and $n and $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $text and $n and $n > 0 2 5 7
Condition83%5/6
$text and $n and $n > 0
!ll&&!rl&&r
115
$text and $n
!ll&&!rl&&r
016
MC/DC66%2/3
$text and $n and $n > 0
$text $n $n > 0
Truth table75%3/4
A and B and C
A= $textB= $nC= $n > 0
ABCresult
0XX0
10X0
1100
1111
47  
48 5 ▶ my $omited = shift // $maxwords->{omit} // '...';
Condition100%5/5
(shift() // $maxwords->{"omit"}) // "..."
l!l
32
shift() // $maxwords->{"omit"}
l!l&&r!l&&!r
212
MC/DC100%2/2
(shift() // $maxwords->{"omit"}) // "..."
shift() $maxwords->{"omit"}
Truth table100%3/3
A // B
A= shift()B= $maxwords->{"omit"}
ABresult
1X1
011
000
49 5 my @words = split /\s+/, $text;
50 5 ▶ return $text unless @words > $n;
Branch: 1 branch
BranchTrueFalseTotal
unless @words > $n 1 4 5
51  
52 4 $text = join ' ', @words[0..$n-1];
53  
54 4 ▶ if(@words > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words > $n) 4 0 4
55 4 $text =~ s/[[:punct:]]$//;
56 4 $text .= $omited;
57 }
58  
59 4 return $text;
60 7 });
61  
62 7 my $sanitize = $defaults->{sanitize};
63 5 ▶ $app->helper(sanitize => sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
64 5 my $c = shift;
65 5 my $html = shift;
66 5 ▶ return unless $html;
Branch: 1 branch
BranchTrueFalseTotal
unless $html 0 5 5
67  
68 5 my %options = @_;
69  
70 5 my (%tags, %attr);
71 5 ▶ my $names = $options{tags} // $sanitize->{tags};
Condition100%3/3
$options{"tags"} // $sanitize->{"tags"}
l!l&&r!l&&!r
311
MC/DC100%2/2
$options{"tags"} // $sanitize->{"tags"}
$options{"tags"} $sanitize->{"tags"}
Truth table100%3/3
A // B
A= $options{"tags"}B= $sanitize->{"tags"}
ABresult
1X1
011
000
72 5 ▶ @tags{@$names} = (1) x @$names if ref $names eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $names eq "ARRAY" 4 1 5
73  
74 5 ▶ $names = $options{attr} // $sanitize->{attr};
Condition100%3/3
$options{"attr"} // $sanitize->{"attr"}
l!l&&r!l&&!r
212
MC/DC100%2/2
$options{"attr"} // $sanitize->{"attr"}
$options{"attr"} $sanitize->{"attr"}
Truth table100%3/3
A // B
A= $options{"attr"}B= $sanitize->{"attr"}
ABresult
1X1
011
000
75 5 ▶ @attr{@$names} = (1) x @$names if ref $names eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $names eq "ARRAY" 3 2 5
76  
77 5 my $doc = Mojo::DOM->new($html);
78 5 ▶ if (! %tags) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%tags) 1 4 5
79 1 my $txt = $doc->all_text;
80 1 $txt =~ s/\s+/ /g;
81 1 $txt =~ s/^ //;
82 1 $txt =~ s/ $//;
83 1 return $txt;
84 }
85  
86 4 for my $node (@{$doc->descendant_nodes}) {
87 44 ▶ if($node->tag && !$tags{ $node->tag }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->tag and not $tags{$node->tag}) 10 34 44
Condition100%3/3
$node->tag and not $tags{$node->tag}
!ll&&!rl&&r
28610
MC/DC100%2/2
$node->tag and not $tags{$node->tag}
$node->tag not $tags{$node->tag}
Truth table100%3/3
A and not(B)
A= $node->tagB= not $tags{$node->tag}
ABresult
0X0
100
111
88 10 $node->strip;
89 10 next;
90 }
91  
92 34 ▶ if(%attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (%attr) 21 13 34
93 21 for my $name (keys %{$node->attr}) {
94 4 ▶ delete $node->attr->{$name} unless $attr{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{$name} 1 3 4
95 }
96 }
97 }
98  
99 4 return $c->b($doc->to_string);
100 7 });
101  
102 7 ▶ $app->helper(trim_param => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
103 7 my $c = shift;
104 7 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 7 7
105  
106 10 ▶ my $trim = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
107 10 my $name = shift;
108 10 my $vals = $c->every_param($name);
109  
110 10 ▶ $c->param($name => @$vals == 1 ?
Branch: 1 branch
BranchTrueFalseTotal
@$vals == 1 ? : 8 2 10
111 Mojo::Util::trim($vals->[0]) :
112 [ map Mojo::Util::trim($_), @$vals ]);
113 7 };
114  
115 7 my %params;
116 7 my $names = $c->req->params->names;
117 7 @params{ @$names } = (1) x @$names;
118  
119 7 for my $name (@_) {
120 10 ▶ if(ref($name) ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name ne "Regexp") 6 4 10
121 6 $trim->($name);
122 6 next;
123 }
124  
125 4 for(keys %params) {
126 7 ▶ next unless $_ =~ $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$name/u 3 4 7
127  
128 4 $trim->($_);
129 4 delete $params{$_};
130 }
131 }
132 7 });
133  
134 3 ▶ $app->helper(collection => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
135 3 my $c = shift;
136 3 ▶ my @data = ( @_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 1 2 3
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
111
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
137 3 ▶ return Mojo::Collection->new(@data == 1 && !defined $data[0] ? () : @data );
Branch: 1 branch
BranchTrueFalseTotal
@data == 1 && !defined($data[0]) ? : 1 2 3
Condition66%2/3
@data == 1 and not defined $data[0]
!ll&&!rl&&r
201
MC/DC50%1/2
@data == 1 and not defined $data[0]
@data == 1 not defined $data[0]
Truth table66%2/3
A and not(B)
A= @data == 1B= not defined $data[0]
ABresult
0X0
100
111
138 7 });
139  
140 7 ▶ if($defaults->{collection}->{patch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults->{"collection"}{"patch"}) 1 6 7
141 0 ▶ $app->helper(c => sub { shift->collection(@_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
142 }
143  
144 }
145  
146 7 1;
147  
148 __END__
↑ Summary