lib/Dancer2/Core/Cookie.pm

filter: Statementstmt 91.5% 65 / 71 Branchbran 76.6% 23 / 30 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 94.4% 17 / 18 Podpod 33.3% 1 / 3 totaltotal 84.6% 110 / 130 CC 18 SCAR 14.7
CC
18
Cov
86%
pp_to_header
8.0
BEGIN
7.8
xs_to_header
6.0
CRAP 4.3
« lib/Dancer2/Core/Runner.pm ↑ Summary lib/Dancer2/Core/Role/Hookable.pm »
1 package Dancer2::Core::Cookie;
2 # ABSTRACT: A cookie representing class
3 193 $Dancer2::Core::Cookie::VERSION = '2.3.0';
4 193 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 193 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 193 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 193 ▶use Dancer2::Core::Time;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 193 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 193 ▶use Ref::Util qw< is_arrayref is_hashref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 193 ▶use overload '""' => \&_get_value;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 193 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=20.5
13 my $try_xs =
14 exists($ENV{PERL_HTTP_XSCOOKIES}) ? !!$ENV{PERL_HTTP_XSCOOKIES} :
15 exists($ENV{PERL_ONLY}) ? !$ENV{PERL_ONLY} :
16 193 ▶ 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $ENV{'PERL_HTTP_XSCOOKIES'} ? : 0 193 193
exists $ENV{'PERL_ONLY'} ? : 0 193 193
17  
18 193 my $use_xs = 0;
19 193 ▶ $try_xs and eval {
Branch: 1 branch
BranchTrueFalseTotal
$try_xs and eval { do { require HTTP::XSCookies; ++$use_xs } } 193 0 193
20 193 require HTTP::XSCookies;
21 0 $use_xs++;
22 };
23 193 ▶ if ( $use_xs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_xs) { } 0 193 193
24 0 *to_header = \&xs_to_header;
25 }
26 else {
27 193 *to_header = \&pp_to_header;
28 }
29 193 ▶ *_USE_XS = $use_xs ? sub () { !!1 } : sub () { !!0 };
Branch: 1 branch
BranchTrueFalseTotal
$use_xs ? : 0 193 193
30 }
31  
32 0 ▶sub xs_to_header {
Subroutine xs_to_header: not called CC=2 SCAR=17.9
Pod undocumented
33 0 my $self = shift;
34  
35 # HTTP::XSCookies can't handle multi-value cookies.
36 0 ▶ return $self->pp_to_header(@_) if @{[ $self->value ]} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{[$self->value];} > 1 0 0 0
37  
38 0 return HTTP::XSCookies::bake_cookie(
39 $self->name,
40 { value => $self->value,
41 path => $self->path,
42 domain => $self->domain,
43 expires => $self->expires,
44 httponly => $self->http_only,
45 secure => $self->secure,
46 samesite => $self->same_site,
47 }
48 );
49 }
50  
51 136 ▶sub pp_to_header {
Subroutine pp_to_header: called CC=8 SCAR=20.8
Pod undocumented
52 136 my $self = shift;
53  
54 136 my $value = join( '&', map uri_escape($_), $self->value );
55 136 ▶ my $no_httponly = defined( $self->http_only ) && $self->http_only == 0;
Condition66%2/3
defined $self->http_only && $self->http_only == 0
!ll&&!rl&&r
01279
MC/DC50%1/2
defined $self->http_only && $self->http_only == 0
defined $self->http_only $self->http_only == 0
Truth table66%2/3
A && B
A= defined $self->http_onlyB= $self->http_only == 0
ABresult
0X0
100
111
56  
57 136 my @headers = $self->name . '=' . $value;
58 136 ▶ push @headers, "Path=" . $self->path if $self->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 135 1 136
59 136 ▶ push @headers, "Expires=" . $self->expires if $self->expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expires 19 117 136
60 136 ▶ push @headers, "Domain=" . $self->domain if $self->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->domain 2 134 136
61 136 ▶ push @headers, "SameSite=" . $self->same_site if $self->same_site;
Branch: 1 branch
BranchTrueFalseTotal
if $self->same_site 9 127 136
62 136 ▶ push @headers, "Secure" if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 4 132 136
63 136 ▶ push @headers, 'HttpOnly' unless $no_httponly;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_httponly 127 9 136
64  
65 136 return join '; ', @headers;
66 }
67  
68 has value => (
69 is => 'rw',
70 isa => ArrayRef,
71 required => 0,
72 225 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
73 225 my $value = shift;
74 225 ▶ my @values =
Branch: 2 branches
BranchTrueFalseTotal
is_arrayref($value) ? : 78 147 225
is_hashref($value) ? : 1 146 147
75 is_arrayref($value) ? @$value
76 : is_hashref($value) ? %$value
77 : ($value);
78 225 return [@values];
79 },
80 193 );
81  
82 265 ▶around value => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
83 265 my $orig = shift;
84 265 my $self = shift;
85 265 my $array = $orig->( $self, @_ );
86 265 ▶ return wantarray ? @$array : $array->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 140 125 265
87 193 };
88  
89 3 ▶sub values {
Subroutine values: called CC=2 SCAR=7.1
Pod documented
90 3 my $self = shift;
91 3 ▶ return @{ $self->{'value'} || [] };
Condition50%1/2
$self->{'value'} || []
l!l
30
MC/DC0%0/1
$self->{'value'} || []
$self->{'value'}
Truth table50%1/2
A
A= $self->{'value'}
Aresult
11
00
92 }
93  
94 # this is only for overloading; need a real sub to refer to, as the Moose
95 # attribute accessor won't be available at that point.
96 54 ▶sub _get_value { shift->value }
Subroutine _get_value: called CC=1 SCAR=0.0
97  
98 193 has name => (
99 is => 'rw',
100 isa => Str,
101 required => 1,
102 );
103  
104 has expires => (
105 is => 'rw',
106 isa => Str,
107 required => 0,
108 36 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 36 Dancer2::Core::Time->new( expression => $_[0] )->gmt_string;
110 },
111 193 );
112  
113 193 has domain => (
114 is => 'rw',
115 isa => Str,
116 required => 0,
117 );
118  
119 has path => (
120 is => 'rw',
121 isa => Str,
122 114 ▶ default => sub {'/'},
Subroutine __ANON__: called CC=1 SCAR=0.0
123 193 predicate => 1,
124 );
125  
126 has secure => (
127 is => 'rw',
128 isa => Bool,
129 required => 0,
130 111 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
131 193 );
132  
133 has http_only => (
134 is => 'rw',
135 isa => Bool,
136 required => 0,
137 108 ▶ default => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
138 193 );
139  
140 193 has same_site => (
141 is => 'rw',
142 isa => Enum[qw[Strict Lax None]],
143 required => 0,
144 );
145  
146 193 1;
147  
148 __END__
« lib/Dancer2/Core/Runner.pm ↑ Summary lib/Dancer2/Core/Role/Hookable.pm »