blib/lib/Dancer2/Core/Cookie.pm

filter: Statementstmt 91.5% 65 / 71 Branchbran 75.0% 24 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 94.4% 17 / 18 Podpod 33.3% 1 / 3 totaltotal 85.2% 110 / 129 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
« blib/lib/Dancer2/Core/Runner.pm ↑ Summary blib/lib/Dancer2/Serializer/JSON.pm »
1 package Dancer2::Core::Cookie;
2 # ABSTRACT: A cookie representing class
3 189 $Dancer2::Core::Cookie::VERSION = '2.2.1';
4 189 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 189 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 189 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 189 ▶use Dancer2::Core::Time;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 189 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 189 ▶use Ref::Util qw< is_arrayref is_hashref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 189 ▶use overload '""' => \&_get_value;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 BEGIN {
13 my $try_xs =
14 exists($ENV{PERL_HTTP_XSCOOKIES}) ? !!$ENV{PERL_HTTP_XSCOOKIES} :
15 exists($ENV{PERL_ONLY}) ? !$ENV{PERL_ONLY} :
16 189 ▶ 1;
Subroutine BEGIN: called CC=6 SCAR=20.5
Branch: 2 branches
BranchTrueFalseTotal
exists $ENV{'PERL_HTTP_XSCOOKIES'} ? : 0 189 189
exists $ENV{'PERL_ONLY'} ? : 0 189 189
17  
18 189 my $use_xs = 0;
19 189 ▶ $try_xs and eval {
Branch: 1 branch
BranchTrueFalseTotal
$try_xs and eval { do { require HTTP::XSCookies; ++$use_xs } } 189 0 189
20 189 require HTTP::XSCookies;
21 0 $use_xs++;
22 };
23 189 ▶ if ( $use_xs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_xs) { } 0 189 189
24 0 *to_header = \&xs_to_header;
25 }
26 else {
27 189 *to_header = \&pp_to_header;
28 }
29 189 ▶ *_USE_XS = $use_xs ? sub () { !!1 } : sub () { !!0 };
Branch: 1 branch
BranchTrueFalseTotal
$use_xs ? : 0 189 189
30 }
31  
32 sub xs_to_header {
33 0 ▶ my $self = shift;
Subroutine xs_to_header: not called CC=2 SCAR=17.9
Pod undocumented
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 sub pp_to_header {
52 136 ▶ my $self = shift;
Subroutine pp_to_header: called CC=8 SCAR=20.8
Pod undocumented
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 coerce => sub {
73 225 ▶ my $value = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
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 189 );
81  
82 around value => sub {
83 315 ▶ my $orig = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
84 315 my $self = shift;
85 315 my $array = $orig->( $self, @_ );
86 315 ▶ return wantarray ? @$array : $array->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 140 175 315
87 189 };
88  
89 sub values {
90 3 ▶ my $self = shift;
Subroutine values: called CC=2 SCAR=7.1
Pod documented
91 3 ▶ return @{ $self->{'value'} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"value"} or [] 0 3 3
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 104 ▶sub _get_value { shift->value }
Subroutine _get_value: called CC=1 SCAR=0.0
97  
98 189 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 coerce => sub {
109 36 ▶ Dancer2::Core::Time->new( expression => $_[0] )->gmt_string;
Subroutine __ANON__: called CC=1 SCAR=0.0
110 },
111 189 );
112  
113 189 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 189 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 189 );
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 189 );
139  
140 189 has same_site => (
141 is => 'rw',
142 isa => Enum[qw[Strict Lax None]],
143 required => 0,
144 );
145  
146 189 1;
147  
148 __END__
« blib/lib/Dancer2/Core/Runner.pm ↑ Summary blib/lib/Dancer2/Serializer/JSON.pm »