blib/lib/LWP/UserAgent/Plugin.pm

filter: Statementstmt 31.8% 21 / 66 Branchbran 2.7% 1 / 36 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 5 / 10 Podpod 100% 3 / 3 totaltotal 24.3% 30 / 123 CC 21 SCAR 40.9
CC
21
Cov
21%
_run_hooks
132.0
set_plugins
42.0
request
12.0
CRAP 59.8
↑ Summary
1 package LWP::UserAgent::Plugin;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-08-13'; # DATE
5 1 our $DIST = 'LWP-UserAgent-Plugin'; # DIST
6 1 our $VERSION = '0.004'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use parent 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶if ($ENV{LWP_USERAGENT_PLUGINS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LWP_USERAGENT_PLUGINS"}) 0 1 1
16 0 require JSON::PP;
17 __PACKAGE__->set_plugins(@{
18 0 JSON::PP::decode_json($ENV{LWP_USERAGENT_PLUGINS})
19 0 });
20 }
21  
22 0 ▶sub import {
Subroutine import: not called CC=2 SCAR=17.9
23 0 my $class = shift;
24 0 ▶ $class->set_plugins(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
25 }
26  
27 1 my @plugins;
28 0 ▶sub set_plugins {
Subroutine set_plugins: not called CC=6 SCAR=37.4
Pod documented
29 0 my $class = shift;
30  
31 0 my @old_plugins = @plugins;
32 0 @plugins = ();
33 0 while (1) {
34 0 ▶ last unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
35 0 my $arg = shift;
36 0 ▶ my $class = ref $class eq 'ARRAY' ? $arg->[0] : $arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $class eq 'ARRAY' ? : 0 0 0
37 0 ▶ $class = "LWP::UserAgent::Plugin::$class"
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /\ALWP::UserAgent::Plugin::/ 0 0 0
38 unless $class =~ /\ALWP::UserAgent::Plugin::/;
39 0 (my $class_pm = "$class.pm") =~ s!::!/!g;
40 0 require $class_pm;
41 0 ▶ my $config = ref $arg eq 'ARRAY' ? $arg->[1] :
Branch: 2 branches
BranchTrueFalseTotal
ref $arg eq 'ARRAY' ? : 0 0 0
ref $_[0] eq 'HASH' ? : 0 0 0
42 ref($_[0]) eq 'HASH' ? shift : {};
43 0 push @plugins, [$class, $config];
44 }
45 0 @old_plugins;
46 }
47  
48 0 ▶sub _run_hooks {
Subroutine _run_hooks: not called CC=11 SCAR=48.8
49 0 my ($self, $hook, $opts, $r) = @_;
50  
51 0 ▶ log_trace "[LWP::UserAgent::Plugin] -> running hook %s", $hook if $ENV{LWP_USERAGENT_PLUGIN_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LWP_USERAGENT_PLUGIN_TRACE"} 0 0 0
52  
53 0 my $status;
54 0 for my $p (@plugins) {
55 0 ▶ next unless $p->[0]->can($hook);
Branch: 1 branch
BranchTrueFalseTotal
unless $p->[0]->can($hook) 0 0 0
56 0 ▶ log_trace "[LWP::UserAgent::Plugin] -> handler from plugin %s", $p->[0] if $ENV{LWP_USERAGENT_PLUGIN_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LWP_USERAGENT_PLUGIN_TRACE"} 0 0 0
57 0 local $r->{config} = $p->[1];
58 0 local $r->{hook} = $hook;
59 0 $status = $p->[0]->$hook($r);
60 0 ▶ unless ($opts->{all}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"all"}) 0 0 0
61 0 ▶ last unless $status == -1;
Branch: 1 branch
BranchTrueFalseTotal
unless $status == -1 0 0 0
62 }
63 0 ▶ last if $status == 98 || $status == 99;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 98 or $status == 99 0 0 0
Condition0%0/3
$status == 98 or $status == 99
l!l&&r!l&&!r
000
MC/DC0%0/2
$status == 98 or $status == 99
$status == 98 $status == 99
Truth table0%0/3
A or B
A= $status == 98B= $status == 99
ABresult
1X1
011
000
64 }
65 0 ▶ $status //= -1;
Condition0%0/2
$status //= -1
l!l
00
MC/DC0%0/1
$status //= -1
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
66 0 ▶ log_trace "[LWP::UserAgent::Plugin] <- return hook %s, status %d", $hook, $status if $ENV{LWP_USERAGENT_PLUGIN_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LWP_USERAGENT_PLUGIN_TRACE"} 0 0 0
67 0 $status;
68 }
69  
70 0 ▶sub request {
Subroutine request: not called CC=3 SCAR=24.8
Pod documented
71 0 my $r = {ua=>$_[0], argv=>[@_], method=>'request'};
72 0 my $self = shift;
73  
74 0 while (1) {
75 0 ▶ $r->{response} = $self->SUPER::request(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_run_hooks("before_request", {"all", 1}, $r) == 99 0 0 0
76 unless $self->_run_hooks('before_request', {all=>1}, $r) == 99;
77 0 ▶ last unless $self->_run_hooks('after_request', {all=>1}, $r) == 98;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_run_hooks("after_request", {"all", 1}, $r) == 98 0 0 0
78 }
79 0 $r->{response};
80 }
81  
82 0 ▶sub mirror {
Subroutine mirror: not called CC=3 SCAR=24.8
Pod documented
83 0 my $r = {ua=>$_[0], argv=>[@_], method=>'mirror'};
84 0 my $self = shift;
85  
86 0 while (1) {
87 0 ▶ $r->{response} = $self->SUPER::mirror(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_run_hooks("before_mirror", {"all", 1}, $r) == 99 0 0 0
88 unless $self->_run_hooks('before_mirror', {all=>1}, $r) == 99;
89 0 ▶ last unless $self->_run_hooks('after_mirror', {all=>1}, $r) == 98;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_run_hooks("after_mirror", {"all", 1}, $r) == 98 0 0 0
90 }
91 0 $r->{response};
92 }
93  
94 1 1;
95 # ABSTRACT: LWP::UserAgent with plugins
96  
97 __END__
↑ Summary