blib/lib/YATT/Lite/ConnectionProxy.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 79.1% 19 / 24 Conditioncond 58.3% 14 / 24 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 93.7% 15 / 16 Podpod 50.0% 2 / 4 totaltotal 81.0% 124 / 153 CC 20 SCAR 13.6
CC
20
Cov
87%
AUTOLOAD
7.1
can
6.0
isa
4.4
CRAP 3.9
« blib/lib/YATT/Lite/NSBuilder.pm ↑ Summary blib/lib/YATT/Lite/Partial/MarkAfterNew.pm »
1 package YATT::Lite::ConnectionProxy; sub MY () {__PACKAGE__}
2 74 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 74 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 74 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 74 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 74 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 74 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 74 require YATT::Lite::Util;
10  
11 # $CON while capturing output (GH-295).
12 #
13 # Like YATT::Lite::Connection, this is a blessed glob. Its IO slot is
14 # the capture buffer and its HASH slot holds the original connection.
15 # So `print $CON ...` goes to the buffer, and all methods are
16 # delegated to the original connection.
17  
18 23 ▶sub capture {
Subroutine capture: called CC=3 SCAR=11.0
Pod documented
19 23 my ($pack, $con, $code) = @_;
20 23 ▶ my $root = _root_connection($con)
Branch: 1 branch
BranchTrueFalseTotal
my $root = _root_connection($con) or return &YATT::Lite::Util::captured($code) 14 9 23
21 or return &YATT::Lite::Util::captured($code);
22 14 my $buffer = "";
23 {
24 14 my $glob = Symbol::gensym();
25 14 ▶ open $glob, '>:utf8', \ $buffer
Branch: 1 branch
BranchTrueFalseTotal
open $glob, ">:utf8", \$buffer or die "Can't create capture buf:$!" 14 0 14
26 or die "Can't create capture buf:$!";
27 14 bless $glob, $pack;
28 14 *$glob = {connection => $root};
29 14 $code->($glob);
30 14 close $glob;
31 }
32 14 Encode::_utf8_on($buffer);
33 14 $buffer;
34 }
35  
36 # Nested capture shares the original connection (no proxy of proxy).
37 23 ▶sub _root_connection {
Subroutine _root_connection: called CC=4 SCAR=14.0
38 23 my ($con) = @_;
39 23 ▶ return undef unless Scalar::Util::blessed($con);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($con) 9 14 23
40 14 ▶ if (UNIVERSAL::isa($con, MY)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($con, 'YATT::Lite::ConnectionProxy')) { } 2 12 14
elsif ($con->isa('YATT::Lite::Connection')) { } 12 0 12
41 2 $con->connection;
42 } elsif ($con->isa('YATT::Lite::Connection')) {
43 12 $con;
44 } else {
45 0 undef;
46 }
47 }
48  
49 22 ▶sub connection {
Subroutine connection: called CC=1 SCAR=0.0
Pod documented
50 22 my ($glob) = @_;
51 22 *{$glob}{HASH}->{connection};
52 }
53  
54 # Forwarders are independent from the class of the original connection,
55 # so they are installed once and shared.
56 74 our %FORWARDER;
57 5 ▶sub _forwarder {
Subroutine _forwarder: called CC=2 SCAR=6.9
58 5 my ($name) = @_;
59 5 ▶ $FORWARDER{$name} //= do {
Condition66%2/3
$YATT::Lite::ConnectionProxy::FORWARDER{$name} //= do { my $sub = sub { my $glob = shift(); $glob->connection->$name(@_); } ; no strict 'refs'; *{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub }
l!l&&r!l&&!r
140
MC/DC0%0/2
$YATT::Lite::ConnectionProxy::FORWARDER{$name} //= do { my $sub = sub { my $glob = shift(); $glob->connection->$name(@_); } ; no strict 'refs'; *{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub }
$YATT::Lite::ConnectionProxy::FORWARDER{$name} do { my $sub = sub { my $glob = shift(); $glob->connection->$name(@_); } ; no strict 'refs'; *{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub }
Truth table66%2/3
A //= B
A= $YATT::Lite::ConnectionProxy::FORWARDER{$name}B= do { my $sub = sub { my $glob = shift(); $glob->connection->$name(@_); } ; no strict 'refs'; *{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub }
ABresult
1X1
011
000
60 8 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 8 my $glob = shift;
62 8 $glob->connection->$name(@_);
63 4 };
64 74 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 4 *{MY . "::$name"} = $sub;
66 };
67 5 \&{MY . "::$name"};
68 }
69  
70 74 our $AUTOLOAD;
71 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=19.6
72 5 my $name = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1);
73 5 ▶ my $con = ref $_[0] ? $_[0]->connection : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
74 5 ▶ unless ($con and $con->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($con and $con->can($name)) 1 4 5
Condition66%2/3
$con and $con->can($name)
!ll&&!rl&&r
014
MC/DC50%1/2
$con and $con->can($name)
$con $con->can($name)
Truth table66%2/3
A and B
A= $conB= $con->can($name)
ABresult
0X0
100
111
75 1 ▶ Carp::croak(sprintf q{Can't locate object method "%s" via package "%s"}
Condition33%2/6
ref $con || ref $_[0] || $_[0]
l!l&&r!l&&!r
100
ref $con or ref $_[0]
l!l&&r!l&&!r
100
MC/DC0%0/3
ref $con || ref $_[0] || $_[0]
ref $con ref $_[0] $_[0]
Truth table25%1/4
A or B || C
A= ref $conB= ref $_[0]C= $_[0]
ABCresult
1XX1
01X1
0011
0000
76 , $name, ref $con || ref $_[0] || $_[0]);
77 }
78 4 goto &{_forwarder($name)};
79 }
80  
81 # Methods from ->can must work with the proxy as invocant.
82 4 ▶sub can {
Subroutine can: called CC=6 SCAR=18.0
Pod undocumented
83 4 my ($glob, $name) = @_;
84 4 ▶ if (not $FORWARDER{$name} and my $sub = $glob->SUPER::can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $YATT::Lite::ConnectionProxy::FORWARDER{$name} and my $sub = $glob->SUPER::can($name)) 1 3 4
Condition100%3/3
not $YATT::Lite::ConnectionProxy::FORWARDER{$name} and my $sub = $glob->SUPER::can($name)
!ll&&!rl&&r
121
MC/DC100%2/2
not $YATT::Lite::ConnectionProxy::FORWARDER{$name} and my $sub = $glob->SUPER::can($name)
not $YATT::Lite::ConnectionProxy::FORWARDER{$name} my $sub = $glob->SUPER::can($name)
Truth table100%3/3
not(A) and B
A= not $YATT::Lite::ConnectionProxy::FORWARDER{$name}B= my $sub = $glob->SUPER::can($name)
ABresult
0X0
100
111
85 1 return $sub;
86 }
87 3 ▶ return undef unless ref $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $glob 0 3 3
88 3 my $con = $glob->connection;
89 3 ▶ $con && $con->can($name) ? _forwarder($name) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$con && $con->can($name) ? : 1 2 3
Condition66%2/3
$con and $con->can($name)
!ll&&!rl&&r
021
MC/DC50%1/2
$con and $con->can($name)
$con $con->can($name)
Truth table66%2/3
A and B
A= $conB= $con->can($name)
ABresult
0X0
100
111
90 }
91  
92 3 ▶sub isa {
Subroutine isa: called CC=4 SCAR=14.8
Pod undocumented
93 3 my ($glob, $class) = @_;
94 $glob->SUPER::isa($class)
95 3 ▶ or ref $glob && do {
Branch: 1 branch
BranchTrueFalseTotal
$glob->SUPER::isa($class) or ref $glob and do { my $con = $glob->connection; $con and $con->isa($class) } 1 2 3
Condition50%3/6
$glob->SUPER::isa($class) || ref $glob && do { my $con = $glob->connection; $con and $con->isa($class) }
l!l&&r!l&&!r
120
ref $glob and do { my $con = $glob->connection; $con and $con->isa($class) }
!ll&&!rl&&r
002
MC/DC0%0/3
$glob->SUPER::isa($class) || ref $glob && do { my $con = $glob->connection; $con and $con->isa($class) }
$glob->SUPER::isa($class) ref $glob do { my $con = $glob->connection; $con and $con->isa($class) }
Truth table50%2/4
A || B and C
A= $glob->SUPER::isa($class)B= ref $globC= do { my $con = $glob->connection; $con and $con->isa($class) }
ABCresult
1XX1
0111
00X0
0100
96 2 my $con = $glob->connection;
97 2 ▶ $con && $con->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
$con and $con->isa($class) 2 0 2
98 };
99 }
100  
101 # Never delegate this. Connection::DESTROY clears its props.
102 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
103  
104 74 1;
« blib/lib/YATT/Lite/NSBuilder.pm ↑ Summary blib/lib/YATT/Lite/Partial/MarkAfterNew.pm »