lib/HTML/FormFu/Localize.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 72.7% 16 / 22 Conditioncond 41.6% 5 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 7 totaltotal 78.6% 118 / 150 SCAR 29.6
CC
18
Cov
84%
localize
11.3
add_localize_object
7.6
get_localize_object
2.3
CRAP 19.3
« lib/HTML/FormFu/Constraint/File/Size.pm ↑ Summary lib/HTML/FormFu/Constraint/MinMaxFields.pm »
1 403 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Localize;
4 403 $HTML::FormFu::Localize::VERSION = '2.06';
5 403 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 403 ▶use HTML::FormFu::Util qw( require_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 403 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 403 ▶use List::MoreUtils qw( pairwise );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 403 ▶use Scalar::Util qw( weaken isweak blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 403 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 403 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 403 our @EXPORT = qw(
15 localize
16 add_localize_object
17 add_localize_object_from_class
18 get_localize_object_from_class
19 get_localize_object_dies_on_missing_key
20 add_default_localize_object
21 get_localize_object
22 );
23  
24 sub localize {
25 62 ▶ my ( $self, @original_strings ) = @_;
Subroutine localize: called CC=10 SCAR=24.3
Pod undocumented
26  
27 62 @original_strings = grep {defined} @original_strings;
28  
29 62 ▶ if ( !$self->{has_default_localize_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"has_default_localize_object"}) 41 21 62
30 41 $self->add_default_localize_object;
31 }
32  
33 62 my @localized_strings;
34  
35 62 foreach my $localize_data ( @{ $self->{localize_data} } ) {
36 64 my $localize_object = $self->get_localize_object($localize_data);
37  
38 64 eval {
39 64 @localized_strings = $localize_object->localize(@original_strings);
40 };
41  
42 64 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 57 64
43  
44 # NOTE:
45 # As FormFu uses L10N to return messages based on artificial message
46 # ids (instead of english language as message ids) the assumption
47 # that we just got a result from Locale::Maketext with AUTO = 1 seems
48 # to be safe when localize returns the same string as handed over.
49 57 ▶ if ( !$localize_data->{dies_on_missing_key}
Branch: 1 branch
BranchTrueFalseTotal
if (not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings and scalar &any(sub { not $_; } , &pairwise(sub { $a eq $b; } , \@original_strings, \@localized_strings)) == 0) 0 57 57
Condition33%2/6
not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings and scalar &any(sub {
!ll&&!rl&&r
5700
not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings
!ll&&!rl&&r
5700
MC/DC0%0/3
not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings and scalar &any(sub { not $_; } , &pairwise(sub { $a eq $b; } , \@original_strings, \@localized_strings)) == 0
not $localize_data->{"dies_on_missing_key"} scalar @original_strings == scalar @localized_strings scalar &any(sub { not $_; } , &pairwise(sub { $a eq $b; } , \@original_strings, \@localized_strings)) == 0
Truth table25%1/4
not(A) and B and C
A= not $localize_data->{"dies_on_missing_key"}B= scalar @original_strings == scalar @localized_stringsC= scalar &any(sub { not $_; } , &pairwise(sub { $a eq $b; } , \@original_strings, \@localized_strings)) == 0
ABCresult
0XX0
10X0
1100
1111
50 && scalar(@original_strings) == scalar(@localized_strings)
51 0 ▶ && scalar( any { !$_ } pairwise { $a eq $b } @original_strings,
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 @localized_strings ) == 0
53 )
54 {
55 0 next;
56 }
57  
58 57 ▶ last if @localized_strings;
Branch: 1 branch
BranchTrueFalseTotal
if @localized_strings 57 0 57
59 }
60  
61 62 ▶ if ( !@localized_strings ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@localized_strings) 5 57 62
62 5 @localized_strings = @original_strings;
63 }
64  
65 62 ▶ return wantarray ? @localized_strings : $localized_strings[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 48 62
66 }
67  
68 sub add_localize_object {
69 2 ▶ my ( $self, @objects ) = @_;
Subroutine add_localize_object: called CC=7 SCAR=20.3
Pod undocumented
70  
71 2 ▶ croak 'no arguments given' if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 2 2
72  
73 2 foreach my $localize_object (@objects) {
74 2 my $dies_on_missing_key = undef;
75  
76 2 ▶ if ( blessed $localize_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($localize_object)) 2 0 2
77 2 $dies_on_missing_key
78 = $self->get_localize_object_dies_on_missing_key(
79 $localize_object);
80 }
81  
82 # add external localize object to the end of the list
83 2 push @{ $self->{localize_data} },
84 {
85 localize_object => $localize_object,
86 dies_on_missing_key => $dies_on_missing_key,
87 };
88  
89 2 ▶ if ( !exists $self->{weaken_localize_object}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'weaken_localize_object'} or $self->{'weaken_localize_object'} != 0 and not builtin::is_weak(@{$$self{'localize_data'};}[-1]->{'localize_object'})) { } 1 1 2
Condition50%3/6
not exists $self->{"weaken_localize_object"} or $self->{"weaken_localize_object"} != 0 and not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
l!l&&r!l&&!r
101
$self->{"weaken_localize_object"} != 0 and not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
!ll&&!rl&&r
100
MC/DC33%1/3
not exists $self->{"weaken_localize_object"} or $self->{"weaken_localize_object"} != 0 and not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
not exists $self->{"weaken_localize_object"} $self->{"weaken_localize_object"} != 0 not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
Truth table50%2/4
not(A) or B and not(C)
A= not exists $self->{"weaken_localize_object"}B= $self->{"weaken_localize_object"} != 0C= not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
ABCresult
1XX1
0111
00X0
0100
90 || $self->{weaken_localize_object} != 0
91 0 && !isweak @{ $self->{localize_data} }[-1]->{localize_object} )
92 {
93 1 weaken @{ $self->{localize_data} }[-1]->{localize_object};
94 }
95 else {
96 1 delete $self->{weaken_localize_object};
97 }
98 }
99  
100 2 return $self;
101 }
102  
103 sub add_localize_object_from_class {
104 1 ▶ my ( $self, @class ) = @_;
Subroutine add_localize_object_from_class: called CC=1 SCAR=0.0
Pod undocumented
105  
106 1 $self->{weaken_localize_object} = 0;
107 return $self->add_localize_object(
108 1 map { $self->get_localize_object_from_class($_) } @class );
109 }
110  
111 sub get_localize_object_from_class {
112 42 ▶ my ( $self, $class ) = @_;
Subroutine get_localize_object_from_class: called CC=1 SCAR=0.0
Pod undocumented
113  
114 42 require_class($class);
115  
116 42 my $languages = $self->languages;
117  
118 42 return $class->get_handle(@$languages);
119  
120 }
121  
122 sub get_localize_object_dies_on_missing_key {
123 2 ▶ my ( $self, $localize_object ) = @_;
Subroutine get_localize_object_dies_on_missing_key: called CC=2 SCAR=7.0
Pod undocumented
124  
125 # NOTE:
126 # Findout how this class reacts on missing entries
127 # this is an issue with catalyst and po-style localization
128 # (in pm-style localization, you could set
129 # $Hello::I18N::en::Lexicon{_AUTO} = 0;
130 # to avoid autocreating missing keys)
131  
132 # HINT:
133 # Never use underscores for the beginning of the testkey as they
134 # will lead Locale::Maketext to croak even if _AUTO is on (1) as
135 # Locale::Maketext useses underscores to identify text for
136 # processing via the AUTO-function (_compile).
137  
138 2 my $testkey = 'html_formfu_missing_key_test';
139  
140 2 eval { $localize_object->localize($testkey) };
141  
142 2 ▶ my $dies_on_missing_key = $@ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 0 2
143  
144 2 return $dies_on_missing_key;
145 }
146  
147 sub add_default_localize_object {
148 41 ▶ my ($self) = @_;
Subroutine add_default_localize_object: called CC=1 SCAR=0.0
Pod undocumented
149  
150 41 my $localize_object
151 = $self->get_localize_object_from_class( $self->localize_class );
152  
153 41 my $dies_on_missing_key = 1;
154  
155 # put FormFu localize object in first place
156 41 unshift @{ $self->{localize_data} },
157 {
158 localize_object => $localize_object,
159 dies_on_missing_key => $dies_on_missing_key,
160 };
161  
162 41 $self->{has_default_localize_object} = 1;
163  
164 41 return $self;
165 }
166  
167 sub get_localize_object {
168 64 ▶ my ( $self, $localize_data ) = @_;
Subroutine get_localize_object: called CC=2 SCAR=8.4
Pod undocumented
169  
170 64 ▶ if ( !blessed $localize_data->{localize_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($localize_data->{"localize_object"})) 0 64 64
171  
172 $localize_data->{localize_object}
173 0 = $self->get_localize_object_from_class( $self->localize_class );
174  
175 $localize_data->{dies_on_missing_key}
176 = $self->get_localize_object_dies_on_missing_key(
177 0 $localize_data->{localize_object} );
178 }
179  
180 64 return $localize_data->{localize_object};
181 }
182  
183 403 1;
184  
185 __END__
186  
187 =pod
188  
189 =encoding UTF-8
190  
191 =head1 NAME
192  
193 HTML::FormFu::Localize
194  
195 =head1 VERSION
196  
197 version 2.06
198  
199 =head1 AUTHOR
200  
201 Carl Franks <cpan@fireartist.com>
202  
203 =head1 COPYRIGHT AND LICENSE
204  
205 This software is copyright (c) 2018 by Carl Franks.
206  
207 This is free software; you can redistribute it and/or modify it under
208 the same terms as the Perl 5 programming language system itself.
209  
210 =cut
« lib/HTML/FormFu/Constraint/File/Size.pm ↑ Summary lib/HTML/FormFu/Constraint/MinMaxFields.pm »