Log/Report/Domain.pm

filter: Statementstmt 57.4% 54 / 94 Branchbran 17.3% 8 / 46 Conditioncond 38.8% 7 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 73.6% 14 / 19 Podpod 90.0% 9 / 10 totaltotal 46.4% 92 / 198 CC 31 SCAR 33.7
CC
31
Cov
44%
configure
53.1
translate
39.8
readConfig
30.0
CRAP 29.0
« Log/Report.pm ↑ Summary Log/Report/Dispatcher.pm »
1 # This code is part of Perl distribution Log-Report version 1.46.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2007-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Log::Report::Domain;{
13 17 our $VERSION = '1.46';
14 }
15  
16 17 ▶use parent 'Log::Report::Minimal::Domain';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use Log::Report 'log-report', import => [ qw/__x error fault panic warning/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Log::Report::Util qw/parse_locale/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Log::Report::Translator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 17 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #--------------------
28  
29 sub init($)
30 11 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
31 11 $self->SUPER::init($args);
32 11 $self->{LRD_ctxt_def} = {};
33 11 $self;
34 }
35  
36 #--------------------
37  
38 10 ▶sub nativeLanguage() { $_[0]->{LRD_native} }
Subroutine nativeLanguage: called CC=1 SCAR=0.0
Pod documented
39 112 ▶sub translator() { $_[0]->{LRD_transl} }
Subroutine translator: called CC=1 SCAR=0.0
Pod documented
40 0 ▶sub contextRules() { $_[0]->{LRD_ctxt_rules} }
Subroutine contextRules: not called CC=1 SCAR=6.9
Pod documented
41  
42  
43 sub configure(%)
44 12 ▶{ my ($self, %args) = @_;
Subroutine configure: called CC=16 SCAR=39.7
Pod documented
45  
46 12 ▶ if(my $config = delete $args{config})
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = delete $args{"config"}) 0 12 12
47 0 { my $set = $self->readConfig($config);
48 0 %args = (%$set, %args);
49 }
50  
51 12 ▶ my $format = $args{formatter} || 'PRINTI';
Condition50%1/2
$args{'formatter'} || 'PRINTI'
l!l
012
MC/DC0%0/1
$args{'formatter'} || 'PRINTI'
$args{'formatter'}
Truth table50%1/2
A
A= $args{'formatter'}
Aresult
11
00
52 12 ▶ $args{formatter} = $format = {} if $format eq 'PRINTI';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "PRINTI" 12 0 12
53  
54 12 ▶ if(ref $format eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format eq "HASH") 12 0 12
55 12 ▶ { $format->{missing_key} = sub { $self->_reportMissingKey(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
56 }
57  
58 12 $self->SUPER::configure(%args);
59  
60 12 ▶ my $transl = $args{translator} || Log::Report::Translator->new;
Condition33%1/3
$args{'translator'} || 'Log::Report::Translator'->new
l!l&&r!l&&!r
0120
MC/DC0%0/2
$args{'translator'} || 'Log::Report::Translator'->new
$args{'translator'} 'Log::Report::Translator'->new
Truth table33%1/3
A || B
A= $args{'translator'}B= 'Log::Report::Translator'->new
ABresult
1X1
011
000
61 12 ▶ $transl = Log::Report::Translator->new(%$transl)
Branch: 1 branch
BranchTrueFalseTotal
if ref $transl eq "HASH" 0 12 12
62 if ref $transl eq 'HASH';
63  
64 12 ▶ !blessed $transl || $transl->isa('Log::Report::Translator')
Branch: 1 branch
BranchTrueFalseTotal
not builtin::blessed($transl) or $transl->isa("Log::Report::Translator") or &panic("translator must be a Log::Report::Translator object") 12 0 12
Condition33%1/3
not builtin::blessed($transl) or $transl->isa("Log::Report::Translator")
l!l&&r!l&&!r
0120
MC/DC0%0/2
not builtin::blessed($transl) or $transl->isa("Log::Report::Translator")
not builtin::blessed($transl) $transl->isa("Log::Report::Translator")
Truth table33%1/3
not(A) or B
A= not builtin::blessed($transl)B= $transl->isa("Log::Report::Translator")
ABresult
1X1
011
000
65 or panic "translator must be a Log::Report::Translator object";
66 12 $self->{LRD_transl} = $transl;
67  
68 12 ▶ my $native = $self->{LRD_native} = $args{native_language} || 'en_US';
Condition50%1/2
$args{'native_language'} || 'en_US'
l!l
012
MC/DC0%0/1
$args{'native_language'} || 'en_US'
$args{'native_language'}
Truth table50%1/2
A
A= $args{'native_language'}
Aresult
11
00
69 12 my ($lang) = parse_locale $native;
70 12 ▶ defined $lang
Branch: 1 branch
BranchTrueFalseTotal
defined $lang or &error(&__x("the native_language '{locale}' is not a valid locale", "locale", $native)) 12 0 12
71 or error __x"the native_language '{locale}' is not a valid locale", locale => $native;
72  
73 12 ▶ if(my $cr = $args{context_rules})
Branch: 1 branch
BranchTrueFalseTotal
if (my $cr = $args{"context_rules"}) 0 12 12
74 0 { my $tc = 'Log::Report::Translator::Context';
75 0 ▶ eval "require $tc"; panic $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
76 0 ▶ if(blessed $cr) { $cr->isa($tc) or panic "context_rules must be a $tc" }
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($cr)) { } 0 0 0
elsif (ref $cr eq 'HASH') { } 0 0 0
$cr->isa($tc) or &panic("context_rules must be a $tc") 0 0 0
77 0 elsif(ref $cr eq 'HASH') { $cr = Log::Report::Translator::Context->new(rules => $cr) }
78 0 else { panic "context_rules expects object or hash, not {have}", have=>$cr }
79  
80 0 $self->{LRD_ctxt_rules} = $cr;
81 }
82  
83 12 $self;
84 }
85  
86 sub _reportMissingKey($$)
87 2 ▶{ my ($self, $sp, $key, $args) = @_;
Subroutine _reportMissingKey: called CC=1 SCAR=0.0
88  
89 warning __x"Missing key '{key}' in format '{format}', file {use}",
90 2 key => $key, format => $args->{_format}, use => $args->{_use};
91  
92 2 undef;
93 }
94  
95  
96  
97 sub readConfig($)
98 0 ▶{ my ($thing, $fn) = @_;
Subroutine readConfig: not called CC=5 SCAR=34.0
Pod documented
99 0 my $config;
100  
101 0 ▶ if($fn =~ m/\.pl$/i)
Branch: 2 branches
BranchTrueFalseTotal
if ($fn =~ /\.pl$/i) { } 0 0 0
elsif ($fn =~ /\.json$/i) { } 0 0 0
102 0 { $config = do $fn;
103 }
104 elsif($fn =~ m/\.json$/i)
105 0 ▶ { eval "require JSON"; panic $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
106 0 ▶ open my($fh), '<:encoding(utf8)', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $fn or &fault(&__x("cannot open JSON file for context at {fn}", "fn", $fn)) 0 0 0
107 or fault __x"cannot open JSON file for context at {fn}", fn => $fn;
108 0 local $/;
109 0 $config = JSON->utf8->decode(<$fh>);
110 }
111 else
112 0 { error __x"unsupported context file type for {fn}", fn => $fn;
113 }
114  
115 0 $config;
116 }
117  
118 #--------------------
119  
120 sub setContext(@)
121 0 ▶{ my $self = shift;
Subroutine setContext: not called CC=2 SCAR=17.9
Pod documented
122 0 ▶ my $cr = $self->contextRules # ignore context if no rules given
Branch: 1 branch
BranchTrueFalseTotal
my $cr = $self->contextRules or &error(&__x("you need to configure context_rules before setContext")) 0 0 0
123 or error __x"you need to configure context_rules before setContext";
124  
125 0 $self->{LRD_ctxt_def} = $cr->needDecode(set => @_);
126 }
127  
128  
129 sub updateContext(@)
130 0 ▶{ my $self = shift;
Subroutine updateContext: not called CC=3 SCAR=24.8
Pod documented
131 0 ▶ my $cr = $self->contextRules # ignore context if no rules given
Branch: 1 branch
BranchTrueFalseTotal
my $cr = $self->contextRules or return 0 0 0
132 or return;
133  
134 0 my $rules = $cr->needDecode(update => @_);
135 0 ▶ my $r = $self->{LRD_ctxt_def} ||= {};
Condition0%0/2
$self->{"LRD_ctxt_def"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"LRD_ctxt_def"} ||= {}
$self->{"LRD_ctxt_def"}
Truth table0%0/2
A
A= $self->{"LRD_ctxt_def"}
Aresult
11
00
136 0 @{$r}{keys %$r} = values %$r;
137 0 $r;
138 }
139  
140  
141 0 ▶sub defaultContext() { $_[0]->{LRD_ctxt_def} }
Subroutine defaultContext: not called CC=1 SCAR=6.9
Pod documented
142  
143  
144 sub translate($$)
145 100 ▶{ my ($self, $msg, $lang) = @_;
Subroutine translate: called CC=9 SCAR=36.8
Pod documented
146 100 ▶ my $tr = $self->translator || $self->configure->translator;
Condition66%2/3
$self->translator || $self->configure->translator
l!l&&r!l&&!r
88120
MC/DC0%0/2
$self->translator || $self->configure->translator
$self->translator $self->configure->translator
Truth table66%2/3
A || B
A= $self->translatorB= $self->configure->translator
ABresult
1X1
011
000
147 100 my $msgid = $msg->msgid;
148  
149 # fast route when certainly no context is involved
150 100 ▶ return $tr->translate($msg, $lang) || $msgid
Branch: 1 branch
BranchTrueFalseTotal
if index($msgid, "<") == -1 100 0 100
Condition33%1/3
$tr->translate($msg, $lang) || $msgid
l!l&&r!l&&!r
10000
MC/DC0%0/2
$tr->translate($msg, $lang) || $msgid
$tr->translate($msg, $lang) $msgid
Truth table33%1/3
A || B
A= $tr->translate($msg, $lang)B= $msgid
ABresult
1X1
011
000
151 if index($msgid, '<') == -1;
152  
153 0 my $msgctxt;
154 0 ▶ if($msgctxt = $msg->msgctxt)
Branch: 2 branches
BranchTrueFalseTotal
if ($msgctxt = $msg->msgctxt) { } 0 0 0
elsif (my $rules = $self->contextRules) { } 0 0 0
155 { # msgctxt in traditional gettext style
156 }
157 elsif(my $rules = $self->contextRules)
158 0 { ($msgid, $msgctxt) = $rules->ctxtFor($msg, $lang, $self->defaultContext);
159 }
160 else
161 0 ▶ { 1 while $msgid =~ s/\{([^}]*)\<\w+([^}]*)\}/length "$1$2" ? "{$1$2}" : ''/e;
Branch: 2 branches
BranchTrueFalseTotal
$msgid =~ s/\{([^}]*)\<\w+([^}]*)\}/length "$1$2" ? "{$1$2}" : "";/e and do { '???' } 0 0 0
length "$1$2" ? : 0 0 0
162 }
163  
164 # This is ugly, horrible and worse... but I do not want to mutulate
165 # the message neither to clone it for performance. We do need to get
166 # rit of {<}
167 0 local $msg->{_msgid} = $msgid;
168 0 ▶ $tr->translate($msg, $lang, $msgctxt) || $msgid;
Branch: 1 branch
BranchTrueFalseTotal
$tr->translate($msg, $lang, $msgctxt) or $msgid 0 0 0
169 }
170  
171 17 1;
172  
173 #--------------------
« Log/Report.pm ↑ Summary Log/Report/Dispatcher.pm »