blib/lib/Catalyst/ActionRole/Methods.pm

filter: Statementstmt 88.2% 45 / 51 Branchbran 90.0% 18 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 81.6% 71 / 87 CC 13 SCAR 20.7
CC
13
Cov
86%
__ANON__
11.0
get_allowed_methods
3.1
__ANON__
2.0
CRAP 7.9
↑ Summary
1 package Catalyst::ActionRole::Methods;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.104';
6  
7 0 ▶around 'list_extra_info' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
8 0 my $orig = shift;
9 0 my $self = shift;
10 0 my $info = $self->$orig( @_ );
11 0 $info->{'HTTP_METHOD'} = [ $self->get_allowed_methods( $self->class, undef, $self->name ) ];
12 0 $info;
13 1 };
14  
15 6 ▶around 'dispatch', sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
16 6 my $orig = shift;
17 6 my $self = shift;
18 6 my $c = shift;
19  
20 6 my $return = $self->$orig($c, @_);
21  
22 6 my $class = $self->class;
23 6 my $controller = $c->component( $class );
24 6 my $method_name = $self->name;
25 6 my $req_method = $c->request->method;
26 6 my $suffix = uc $req_method;
27 6 my ( $rest_method, $code );
28  
29 {
30 6 $rest_method = $method_name . '_' . $suffix;
31  
32 9 ▶ if ( $code = $controller->action_for( $rest_method ) ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($code = $controller->action_for($rest_method)) { } 2 7 9
elsif ($code = $controller->can($rest_method)) { } 2 5 7
elsif ('OPTIONS' eq $suffix) { } 1 4 5
elsif ('HEAD' eq $suffix) { } 1 3 4
elsif ('not_implemented' eq $suffix) { } 1 2 3
33 2 my $sub_return = $c->forward( $code, $c->request->args );
34 2 ▶ return defined $sub_return ? $sub_return : $return;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub_return ? : 2 0 2
35 } elsif ( $code = $controller->can( $rest_method ) ) {
36 # nothing to do
37 } elsif ( 'OPTIONS' eq $suffix ) {
38 1 $c->response->status( 204 );
39 } elsif ( 'HEAD' eq $suffix ) {
40 1 $suffix = 'GET';
41 1 redo;
42 } elsif ( 'not_implemented' eq $suffix ) {
43 0 ( my $enc_req_method = $req_method ) =~ s[(["'&<>])]{ '&#'.(ord $1).';' }ge;
44 1 $c->response->status( 405 );
45 1 $c->response->content_type( 'text/html' );
46 1 $c->response->body(
47 '<!DOCTYPE html><title>405 Method Not Allowed</title>'
48 . "<p>The requested method $enc_req_method is not allowed for this URL.</p>"
49 );
50 } else {
51 2 $suffix = 'not_implemented';
52 2 redo;
53 }
54 }
55  
56 4 ▶ if ( not $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 2 2 4
57 2 my @allowed = $self->get_allowed_methods( $class, $c, $method_name );
58 2 ▶ $c->response->header( Allow => @allowed ? \@allowed : '' );
Branch: 1 branch
BranchTrueFalseTotal
@allowed ? : 1 1 2
59 }
60  
61 # localise stuff so we can dispatch the action 'as normal, but get
62 # different stats shown, and different code run.
63 # Also get the full path for the action, and make it look like a forward
64 4 ▶ local $self->{'code'} = $code || sub {};
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$code || sub { }
l!l&&r!l&&!r
220
MC/DC0%0/2
$code || sub { }
$code sub { }
Truth table66%2/3
A || B
A= $codeB= sub { }
ABresult
1X1
011
000
65 4 ( local $self->{'reverse'} = "-> $self->{'reverse'}" ) =~ s{[^/]+\z}{$rest_method};
66  
67 4 my $sub_return = $c->execute( $class, $self, @{ $c->request->args } );
68 4 ▶ defined $sub_return ? $sub_return : $return;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub_return ? : 4 0 4
69 1 };
70  
71 2 ▶sub get_allowed_methods {
Subroutine get_allowed_methods: called CC=3 SCAR=11.2
Pod documented
72 2 my ( $self, $controller, $c, $name ) = @_;
73 2 ▶ my $class = ref $controller || $controller; # backcompat
Condition33%1/3
ref $controller || $controller
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $controller || $controller
ref $controller $controller
Truth table33%1/3
A || B
A= ref $controllerB= $controller
ABresult
1X1
011
000
74 2 my %methods = map /^\Q$name\E\_(.+)()$/, $class->meta->get_all_method_names;
75 2 ▶ $methods{'HEAD'} = 1 if exists $methods{'GET'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $methods{"GET"} 1 1 2
76 2 delete $methods{'not_implemented'};
77 2 sort keys %methods;
78 }
79  
80 1 1;
81  
82 __END__
↑ Summary