Catmandu/Fix/sort_field.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran 90.0% 18 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 92.3% 84 / 91 CC 12 SCAR 20.4
CC
12
Cov
94%
__ANON__
12.2
_build_fixer
1.0
BEGIN
1.0
CRAP 7.7
« Catmandu/Fix/Has.pm ↑ Summary Catmandu/Env.pm »
1 package Catmandu::Fix::sort_field;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.2025';
6  
7 1 ▶use List::SomeUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Util::Path qw(as_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 with 'Catmandu::Fix::Builder';
14  
15 1 has path => (fix_arg => 1);
16 1 has uniq => (fix_opt => 1);
17 1 has reverse => (fix_opt => 1);
18 1 has numeric => (fix_opt => 1);
19 6 ▶has undef_position => (fix_opt => 1, default => sub {'last'});
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 12 ▶sub _build_fixer {
Subroutine _build_fixer: called CC=1 SCAR=0.0
22 12 my ($self) = @_;
23 12 my $uniq = $self->uniq;
24 12 my $reverse = $self->reverse;
25 12 my $numeric = $self->numeric;
26 12 my $undef_position = $self->undef_position;
27 as_path($self->path)->updater(
28 12 ▶ if_array_ref => sub {
Subroutine __ANON__: called CC=12 SCAR=25.0
29 12 my $val = $_[0];
30  
31 #filter out undef
32 12 my $undefs = [grep {!defined($_)} @$val];
33 12 $val = [grep {defined($_)} @$val];
34  
35 #uniq
36 12 ▶ if ($uniq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniq) 6 6 12
37 6 $val = [uniq(@$val)];
38 }
39  
40 #sort
41 12 ▶ if ($reverse && $numeric) {
Branch: 3 branches
BranchTrueFalseTotal
if ($reverse and $numeric) { } 0 12 12
elsif ($numeric) { } 1 11 12
elsif ($reverse) { } 1 10 11
Condition66%2/3
$reverse and $numeric
!ll&&!rl&&r
1110
MC/DC0%0/2
$reverse and $numeric
$reverse $numeric
Truth table66%2/3
A and B
A= $reverseB= $numeric
ABresult
0X0
100
111
42 0 $val = [sort {$b <=> $a} @$val];
43 }
44 elsif ($numeric) {
45 1 $val = [sort {$a <=> $b} @$val];
46 }
47 elsif ($reverse) {
48 1 $val = [sort {$b cmp $a} @$val];
49 }
50 else {
51 10 $val = [sort {$a cmp $b} @$val];
52 }
53  
54 #insert undef at the end
55 12 ▶ if ($undef_position eq 'first') {
Branch: 2 branches
BranchTrueFalseTotal
if ($undef_position eq 'first') { } 2 10 12
elsif ($undef_position eq 'last') { } 8 2 10
56 2 ▶ if ($uniq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniq) { } 1 1 2
57 1 ▶ unshift @$val, undef if @$undefs;
Branch: 1 branch
BranchTrueFalseTotal
if @$undefs 1 0 1
58 }
59 else {
60 1 unshift @$val, @$undefs;
61 }
62 }
63 elsif ($undef_position eq 'last') {
64 8 ▶ if ($uniq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniq) { } 4 4 8
65 4 ▶ push @$val, undef if @$undefs;
Branch: 1 branch
BranchTrueFalseTotal
if @$undefs 2 2 4
66 }
67 else {
68 4 push @$val, @$undefs;
69 }
70 }
71  
72 12 $val;
73 }
74 12 );
75 }
76  
77 1 1;
78  
79 __END__
« Catmandu/Fix/Has.pm ↑ Summary Catmandu/Env.pm »