Sprocket/Common.pm

filter: Statementstmt 71.1% 42 / 59 Branchbran 37.5% 6 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 5 totaltotal 58.8% 60 / 102 CC 14 SCAR 16.0
CC
14
Cov
62%
uri_escape
12.0
_try_utf8
6.0
uri_unescape
6.0
CRAP 5.0
« Sprocket/Client.pm ↑ Summary Sprocket/Connection.pm »
1 package Sprocket::Common;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our %hex_chr;
8 8 our %chr_hex;
9 8 our $super_event = 'sub super_event {'
10 . 'my $self = shift; my $caller = ( caller( 1 ) )[ 3 ];'
11 . '$caller =~ s/.*::(.+)$/$1/; $caller= "SUPER::$caller";'
12 . 'my $ret = $self->$caller( @_ ); unshift( @_, $self );'
13 . 'push( @_, $ret ); return @_; }';
14  
15 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
16 8 for ( 0 .. 255 ) {
17 2048 my $h = sprintf( "%%%02X", $_ );
18 2048 my $c = chr($_);
19 2048 $chr_hex{$c} = $h;
20 2048 $hex_chr{lc($h)} = $hex_chr{uc($h)} = $c;
21 }
22 }
23  
24 68 ▶sub import {
Subroutine import: called CC=5 SCAR=17.6
25 68 my ( $class, $args ) = @_;
26 68 my $package = caller();
27  
28 68 my @exports = qw(
29 uri_unescape
30 uri_escape
31 adjust_params
32 gen_uuid
33 new_uuid
34 );
35  
36 68 ▶ push( @exports, @_ ) if ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 68 0 68
37
38 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 68 foreach my $sub ( @exports ) {
40 408 ▶ if ( $sub eq 'super_event' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq 'super_event') { } 0 408 408
41 # XXX We must define this sub in the class because it uses SUPER
42 # I don't know of any other way to do this, yet.
43 eval ( "package $package;" . $super_event )
44 0 ▶ if ( !defined *{ $package . '::super_event' } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$package . '::super_event';} 0 0 0
45 } else {
46 408 *{ $package . '::' . $sub } = \&$sub;
47 }
48 }
49 }
50  
51 0 ▶sub uri_escape {
Subroutine uri_escape: not called CC=3 SCAR=24.8
Pod undocumented
52 0 ▶ my $es = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $es = shift() or return 0 0 0
53 0 ▶ $es =~ s/([^A-Za-z0-9\-_.!~*'()])/$chr_hex{$1}||_try_utf8($1)/ge;
Condition0%0/3
$Sprocket::Common::chr_hex{$1} || _try_utf8($1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Sprocket::Common::chr_hex{$1} || _try_utf8($1)
$Sprocket::Common::chr_hex{$1} _try_utf8($1)
Truth table0%0/3
A || B
A= $Sprocket::Common::chr_hex{$1}B= _try_utf8($1)
ABresult
1X1
011
000
54 0 return $es;
55 }
56  
57 0 ▶sub _try_utf8 {
Subroutine _try_utf8: not called CC=2 SCAR=17.9
58 0 my $c = shift;
59 0 $c = eval { utf8::encode($c); };
60 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
61 0 warn $@;
62 0 return '';
63 }
64 0 return $c
65 }
66  
67 0 ▶sub uri_unescape {
Subroutine uri_unescape: not called CC=2 SCAR=17.9
Pod undocumented
68 0 ▶ my $es = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $es = shift() or return 0 0 0
69 0 $es =~ tr/+/ /; # foo=this+is+a+test
70 0 $es =~ s/(%[0-9a-fA-F]{2})/$hex_chr{$1}/gs;
71 0 return $es;
72 }
73  
74 # ThisIsCamelCase -> this_is_camel_case
75 # my %opts = &adjust_params;
76 # my $t = adjust_params($f); # $f being a hashref
77 30 ▶sub adjust_params {
Subroutine adjust_params: called CC=5 SCAR=16.1
Pod undocumented
78 30 ▶ my $o = ( $#_ == 0 && ref( $_[ 0 ] ) ) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 && ref $_[0] ? : 10 20 30
Condition66%2/3
$#_ == 0 and ref $_[0]
!ll&&!rl&&r
20010
MC/DC50%1/2
$#_ == 0 and ref $_[0]
$#_ == 0 ref $_[0]
Truth table66%2/3
A and B
A= $#_ == 0B= ref $_[0]
ABresult
0X0
100
111
79 30 foreach my $k ( keys %$o ) {
80 90 local $_ = "$k";
81 90 s/([A-Z][a-z]+)/lc($1)."_"/ge; s/_$//;
82 90 $o->{+lc} = delete $o->{$k};
83 }
84 30 ▶ return wantarray ? %$o : $o;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 20 10 30
85 }
86  
87 28 ▶sub gen_uuid {
Subroutine gen_uuid: called CC=1 SCAR=0.0
Pod undocumented
88 28 my $from = shift;
89 28 my $u = Data::UUID->new();
90 28 my $uuid = $u->create_from_name( "cc.sprocket", "$from" );
91 28 return lc( $u->to_string( $uuid ) );
92 }
93  
94 29 ▶sub new_uuid {
Subroutine new_uuid: called CC=1 SCAR=0.0
Pod undocumented
95 29 return lc( new Data::UUID->create_str() );
96 }
97  
98 8 1;
« Sprocket/Client.pm ↑ Summary Sprocket/Connection.pm »