blib/lib/Mojolicious/Plugin/Host.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 96.1% 25 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 97.8% 90 / 92 CC 16 SCAR 18.7
CC
16
Cov
97%
_parse_and_validate_config
11.0
register
4.0
__ANON__
2.0
CRAP 6.5
↑ Summary
1 package Mojolicious::Plugin::Host;
2 5 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Mojo::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.01';
7  
8 27 ▶sub register {
Subroutine register: called CC=4 SCAR=13.9
Pod documented
9 27 my (undef, $app, $config) = @_;
10  
11 27 my ($helper, $www) = _parse_and_validate_config($config);
12 12 my $sub;
13 12 ▶ if (not defined $www) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $www) { } 6 6 12
elsif ($www eq 'always') { } 3 3 6
elsif ($www eq 'never') { } 3 0 3
14 6 ▶ $sub = sub { $_[0]->req->url->to_abs->host };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 } elsif ($www eq 'always') {
16 4 ▶ $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
17 4 my $host = $_[0]->req->url->to_abs->host;
18 4 ▶ if (index($host, 'www.') != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($host, "www.") != 0) 2 2 4
19 2 $host = "www.$host";
20 }
21  
22 4 return $host;
23 3 };
24 } elsif ($www eq 'never') {
25 4 ▶ $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
26 4 my $host = $_[0]->req->url->to_abs->host;
27 4 ▶ if (index($host, 'www.') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($host, "www.") == 0) 2 2 4
28 2 substr $host, 0, 4, '';
29 }
30  
31 4 return $host;
32 3 };
33 } else {
34 0 Carp::croak qq{unknown value provided for www: '$www'};
35 }
36  
37 12 $app->helper($helper => $sub);
38 }
39  
40 27 ▶sub _parse_and_validate_config {
Subroutine _parse_and_validate_config: called CC=11 SCAR=24.0
41 27 my ($config) = @_;
42  
43 27 my $helper;
44 27 ▶ if (exists $config->{helper}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{'helper'}) { } 10 17 27
45 10 $helper = delete $config->{helper};
46  
47 10 my $ref = ref $helper;
48 10 ▶ Carp::croak qq{helper must be a string, but was '$ref'} if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 2 8 10
49 8 ▶ Carp::croak 'helper must be non-empty' if not defined $helper or $helper eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $helper or $helper eq "" 2 6 8
Condition100%3/3
not defined $helper or $helper eq ""
l!l&&r!l&&!r
116
MC/DC100%2/2
not defined $helper or $helper eq ""
not defined $helper $helper eq ""
Truth table100%3/3
not(A) or B
A= not defined $helperB= $helper eq ""
ABresult
1X1
011
000
50 } else {
51 17 $helper = 'host';
52 }
53  
54 23 my $www;
55 23 ▶ if (exists $config->{www}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"www"}) 15 8 23
56 15 $www = delete $config->{www};
57  
58 15 my $ref = ref $www;
59 15 ▶ Carp::croak qq{www must be a string, but was '$ref'} if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 2 13 15
60 13 ▶ Carp::croak 'www must be non-empty' if not defined $www or $www eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $www or $www eq "" 2 11 13
Condition100%3/3
not defined $www or $www eq ""
l!l&&r!l&&!r
1111
MC/DC100%2/2
not defined $www or $www eq ""
not defined $www $www eq ""
Truth table100%3/3
not(A) or B
A= not defined $wwwB= $www eq ""
ABresult
1X1
011
000
61 Carp::croak qq{www must be either 'always' or 'never', but was '$www'}
62 11 ▶ unless grep { $www eq $_ } 'always', 'never';
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$www eq $_;} "always", "never" 3 8 11
63 }
64  
65 16 ▶ Carp::croak 'unknown keys/values: ' . Mojo::Util::dumper $config if %$config;
Branch: 1 branch
BranchTrueFalseTotal
if %$config 4 12 16
66  
67 12 return $helper, $www;
68 }
69  
70 5 1;
71 __END__
↑ Summary