Natural/Expand.pm

filter: Statementstmt 100% 91 / 91 Branchbran 91.6% 22 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 96.8% 124 / 128 CC 21 SCAR 24.1
CC
21
Cov
97%
_expand
17.0
__ANON__
3.0
_expand_for
3.0
CRAP 11.1
« Natural.pm ↑ Summary Natural/Formatted.pm »
1 package DateTime::Format::Natural::Expand;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use DateTime::Format::Natural::Helpers qw(%flag);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 our $VERSION = '0.04';
11  
12 my %data = (
13 time => {
14 4 => {},
15 5 => '_time',
16 6 => { truncate_to => [q(hour_minute_second)] },
17 },
18 time_min => {
19 4 => {},
20 5 => '_time',
21 6 => { truncate_to => [q(minute_second)] },
22 },
23 time_am => {
24 2 => 'meridiem',
25 3 => $flag{time_am},
26 4 => {},
27 5 => '_at',
28 6 => { truncate_to => [q(hour_minute_second)] },
29 },
30 time_pm => {
31 2 => 'meridiem',
32 3 => $flag{time_pm},
33 27 4 => {},
34 5 => '_at',
35 6 => { truncate_to => [q(hour_minute_second)] },
36 },
37 );
38  
39 27 my %expand_prefix = (
40 date_literal_variant => [ qw( time_min time_am time_pm) ],
41 week_variant => [ qw( time_min time_am time_pm) ],
42 month_variant => [ qw( time_min time_am time_pm) ],
43 year_variant => [ qw( time_min time_am time_pm) ],
44 weekday_variant_week => [ qw(time time_am time_pm) ],
45 variant_week_weekday => [ qw(time time_am time_pm) ],
46 final_weekday_in_month => [ qw(time time_am time_pm) ],
47 month_day => [ qw( time_min time_am time_pm) ],
48 day_month_variant_year => [ qw( time_min time_am time_pm) ],
49 day_month_year_ago => [ qw( time_min time_am time_pm) ],
50 count_weekday => [ qw( time_min time_am time_pm) ],
51 count_yearday => [ qw( time_min time_am time_pm) ],
52 count_weekday_from_now => [ qw( time_min time_am time_pm) ],
53 count_day_variant_week => [ qw( time_min time_am time_pm) ],
54 count_day_variant_month => [ qw( time_min time_am time_pm) ],
55 count_month_variant_year => [ qw( time_min time_am time_pm) ],
56 count_weekday_variant_month => [ qw( time_min time_am time_pm) ],
57 count_weekday_in_month => [ qw( time_min time_am time_pm) ],
58 count_yearday_variant_year => [ qw( time_min time_am time_pm) ],
59 );
60 27 my %expand_suffix = (
61 date_literal_variant => [ qw( time_min time_am time_pm) ],
62 week_variant => [ qw( time_min time_am time_pm) ],
63 month_variant => [ qw( time_min time_am time_pm) ],
64 year_variant => [ qw( time_min time_am time_pm) ],
65 weekday_variant_week => [ qw(time time_am time_pm) ],
66 variant_week_weekday => [ qw(time time_am time_pm) ],
67 final_weekday_in_month => [ qw(time time_am time_pm) ],
68 day_month_variant_year => [ qw( time_min time_am time_pm) ],
69 day_month_year_ago => [ qw( time_min time_am time_pm) ],
70 count_weekday => [ qw( time_min time_am time_pm) ],
71 count_yearday => [ qw( time_min time_am time_pm) ],
72 count_weekday_from_now => [ qw( time_min time_am time_pm) ],
73 count_day_variant_week => [ qw( time_min time_am time_pm) ],
74 count_day_variant_month => [ qw( time_min time_am time_pm) ],
75 count_month_variant_year => [ qw( time_min time_am time_pm) ],
76 count_weekday_variant_month => [ qw( time_min time_am time_pm) ],
77 count_weekday_in_month => [ qw( time_min time_am time_pm) ],
78 count_yearday_variant_year => [ qw( time_min time_am time_pm) ],
79 );
80  
81 my $save = sub
82 592186 ▶{
Subroutine __ANON__: called CC=3 SCAR=11.0
83 592186 my ($type, $target, @values) = @_;
84  
85 592186 ▶ if ($type eq 'prefix') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'prefix') { } 312193 279993 592186
elsif ($type eq 'suffix') { } 279993 0 279993
86 312193 unshift @$target, @values;
87 }
88 elsif ($type eq 'suffix') {
89 279993 push @$target, @values;
90 }
91 27 };
92  
93 sub _expand_for
94 785232 ▶{
Subroutine _expand_for: called CC=3 SCAR=11.0
95 785232 my $self = shift;
96 785232 my ($keyword) = @_;
97  
98 785232 ▶ return (exists $expand_prefix{$keyword} || exists $expand_suffix{$keyword}) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
exists $expand_prefix{$keyword} || exists $expand_suffix{$keyword} ? : 207214 578018 785232
Condition66%2/3
exists $expand_prefix{$keyword} or exists $expand_suffix{$keyword}
l!l&&r!l&&!r
2072140578018
MC/DC50%1/2
exists $expand_prefix{$keyword} or exists $expand_suffix{$keyword}
exists $expand_prefix{$keyword} exists $expand_suffix{$keyword}
Truth table66%2/3
A or B
A= exists $expand_prefix{$keyword}B= exists $expand_suffix{$keyword}
ABresult
1X1
011
000
99 }
100  
101 sub _expand
102 15946 ▶{
Subroutine _expand: called CC=17 SCAR=28.3
103 15946 my $self = shift;
104 15946 my ($keyword, $types_entry, $grammar) = @_;
105  
106 15946 my %expand = (
107 prefix => \%expand_prefix,
108 suffix => \%expand_suffix,
109 );
110  
111 15946 my (@expandable, @expansions);
112  
113 15946 ▶ push @expandable, 'prefix' if exists $expand_prefix{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if exists $expand_prefix{$keyword} 15946 0 15946
114 15946 ▶ push @expandable, 'suffix' if exists $expand_suffix{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if exists $expand_suffix{$keyword} 13646 2300 15946
115  
116 15946 foreach my $type (@expandable) {
117 29592 my @elements = @{$expand{$type}->{$keyword}};
118  
119 29592 foreach my $element (@elements) {
120 88776 foreach my $entry (@$grammar) {
121 253794 my $types = clone($types_entry);
122  
123 253794 $save->($type, $types, 'REGEXP');
124  
125 253794 my $new = clone($entry);
126  
127 253794 ▶ if ($type eq 'prefix') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "prefix") 133797 119997 253794
128 133797 my %definition;
129 133797 ▶ while (my ($pos, $def) = each %{$new->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$new[0];}) 358266 133797 492063
130 358266 $definition{$pos + 1} = $def;
131 }
132 133797 %{$new->[0]} = %definition;
133  
134 133797 my @indexes;
135 133797 foreach my $aref (@{$new->[1]}) {
136 52731 my @tmp = map $_ + 1, @$aref;
137 52731 push @indexes, [ @tmp ];
138 }
139 133797 @{$new->[1]} = @indexes;
140  
141 133797 my @flags;
142 133797 foreach my $aref (@{$new->[3]}) {
143 140685 my @tmp;
144 140685 foreach my $value (@$aref) {
145 234762 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 182031 52731 234762
146 182031 my %hash;
147 182031 ▶ while (my ($key, $val) = each %$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$value) 182031 182031 364062
148 182031 ▶ $key++ if $key =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^\d+$/ 176796 5235 182031
149 182031 $hash{$key} = $val;
150 }
151 182031 push @tmp, { %hash };
152 }
153 else {
154 52731 push @tmp, $value + 1;
155 }
156 }
157 140685 push @flags, [ @tmp ];
158 }
159 133797 @{$new->[3]} = @flags;
160 }
161  
162 my %indexes = (
163 prefix => 0,
164 253794 suffix => scalar keys %{$new->[0]},
165 );
166  
167 253794 my $i = $indexes{$type};
168  
169 253794 $new->[0]->{$i} = $self->{data}->__RE($element);
170  
171 253794 ▶ if (exists $data{$element}->{2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$element}{2}) 169196 84598 253794
172 169196 $save->($type, $new->[1], [ $i ]);
173 169196 $save->($type, $new->[2], $self->{data}->__extended_checks($data{$element}->{2}));
174 }
175  
176 253794 ▶ push @{$new->[3]}, exists $data{$element}->{3} ? [ { $i => [ $data{$element}->{3} ] } ] : [ $i ];
Branch: 1 branch
BranchTrueFalseTotal
exists $data{$element}{'3'} ? : 169196 84598 253794
177  
178 253794 push @{$new->[4]}, $data{$element}->{4};
179 253794 push @{$new->[5]}, $data{$element}->{5};
180  
181 253794 foreach my $key (keys %{$data{$element}->{6}}) {
182 253794 push @{$new->[6]->{$key}}, @{$data{$element}->{6}->{$key}};
183 }
184  
185 253794 push @expansions, [ $types, $new ];
186 }
187 }
188 }
189  
190 15946 return @expansions;
191 }
192  
193 27 1;
194 __END__
« Natural.pm ↑ Summary Natural/Formatted.pm »