blib/lib/Catalyst/Plugin/CachedUriForAction.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 90.4% 38 / 42 Conditioncond 84.2% 16 / 19 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 9 / 9 Podpod 50.0% 1 / 2 totaltotal 92.5% 162 / 175 CC 37 SCAR 32.8
CC
37
Cov
94%
uri_for_action
34.6
setup_finalize
4.0
BEGIN
1.0
CRAP 26.6
↑ Summary
1 4 ▶use 5.008005; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Catalyst::Plugin::CachedUriForAction;
4  
5 4 our $VERSION = '1.005';
6  
7 4 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Encode::XS 'uri_encode_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub CACHE_KEY () { __PACKAGE__ . '::action_uri_info' }
12  
13 3 ▶sub setup_finalize {
Subroutine setup_finalize: called CC=4 SCAR=13.9
Pod undocumented
14 3 my $c = shift;
15 3 $c->maybe::next::method( @_ );
16  
17 3 my $cache = \%{ $c->dispatcher->{(CACHE_KEY)} };
18 3 for my $action ( values %{ $c->dispatcher->_action_hash } ) {
19 174 my $xa = $c->dispatcher->expand_action( $action );
20 174 my $n_caps = $xa->number_of_captures;
21  
22 # not an action that a request can be dispatched to?
23 174 ▶ next if not defined $c->dispatcher->uri_for_action( $action, [ ('dummy') x $n_caps ] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->dispatcher->uri_for_action($action, [("dummy") x $n_caps]) 126 48 174
24  
25 48 my $n_args = $xa->number_of_args; # might be undef to mean "any number"
26 48 ▶ my $tmpl = $c->uri_for( $action, [ ("\0\0\0\0") x $n_caps ], ("\0\0\0\0") x ( $n_args || 0 ) );
Condition100%2/2
$n_args || 0
l!l
1533
MC/DC100%1/1
$n_args || 0
$n_args
Truth table100%2/2
A
A= $n_args
Aresult
11
00
27 48 my ( $prefix, @part ) = split /%00%00%00%00/, $tmpl, -1;
28 48 $prefix =~ s!\A/!!;
29 48 $cache->{ '/' . $action->reverse } = [ $n_caps, $n_args, \@part, $prefix ];
30 }
31 }
32  
33 57 ▶sub uri_for_action {
Subroutine uri_for_action: called CC=34 SCAR=35.4
Pod documented
34 57 my $c = shift;
35  
36 57 my $dispatcher = $c->dispatcher;
37 57 ▶ my $cache = $dispatcher && $dispatcher->{(CACHE_KEY)}
Branch: 1 branch
BranchTrueFalseTotal
my $cache = $dispatcher && $dispatcher->{"Catalyst::Plugin::CachedUriForAction::action_uri_info"} or return $c->next::method(@_) 57 0 57
Condition33%1/3
$dispatcher && $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
!ll&&!rl&&r
0057
MC/DC0%0/2
$dispatcher && $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
$dispatcher $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
Truth table33%1/3
A && B
A= $dispatcherB= $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
ABresult
0X0
100
111
38 or return $c->next::method( @_ ); # fall back if called too early
39  
40 57 my $action = shift;
41 57 ▶ my $captures = @_ && 'ARRAY' eq ref $_[0] ? shift : [];
Branch: 1 branch
BranchTrueFalseTotal
@_ && 'ARRAY' eq ref $_[0] ? : 30 27 57
Condition100%3/3
@_ and "ARRAY" eq ref $_[0]
!ll&&!rl&&r
72030
MC/DC100%2/2
@_ and "ARRAY" eq ref $_[0]
@_ "ARRAY" eq ref $_[0]
Truth table100%3/3
A and B
A= @_B= "ARRAY" eq ref $_[0]
ABresult
0X0
100
111
42 57 ▶ my $fragment = @_ && 'SCALAR' eq ref $_[-1] ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && 'SCALAR' eq ref $_[-1] ? : 2 55 57
Condition100%3/3
@_ and "SCALAR" eq ref $_[-1]
!ll&&!rl&&r
16392
MC/DC100%2/2
@_ and "SCALAR" eq ref $_[-1]
@_ "SCALAR" eq ref $_[-1]
Truth table100%3/3
A and B
A= @_B= "SCALAR" eq ref $_[-1]
ABresult
0X0
100
111
43 57 ▶ my $params = @_ && 'HASH' eq ref $_[-1] ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && 'HASH' eq ref $_[-1] ? : 20 37 57
Condition100%3/3
@_ and "HASH" eq ref $_[-1]
!ll&&!rl&&r
172020
MC/DC100%2/2
@_ and "HASH" eq ref $_[-1]
@_ "HASH" eq ref $_[-1]
Truth table100%3/3
A and B
A= @_B= "HASH" eq ref $_[-1]
ABresult
0X0
100
111
44  
45 $action = '/' . $dispatcher->get_action_by_path( $action )->reverse
46 if ref $action
47 57 ▶ and do { local $@; eval { $action->isa( 'Catalyst::Action' ) } };
Branch: 1 branch
BranchTrueFalseTotal
if ref $action and do { local $@; eval { do { $action->isa("Catalyst::Action") } } } 1 56 57
Condition100%3/3
ref $action and do { local $@; eval { do { $action->isa("Catalyst::Action") } } }
!ll&&!rl&&r
5421
MC/DC100%2/2
ref $action and do { local $@; eval { do { $action->isa("Catalyst::Action") } } }
ref $action do { local $@; eval { do { $action->isa("Catalyst::Action") } } }
Truth table100%3/3
A and B
A= ref $actionB= do { local $@; eval { do { $action->isa("Catalyst::Action") } } }
ABresult
0X0
100
111
48  
49 57 ▶ my $info = $cache->{ $action }
Branch: 1 branch
BranchTrueFalseTotal
my $info = $cache->{$action} or Carp::croak("Can't find action for path '${action}' in uri_for_action") 55 2 57
50 or Carp::croak "Can't find action for path '$action' in uri_for_action";
51  
52 55 my ( $uri, $base ) = '';
53 55 ▶ if ( ref $c ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 41 14 55
54 41 $base = $c->request->base;
55 41 ▶ $uri = '/' if $$base !~ m!/\z!;
Branch: 1 branch
BranchTrueFalseTotal
unless $$base =~ m[/\z] 34 7 41
56 } else { # fallback if called as class method
57 14 $base = bless \( my $tmp = '' ), 'URI::_generic';
58 14 $uri = '/';
59 }
60  
61 55 my ( $n_caps, $n_args, $extra_parts ) = @$info;
62 55 $uri .= $info->[-1];
63  
64 # this is not very sensical but it has to be like this because it is what Catalyst does:
65 # the :Args() case (i.e. any number of args) is grouped with the :Args(0) case (i.e. no args)
66 # instead of being grouped with with the :Args(N) case (i.e. a fixed non-zero number of args)
67 55 ▶ if ( $n_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_args) { } 29 26 55
68 29 ▶ Carp::croak "Not enough captures for path '$action' (need $n_caps) in uri_for_action"
Branch: 1 branch
BranchTrueFalseTotal
if @$captures < $n_caps 2 27 29
69 if @$captures < $n_caps;
70 } else {
71 26 ▶ Carp::croak "Wrong number of captures for path '$action' (need $n_caps) in uri_for_action"
Branch: 1 branch
BranchTrueFalseTotal
if @$captures != $n_caps 2 24 26
72 if @$captures != $n_caps;
73 }
74  
75 # the following is carefully written to
76 # - loop over every input array exactly once
77 # - avoid any conditionals inside each loop body
78 # - use only simple loop forms that are specially optimised by the perl interpreter
79 51 my $i = -1;
80 51 ▶ if ( defined $n_args ) { # the non-slurpy case
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n_args) { } 32 19 51
81 32 ▶ Carp::croak "Wrong number of args+captures for path '$action' (need ".@$extra_parts.") in uri_for_action"
Branch: 1 branch
BranchTrueFalseTotal
if @$captures + @_ != @$extra_parts 6 26 32
82 if ( @$captures + @_ ) != @$extra_parts;
83 # and now since @$extra_parts is exactly the same length as @$captures and @_ combined
84 # iterate over those arrays and use a cursor into @$extra_parts to interleave its elements
85 26 for ( @$captures ) { ( $uri .= uri_encode_utf8 $_ ) .= $extra_parts->[ ++$i ] }
86 26 for ( @_ ) { ( $uri .= uri_encode_utf8 $_ ) .= $extra_parts->[ ++$i ] }
87 } else {
88 # in the slurpy case, the size of @$extra_parts is determined by $n_caps alone since $n_args was undef
89 # and as we checked above @$captures alone has at least length $n_caps
90 # so we will need all of @$captures to cover @$extra_parts, and may then still have some of it left over
91 # so iterate over @$extra_parts and use a cursor into @$captures to interleave its elements
92 0 for ( @$extra_parts ) { ( $uri .= uri_encode_utf8 $captures->[ ++$i ] ) .= $_ }
93 # and then append the rest of @$captures, and then everything from @_ after that
94 0 for ( ++$i .. $#$captures ) { ( $uri .= '/' ) .= uri_encode_utf8 $captures->[ $_ ] }
95 19 for ( @_ ) { ( $uri .= '/' ) .= uri_encode_utf8 $_ }
96 }
97  
98 45 $uri =~ s/%2B/+/g;
99 45 substr $uri, 0, 0, $$base;
100  
101 45 ▶ if ( defined $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 20 25 45
102 20 my $query = '';
103 20 ▶ my $delim = $URI::DEFAULT_QUERY_FORM_DELIMITER || '&';
Condition50%1/2
$URI::DEFAULT_QUERY_FORM_DELIMITER || '&'
l!l
020
MC/DC0%0/1
$URI::DEFAULT_QUERY_FORM_DELIMITER || '&'
$URI::DEFAULT_QUERY_FORM_DELIMITER
Truth table50%1/2
A
A= $URI::DEFAULT_QUERY_FORM_DELIMITER
Aresult
11
00
104 20 my ( $v, $enc_key );
105 20 for my $key ( sort keys %$params ) {
106 21 $v = $params->{ $key };
107 21 ▶ if ( 'ARRAY' ne ref $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' ne ref $v) { } 20 1 21
elsif (@$v) { } 1 0 1
108 20 ( $query .= $delim ) .= uri_encode_utf8 $key;
109 20 ▶ ( $query .= '=' ) .= uri_encode_utf8 $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 19 1 20
110 } elsif ( @$v ) {
111 1 $enc_key = $delim . uri_encode_utf8 $key;
112 1 for ( @$v ) {
113 2 $query .= $enc_key;
114 2 ▶ ( $query .= '=' ) .= uri_encode_utf8 $_ if defined;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 2 0 2
115 }
116 }
117 }
118 20 ▶ if ( '' ne $query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("" ne $query) 20 0 20
119 20 $query =~ s/%20/+/g;
120 20 ( $uri .= '?' ) .= substr $query, length $delim;
121 }
122 }
123  
124 45 ▶ if ( defined $fragment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fragment) 2 43 45
125 2 ( $uri .= '#' ) .= uri_encode_utf8 $$fragment;
126 }
127  
128 45 bless \$uri, ref $base;
129 }
130  
131 4 ▶BEGIN { delete $Catalyst::Plugin::CachedUriForAction::{'uri_encode_utf8'} }
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 4 1;
134  
135 __END__
↑ Summary