File Coverage

blib/lib/Locale/Maketext/Utils/Phrase/Norm/Compiles.pm
Criterion Covered Total %
statement 35 39 89.7
branch 5 6 83.3
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 45 51 88.2


line stmt bran cond sub pod time code
1             package Locale::Maketext::Utils::Phrase::Norm::Compiles;
2              
3 4     4   3167 use strict;
  4         9  
  4         178  
4 4     4   22 use warnings;
  4         9  
  4         226  
5 4     4   925 use Locale::Maketext::Utils ();
  4         10  
  4         105  
6 4     4   734 use Locale::Maketext::Utils::Phrase ();
  4         13  
  4         2351  
7              
8             sub normalize_maketext_string {
9 90     90 0 225 my ($filter) = @_;
10              
11 90         315 my $string = $filter->get_orig_str();
12 90         307 my $mt_obj = $filter->get_maketext_object();
13 90         303 my $bn_regexp = Locale::Maketext::Utils::Phrase::get_bn_var_regexp();
14              
15 90         490 local $SIG{'__DIE__'}; # cpanel specific: ensure a benign eval does not trigger cpsrvd's DIE handler (may be made moot by internal case 50857)
16 90         212 eval {
17              
18             # TODO: when we have a phrase class we can pass in proper args to each BN method, for now pass in a bunch of numbers tpo avoid warnings
19 90         169 my $n = 0;
20 90         1298 my @args = map { $n++ } ( $string =~ m/($bn_regexp)/g );
  76         287  
21              
22 90         853 $mt_obj->makethis( $string, @args );
23             };
24              
25 90 100       5830 if ($@) {
26 26         64 my $error = $@;
27              
28 26         340 $error =~ s/([\[\]])/~$1/g;
29 26         225 $error =~ s/[\n\r]+/ /g;
30              
31 26         163 $string =~ s/([\[\]])/~$1/g;
32 26         350 $error =~ s/\Q$string\E.*$/$string/;
33 26         130 my $string_sr = $filter->get_string_sr();
34 26 100       243 if ( $error =~ m/Can't locate object method "(.*)" via package "(.*)"/i ) {
    50          
35 10         57 $error = "“$2” does not have a method “$1” in: $string";
36             }
37             elsif ( $error =~ m/Undefined subroutine (\S+)/i ) { # odd but theoretically possible
38 0         0 my $full_func = $1;
39 0         0 my ( $f, @c ) = reverse( split( /::/, $full_func ) );
40 0         0 my $c = join( '::', reverse(@c) );
41 0         0 $error = "“$2” does not have a function “$1” in: $string";
42             }
43              
44 26         53 ${$string_sr} = "[comment,Bracket Notation Error: $error]";
  26         71  
45              
46 26         89 $filter->add_violation('Bracket Notation Error');
47             }
48              
49 90         356 return $filter->return_value;
50             }
51              
52             1;
53              
54             __END__
55              
56             =encoding utf-8
57              
58             =head1 Normalization
59              
60             Check that the string compiles.
61              
62             =head2 Rationale
63              
64             If the source phrase is broken, we have to die at run time. If the target phrase is broken, we do not get the translation.
65              
66             Conceivably there could also be more subtle problems it could cause.
67              
68             =head1 possible violations
69              
70             =over 4
71              
72             =item Bracket Notation Error
73              
74             There was a problem compiling the string.
75              
76             The string is replaced with a comment that details the problem, typically including an escaped verison of the problematic string: [comment,Bracket Notation Error: DETAILS_GO_HERE]
77              
78             =back
79              
80             =head1 possible warnings
81              
82             None