| 1 | package UserAgent::Any::Wrapper; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.036; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use UserAgent::Any::Impl::Helper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use List::Util 'none'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Sub::Util 'set_subname'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | our $VERSION = 0.01; | |||||||||||
| 11 | 1 | our @EXPORT_OK = qw(wrap_method wrap_all_methods wrap_get_like_methods | |||||||||||
| 12 | wrap_post_like_methods wrap_method_sets); | ||||||||||||
| 13 | 1 | our @CARP_NOT; | |||||||||||
| 14 | |||||||||||||
| 15 | 9 | ▶ | sub _wrap_response { | ||||||||||
|
Subroutine
_wrap_response: called CC=3 SCAR=11.0
| |||||||||||||
| 16 | 9 | ▶ | return undef unless @_; ## no critic (ProhibitExplicitReturnUndef) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 8 | ▶ | return @_ if @_ == 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 1 | return \@_; | |||||||||||
| 19 | } | ||||||||||||
| 20 | |||||||||||||
| 21 | 14 | ▶ | sub _push_if_needed ($list, $val) { | ||||||||||
|
Subroutine
_push_if_needed: called CC=2 SCAR=6.9
| |||||||||||||
| 22 | # list can be an array reference or a symbolic reference. | ||||||||||||
| 23 | 1 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 14 | ▶ | push @{$list}, $val if none { $_ eq $val } @{$list}; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 25 | 14 | return; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 21 | ▶ | sub _create_method ($name, $code) { | ||||||||||
|
Subroutine
_create_method: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 1 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 21 | *{$name} = set_subname($name, $code); | |||||||||||
| 31 | #set_subname($name, *{$name}); | ||||||||||||
| 32 | 21 | return; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 7 | ▶ | sub wrap_method { ## no critic (RequireArgUnpacking) | ||||||||||
|
Subroutine
wrap_method: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 36 | 7 | my $name = shift; | |||||||||||
| 37 | 7 | ▶ | my $getter = shift if ref($_[0]) eq 'CODE'; ## no critic (ProhibitConditionalDeclarations) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 7 | my ($method, $code, $cb, $level) = @_; # level is undocumented | |||||||||||
| 39 | 7 | ▶ | my $dest_pkg = caller($level // 0); | ||||||||||
|
Condition100%2/2
$level // 0
MC/DC100%1/1
$level // 0
$level
Truth table100%2/2
A
A= $level
| |||||||||||||
| 40 | 7 | _push_if_needed("${dest_pkg}::CARP_NOT", 'UserAgent::Any::Wrapper'); | |||||||||||
| 41 | 7 | _push_if_needed(\@CARP_NOT, $dest_pkg); | |||||||||||
| 42 | 7 | ▶ | my $get_obj = defined $getter ? sub ($self) { $self->$getter() } : sub ($self) { $self }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 43 | 7 | ▶ | if (defined $cb) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | _create_method( | ||||||||||||
| 45 | "${dest_pkg}::${name}", | ||||||||||||
| 46 | 5 | ▶ | sub ($self, @args) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | 5 | $cb->($self, _wrap_response($get_obj->($self)->$method($code->($self, @args))), @args); | |||||||||||
| 48 | 3 | }); | |||||||||||
| 49 | 3 | my $method_cb = "${method}_cb"; | |||||||||||
| 50 | _create_method( | ||||||||||||
| 51 | "${dest_pkg}::${name}_cb", | ||||||||||||
| 52 | 2 | ▶ | sub ($self, @args) { | ||||||||||
|
Subroutine
countbar_cb: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | 2 | ▶ | return sub ($final_cb) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | $get_obj->($self)->$method_cb($code->($self, @args)) | ||||||||||||
| 55 | 2 | ▶ | ->(sub { $final_cb->($cb->($self, &_wrap_response, @args)) }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | } | ||||||||||||
| 57 | 3 | }); | |||||||||||
| 58 | 3 | my $method_p = "${method}_p"; | |||||||||||
| 59 | _create_method( | ||||||||||||
| 60 | "${dest_pkg}::${name}_p", | ||||||||||||
| 61 | 2 | ▶ | sub ($self, @args) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | $get_obj->($self)->$method_p($code->($self, @args)) | ||||||||||||
| 63 | 2 | ▶ | ->then(sub { $cb->($self, &_wrap_response, @args) }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | 3 | }); | |||||||||||
| 65 | } else { | ||||||||||||
| 66 | _create_method("${dest_pkg}::${name}", | ||||||||||||
| 67 | 4 | ▶ | sub ($self, @args) { $get_obj->($self)->$method($code->($self, @args)) }); | ||||||||||
|
Subroutine
hop: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | 4 | my $method_cb = "${method}_cb"; | |||||||||||
| 69 | _create_method("${dest_pkg}::${name}_cb", | ||||||||||||
| 70 | 4 | ▶ | sub ($self, @args) { $get_obj->($self)->$method_cb($code->($self, @args)) }); | ||||||||||
|
Subroutine
foo_cb: called CC=1 SCAR=0.0
| |||||||||||||
| 71 | 4 | my $method_p = "${method}_p"; | |||||||||||
| 72 | _create_method("${dest_pkg}::${name}_p", | ||||||||||||
| 73 | 4 | ▶ | sub ($self, @args) { $get_obj->($self)->$method_p($code->($self, @args)) }); | ||||||||||
|
Subroutine
foo_p: called CC=1 SCAR=0.0
| |||||||||||||
| 74 | } | ||||||||||||
| 75 | 7 | return; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 2 | ▶ | sub _wrap_several_methods ($methods, $target, $code, $cb = undef) { | ||||||||||
|
Subroutine
_wrap_several_methods: called CC=4 SCAR=13.9
| |||||||||||||
| 79 | 2 | for my $m (@{$methods}) { | |||||||||||
| 80 | 2 | ▶ | if (ref($target) eq 'CODE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | # The last argument (2) is the number of frame to skip in wrap_method to | ||||||||||||
| 82 | # find the actual caller. | ||||||||||||
| 83 | 1 | wrap_method($m, $target, $m, $code, $cb, 2); | |||||||||||
| 84 | } else { | ||||||||||||
| 85 | 1 | wrap_method($m, "${target}::${m}", $code, $cb, 2); | |||||||||||
| 86 | } | ||||||||||||
| 87 | } | ||||||||||||
| 88 | 2 | return; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | # This is not documented for now, although it would be easy to do so. We mostly | ||||||||||||
| 92 | # expose it to test _wrap_several_methods and still have 2 hops in the | ||||||||||||
| 93 | # call-stack. | ||||||||||||
| 94 | 2 | ▶ | sub wrap_method_sets ($methods, $target, $code, $cb = undef) { | ||||||||||
|
Subroutine
wrap_method_sets: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 95 | 2 | return _wrap_several_methods($methods, $target, $code, $cb = undef); | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 0 | ▶ | sub wrap_all_methods ($target, $code, $cb = undef) { | ||||||||||
|
Subroutine
wrap_all_methods: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 99 | 0 | return _wrap_several_methods(\@UserAgent::Any::Impl::Helper::METHODS, $target, $code, $cb); | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | sub wrap_get_like_methods ($target, $code, $cb = undef) { | ||||||||||
|
Subroutine
wrap_get_like_methods: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 103 | 0 | return _wrap_several_methods(\@UserAgent::Any::Impl::Helper::METHODS_WITHOUT_DATA, | |||||||||||
| 104 | $target, $code, $cb); | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub wrap_post_like_methods ($target, $code, $cb = undef) { | ||||||||||
|
Subroutine
wrap_post_like_methods: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 108 | 0 | return _wrap_several_methods(\@UserAgent::Any::Impl::Helper::METHODS_WITH_DATA, $target, $code, | |||||||||||
| 109 | $cb); | ||||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 1 | 1; | |||||||||||
| 113 | |||||||||||||
| 114 | __END__ | ||||||||||||