blib/lib/App/PPE.pm

filter: Statementstmt 72.5% 58 / 80 Branchbran 40.9% 9 / 22 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.9% 15 / 19 Podpod 0.0% 0 / 6 totaltotal 63.1% 84 / 133 CC 15 SCAR 15.3
CC
15
Cov
65%
parse_options
20.0
run
6.0
new
4.5
CRAP 4.6
↑ Summary
1 package App::PPE;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.04";
7  
8 2 ▶use Parse::ErrorString::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Term::ANSIColor qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # https://perldoc.perl.org/perldiag.html#DESCRIPTION
12 2 our $TAG_MAP = {
13 W => 'WARN',
14 D => 'WARN',
15 S => 'WARN',
16 F => 'CRITICAL',
17 P => 'CRITICAL',
18 X => 'ERROR',
19 A => 'ERROR',
20  
21 undef => 'UNKNOWN',
22 };
23  
24 2 our $COLOR = {
25 'warn' => {
26 text => 'black',
27 background => 'yellow',
28 },
29 'critical' => {
30 text => 'black',
31 background => 'red'
32 },
33 'error' => {
34 text => 'red',
35 background => 'black'
36 },
37 'unknown' => {
38 text => 'white',
39 background => 'red'
40 }
41 };
42  
43 1 ▶our $FORMAT = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 my ($tag, $type, $message, $file, $line) = @_;
45 1 return "$file:$line: [$tag] ($type) $message";
46 2 };
47  
48  
49  
50 0 ▶sub new_with_options {
Subroutine new_with_options: not called CC=1 SCAR=6.9
Pod undocumented
51 0 my ($class, @argv) = @_;
52  
53 0 my ($opt) = $class->parse_options(@argv);
54 0 $class->new($opt);
55 }
56  
57 0 ▶sub parse_options {
Subroutine parse_options: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my ($class, @argv) = @_;
59  
60 0 ▶ if (grep /^--?h(?:elp)?$/, @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^--?h(?:elp)?$/, @argv) 0 0 0
61 0 _print_usage();
62 }
63  
64 0 my $opt = {};
65 0 my @rest;
66 0 for my $v (@argv) {
67 0 ▶ if ($v eq '--no-color') {
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq "--no-color") 0 0 0
68 0 $opt->{color} = undef;
69 0 next;
70 }
71 0 push @rest, $v;
72 }
73 0 ($opt, \@rest)
74 }
75  
76 1 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod undocumented
77 1 my $class = shift;
78  
79 1 ▶ my $opt = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 0 1
80 1 ▶ unless (exists $opt->{parser}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt->{"parser"}) 1 0 1
81 1 $opt->{parser} = Parse::ErrorString::Perl->new()
82 }
83  
84 1 ▶ unless (exists $opt->{color}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt->{"color"}) 0 1 1
85 0 $opt->{color} = 1;
86 }
87  
88 1 bless $opt, $class;
89 }
90  
91 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $self = shift;
93 0 ▶ print $self->prettify_perl_error($_) . "\n" while <STDIN>
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline STDIN) and do { print $self->prettify_perl_error($_) . "\n" } 0 0 0
94 }
95  
96 2 ▶sub prettify_perl_error {
Subroutine prettify_perl_error: called CC=2 SCAR=6.9
Pod undocumented
97 2 my ($self, $perl_error) = @_;
98  
99 2 my ($error_item) = $self->{parser}->parse_string($perl_error);
100 2 ▶ return $perl_error unless $error_item;
Branch: 1 branch
BranchTrueFalseTotal
unless $error_item 1 1 2
101 1 $self->prettify_error_item($error_item);
102 }
103  
104 1 ▶sub prettify_error_item {
Subroutine prettify_error_item: called CC=1 SCAR=0.0
Pod undocumented
105 1 my ($self, $error_item) = @_;
106  
107 1 my $tag = $self->_prettify_tag($error_item);
108 1 my $type = $self->_prettify_type($error_item);
109 1 my $message = $self->_prettify_message($error_item);
110 1 my $file = $self->_prettify_file($error_item);
111  
112 1 $FORMAT->($tag, $type, $message, $file, $error_item->line);
113 }
114  
115 1 ▶sub _tag {
Subroutine _tag: called CC=2 SCAR=7.1
116 1 my $error_item = shift;
117 1 ▶ my $type = $error_item->type // 'undef';
Condition50%1/2
$error_item->type // "undef"
l!l
10
MC/DC0%0/1
$error_item->type // "undef"
$error_item->type
Truth table50%1/2
A
A= $error_item->type
Aresult
11
00
118 1 return $TAG_MAP->{$type};
119 }
120  
121 1 ▶sub _prettify_color {
Subroutine _prettify_color: called CC=2 SCAR=10.1
122 1 my ($self, $error_item) = @_;
123  
124 1 ▶ return {} unless $self->{color};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"color"} 1 0 1
125  
126 0 my $tag = _tag($error_item);
127 0 my $color = $COLOR->{lc($tag)};
128  
129 0 return $color;
130 }
131  
132 1 ▶sub _prettify_tag {
Subroutine _prettify_tag: called CC=3 SCAR=11.2
133 1 my ($self, $error_item) = @_;
134  
135 1 my $tag = _tag($error_item);
136 1 my $color = $self->_prettify_color($error_item);
137 1 ▶ $tag = Term::ANSIColor::color($color->{text}) . $tag . Term::ANSIColor::color("reset") if $color->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $color->{"text"} 0 1 1
138 1 ▶ $tag = Term::ANSIColor::color("on_".$color->{background}) . $tag . Term::ANSIColor::color("reset") if $color->{background};
Branch: 1 branch
BranchTrueFalseTotal
if $color->{"background"} 0 1 1
139  
140 1 return $tag;
141 }
142  
143 1 ▶sub _prettify_type {
Subroutine _prettify_type: called CC=2 SCAR=7.2
144 1 my ($self, $error_item) = @_;
145 1 ▶ return $error_item->type // 'undef';
Condition50%1/2
$error_item->type // "undef"
l!l
10
MC/DC0%0/1
$error_item->type // "undef"
$error_item->type
Truth table50%1/2
A
A= $error_item->type
Aresult
11
00
146 }
147  
148 1 ▶sub _prettify_message {
Subroutine _prettify_message: called CC=2 SCAR=7.0
149 1 my ($self, $error_item) = @_;
150  
151 1 my $message = $error_item->message;
152 1 ▶ if (my $near = $error_item->near) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $near = $error_item->near) 1 0 1
153 1 $near =~ s/:$//;
154 1 $message .= ", near " . $near;
155 }
156  
157 1 return $message;
158 }
159  
160 1 ▶sub _prettify_file {
Subroutine _prettify_file: called CC=1 SCAR=0.0
161 1 my ($self, $error_item) = @_;
162 1 return $error_item->file;
163 }
164  
165  
166 0 ▶sub _print_usage {
Subroutine _print_usage: not called CC=1 SCAR=6.9
167 0 print <<'EOS';
168 $ echo 'syntax error at /home/kfly8/foo.pl line 52, near "$foo:"' | ppe
169 foo.pl:52: [CRITICAL] syntax error: near $foo
170 EOS
171  
172 0 exit;
173 }
174  
175 2 1;
176 __END__
↑ Summary