Gettext.pm

filter: Statementstmt 91.4% 107 / 117 Branchbran 50.0% 15 / 30 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 86.2% 25 / 29 Podpod 100% 2 / 2 totaltotal 71.2% 156 / 219 CC 25 SCAR 19.4
CC
25
Cov
74%
__ANON__
30.0
__ANON__
11.5
__ANON__
9.8
CRAP 6.9
« Gettext/Storage/MO.pm ↑ Summary Gettext/Storage.pm »
1 package File::Gettext;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use version; our $VERSION = qv( sprintf '0.33.%d', q$Rev: 1 $ =~ /\d+/gmx );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::DataClass::Constants qw( EXCEPTION_CLASS FALSE NUL SPC TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::DataClass::Functions qw( is_hashref merge_attributes throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::DataClass::IO qw( io );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::DataClass::Types qw( ArrayRef Directory HashRef Str Undef );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Gettext::Constants qw( LOCALE_DIRS );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Spec::Functions qw( tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Type::Utils qw( as coerce declare from enum via );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Unexpected::Functions qw( Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 extends q(File::DataClass::Schema);
19  
20 # Private functions
21 4 ▶my $_build_localedir = sub {
Subroutine __ANON__: called CC=7 SCAR=24.4
22 4 ▶ my $dir = shift; $dir and $dir = io( $dir ) and $dir->is_dir and return $dir;
Branch: 1 branch
BranchTrueFalseTotal
$dir and $dir = &io($dir) and $dir->is_dir and return $dir 0 4 4
Condition33%2/6
$dir and $dir = &io($dir) and $dir->is_dir
!ll&&!rl&&r
400
$dir and $dir = &io($dir)
!ll&&!rl&&r
400
MC/DC0%0/3
$dir and $dir = &io($dir) and $dir->is_dir
$dir $dir = &io($dir) $dir->is_dir
Truth table25%1/4
A and B and C
A= $dirB= $dir = &io($dir)C= $dir->is_dir
ABCresult
0XX0
10X0
1100
1111
23  
24 4 for $dir (map { io $_ } @{ LOCALE_DIRS() }) {
25 4 ▶ $dir->exists and $dir->is_dir and return $dir;
Branch: 1 branch
BranchTrueFalseTotal
$dir->exists and $dir->is_dir and return $dir 4 0 4
Condition33%1/3
$dir->exists and $dir->is_dir
!ll&&!rl&&r
004
MC/DC0%0/2
$dir->exists and $dir->is_dir
$dir->exists $dir->is_dir
Truth table33%1/3
A and B
A= $dir->existsB= $dir->is_dir
ABresult
0X0
100
111
26 }
27  
28 0 return io tmpdir();
29 2 };
30  
31 2 my $LocaleDir = declare as Directory;
32  
33 coerce $LocaleDir,
34 0 ▶ from ArrayRef, via { $_build_localedir->( $_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 4 ▶ from Str, via { $_build_localedir->( $_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 0 ▶ from Undef, via { $_build_localedir->( $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
37  
38 2 my $SourceType = enum 'SourceType' => [ 'mo', 'po' ];
39  
40 # Public attributes
41 2 has 'charset' => is => 'ro', isa => Str, default => 'iso-8859-1';
42  
43 has 'default_po_header' => is => 'ro', isa => HashRef,
44 5 ▶ default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 5 appname => 'Your_Application',
46 company => 'ExampleCom',
47 email => '<translators@example.com>',
48 lang => 'en',
49 team => 'Translators',
50 2 translator => 'Athena', } };
51  
52 2 has 'gettext_catagory' => is => 'ro', isa => Str, default => 'LC_MESSAGES';
53  
54 has 'header_key_table' => is => 'ro', isa => HashRef,
55 5 ▶ default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 5 project_id_version => [ 0, 'Project-Id-Version' ],
57 report_msgid_bugs_to => [ 1, 'Report-Msgid-Bugs-To' ],
58 pot_creation_date => [ 2, 'POT-Creation-Date' ],
59 po_revision_date => [ 3, 'PO-Revision-Date' ],
60 last_translator => [ 4, 'Last-Translator' ],
61 language_team => [ 5, 'Language-Team' ],
62 language => [ 6, 'Language' ],
63 mime_version => [ 7, 'MIME-Version' ],
64 content_type => [ 8, 'Content-Type' ],
65 content_transfer_encoding => [ 9, 'Content-Transfer-Encoding' ],
66 2 plural_forms => [ 10, 'Plural-Forms' ], } };
67  
68 2 has 'localedir' => is => 'ro', isa => $LocaleDir, coerce => TRUE,
69 default => NUL;
70  
71 has '+result_source_attributes' =>
72 5 ▶ default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 5 mo => {
74 attributes => [ qw( msgid_plural msgstr ) ],
75 defaults => { msgstr => [], }, },
76 po => {
77 attributes =>
78 [ qw( translator_comment extracted_comment reference flags
79 previous msgctxt msgid msgid_plural msgstr ) ],
80 defaults => { 'flags' => [], 'msgstr' => [], },
81 label_attr => 'labels',
82 2 }, } };
83  
84 2 has '+storage_class' => default => '+File::Gettext::Storage::PO';
85  
86 2 has 'source_name' => is => 'ro', isa => $SourceType,
87 default => 'po', trigger => TRUE;
88  
89 # Private methods
90 0 ▶my $_is_file_or_log_debug = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
91 0 my ($self, $path) = @_;
92  
93 0 ▶ $path->exists or ($self->log->debug( 'Path '.$path->pathname.' not found' )
Branch: 1 branch
BranchTrueFalseTotal
$path->exists or $self->log->debug('Path ' . $path->pathname . ' not found') and return 0 0 0 0
Condition0%0/5
$path->exists || $self->log->debug('Path ' . $path->pathname . ' not found') && (return 0)
l!l&&r!l&&!r
000
$self->log->debug('Path ' . $path->pathname . ' not found') and return 0
!ll
00
MC/DC0%0/2
$path->exists || $self->log->debug('Path ' . $path->pathname . ' not found') && (return 0)
$path->exists $self->log->debug('Path ' . $path->pathname . ' not found')
Truth table0%0/3
A || B
A= $path->existsB= $self->log->debug('Path ' . $path->pathname . ' not found')
ABresult
1X1
011
000
94 and return FALSE);
95 0 ▶ $path->is_file or ($self->log->debug( 'Path '.$path->pathname.' not a file' )
Branch: 1 branch
BranchTrueFalseTotal
$path->is_file or $self->log->debug('Path ' . $path->pathname . ' not a file') and return 0 0 0 0
Condition0%0/5
$path->is_file || $self->log->debug('Path ' . $path->pathname . ' not a file') && (return 0)
l!l&&r!l&&!r
000
$self->log->debug('Path ' . $path->pathname . ' not a file') and return 0
!ll
00
MC/DC0%0/2
$path->is_file || $self->log->debug('Path ' . $path->pathname . ' not a file') && (return 0)
$path->is_file $self->log->debug('Path ' . $path->pathname . ' not a file')
Truth table0%0/3
A || B
A= $path->is_fileB= $self->log->debug('Path ' . $path->pathname . ' not a file')
ABresult
1X1
011
000
96 and return FALSE);
97  
98 0 return TRUE;
99 2 };
100  
101 # Construction
102 5 ▶around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
103 5 my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args );
104  
105 5 ▶ my $builder = $attr->{builder} or return $attr;
Branch: 1 branch
BranchTrueFalseTotal
my $builder = $attr->{"builder"} or return $attr 1 4 5
106 1 ▶ my $config = $builder->can( 'config' ) ? $builder->config : {};
Branch: 1 branch
BranchTrueFalseTotal
$builder->can('config') ? : 0 1 1
107 1 my $keys = [ 'gettext_catagory', 'localedir' ];
108  
109 1 merge_attributes $attr, $builder, $keys;
110 1 merge_attributes $attr, $config, $keys;
111  
112 1 return $attr;
113 2 };
114  
115 13 ▶around 'source' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 13 my ($orig, $self) = @_; return $orig->( $self, $self->source_name );
117 2 };
118  
119 5 ▶around 'resultset' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
120 5 my ($orig, $self) = @_; return $orig->( $self, $self->source_name );
121 2 };
122  
123 9 ▶around 'load' => sub {
Subroutine __ANON__: called CC=8 SCAR=22.8
124 9 my ($orig, $self, $lang, @files) = @_;
125  
126 0 my @paths = grep { $self->$_is_file_or_log_debug( $_ ) }
127 0 map { $self->object_file( $lang, $_ ) } @files;
128  
129 9 ▶ not $paths[ 0 ] and not $self->path and return {};
Branch: 1 branch
BranchTrueFalseTotal
$paths[0] or $self->path or return {} 9 0 9
Condition33%1/3
$paths[0] or $self->path
l!l&&r!l&&!r
009
MC/DC0%0/2
$paths[0] or $self->path
$paths[0] $self->path
Truth table33%1/3
A or B
A= $paths[0]B= $self->path
ABresult
1X1
011
000
130  
131 9 my $data = $orig->( $self, @paths );
132 my $po_header = exists $data->{po_header}
133 9 ▶ ? $data->{po_header}->{msgstr} // {} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{'po_header'} ? : 9 0 9
Condition50%1/2
$data->{"po_header"}{"msgstr"} // {}
l!l
90
MC/DC0%0/1
$data->{"po_header"}{"msgstr"} // {}
$data->{"po_header"}{"msgstr"}
Truth table50%1/2
A
A= $data->{"po_header"}{"msgstr"}
Aresult
11
00
134 9 my $plural_func;
135  
136 # This is here because of the code ref. Cannot serialize (cache) a code ref
137 # Determine plural rules. The leading and trailing space is necessary
138 # to be able to match against word boundaries.
139 9 ▶ if (exists $po_header->{plural_forms}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $po_header->{"plural_forms"}) 4 5 9
140 4 my $code = SPC.$po_header->{plural_forms}.SPC;
141  
142 4 $code =~ s{ ([^_a-zA-Z0-9] | \A) ([_a-z][_A-Za-z0-9]*)
143 ([^_a-zA-Z0-9]) }{$1\$$2$3}gmsx;
144 4 $code = "sub { my \$n = shift; my (\$plural, \$nplurals);
145 $code;
146 return (\$nplurals, \$plural ? \$plural : 0); }";
147  
148 # Now try to evaluate the code. There is no need to run the code in
149 # a Safe compartment. The above substitutions should have destroyed
150 # all evil code. Corrections are welcome!
151 4 $plural_func = eval $code; ## no critic
152 4 ▶ $EVAL_ERROR and $plural_func = undef;
Branch: 1 branch
BranchTrueFalseTotal
$File::Gettext::EVAL_ERROR and $plural_func = undef 0 4 4
153 }
154  
155 # Default is Germanic plural (which is incorrect for French).
156 0 ▶ $data->{plural_func} = $plural_func // sub { (2, shift > 1) };
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$plural_func // sub { 2, shift() > 1; }
l!l&&r!l&&!r
450
MC/DC0%0/2
$plural_func // sub { 2, shift() > 1; }
$plural_func sub { 2, shift() > 1; }
Truth table66%2/3
A // B
A= $plural_funcB= sub { 2, shift() > 1; }
ABresult
1X1
011
000
157  
158 9 return $data;
159 2 };
160  
161 1 ▶sub _trigger_source_name {
Subroutine _trigger_source_name: called CC=3 SCAR=11.4
162 1 my ($self, $source) = @_;
163  
164 1 ▶ $source eq 'mo' and $self->storage_class( '+File::Gettext::Storage::MO' );
Branch: 1 branch
BranchTrueFalseTotal
$source eq "mo" and $self->storage_class("+File::Gettext::Storage::MO") 1 0 1
165 1 ▶ $source eq 'po' and $self->storage_class( '+File::Gettext::Storage::PO' );
Branch: 1 branch
BranchTrueFalseTotal
$source eq "po" and $self->storage_class("+File::Gettext::Storage::PO") 0 1 1
166  
167 1 return;
168 }
169  
170 # Public methods
171 16 ▶sub object_file {
Subroutine object_file: called CC=4 SCAR=14.3
Pod documented
172 16 my ($self, $lang, $file) = @_;
173  
174 16 ▶ $lang or throw Unspecified, [ 'language' ];
Branch: 1 branch
BranchTrueFalseTotal
$lang or &throw(Unspecified(), ["language"]) 16 0 16
175 16 ▶ $file or throw Unspecified, [ 'language file name' ];
Branch: 1 branch
BranchTrueFalseTotal
$file or &throw(Unspecified(), ["language file name"]) 16 0 16
176  
177 16 my $dir = $self->localedir; my $extn = $self->storage->extn;
178  
179 16 ▶ length $self->gettext_catagory or return $dir->catfile( $lang, $file.$extn );
Branch: 1 branch
BranchTrueFalseTotal
length $self->gettext_catagory or return $dir->catfile($lang, $file . $extn) 16 0 16
180  
181 16 return $dir->catfile( $lang, $self->gettext_catagory, $file.$extn );
182 }
183  
184 10 ▶sub set_path {
Subroutine set_path: called CC=1 SCAR=0.0
Pod documented
185 10 my $self = shift; return $self->path( $self->object_file( @_ ) );
186 }
187  
188 2 1;
189  
190 __END__
« Gettext/Storage/MO.pm ↑ Summary Gettext/Storage.pm »