Plugin.pm

filter: Statementstmt 97.8% 134 / 137 Branchbran 91.6% 55 / 60 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 25 / 25 Podpod 75.0% 6 / 8 totaltotal 94.2% 230 / 244 CC 35 SCAR 14.7
CC
35
Cov
95%
load_components
11.0
__ANON__
6.0
make_accessor
4.0
CRAP 4.4
« Component.pm ↑ Summary Plugin/Web/Request.pm »
1 package Blosxom::Plugin;
2 9 ▶use 5.008_009;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '0.02004';
8  
9 2 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
10 2 my $class = shift;
11 2 my $component = scalar caller;
12 9 ▶ my $stash = do { no strict 'refs'; \%{"$component\::"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 my %is_excluded;
15 2 ▶ while ( my ($method, $glob) = each %{$stash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$stash;}) 274 2 276
16 274 ▶ next unless defined *{$glob}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$glob;}{"CODE"} 246 28 274
17 28 $is_excluded{$method}++;
18 }
19  
20 2 my ( @requires, @accessors );
21  
22 my @exports = (
23 1 ▶ requires => sub { shift; push @requires, @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 ▶ mk_accessors => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
25 1 my $pkg = shift;
26 0 ▶ my @args = ref $_[0] eq 'HASH' ? %{$_[0]} : map { $_ => undef } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 0 1
27 1 push @accessors, @args,
28 },
29 2 ▶ init => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
30 2 my ( $comp, $plugin ) = @_;
31  
32 2 ▶ if ( my @methods = grep { !$plugin->can($_) } @requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@methods) = grep({not $plugin->can($_);} @requires)) 1 1 2
33 1 my $methods = join ', ', @methods;
34 1 croak "Can't apply '$comp' to '$plugin' - missing $methods";
35 }
36  
37 1 ▶ for ( my $i = 0; $i < @accessors; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @accessors) 2 1 3
38 2 $plugin->add_attribute( @accessors[$i, $i+1] );
39 }
40  
41 1 ▶ while ( my ($method, $glob) = each %{$stash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$stash;}) 7 1 8
42 7 ▶ if ( my $code = *{$glob}{CODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = *{$glob;}{"CODE"}) 5 2 7
43 5 ▶ next if $is_excluded{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $is_excluded{$method} 4 1 5
44 1 $plugin->add_method( $method => $code );
45 }
46 }
47  
48 1 return;
49 },
50 2 );
51  
52 # export mixin methods
53 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶ while ( my ( $method, $code ) = splice @exports, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@exports, 0, 2)) 6 2 8
55 6 *{ "$component\::$method" } = $code;
56 6 $is_excluded{ $method }++;
57 }
58  
59 2 return;
60 }
61  
62 9 my %attribute_of;
63  
64 2 ▶sub mk_accessors {
Subroutine mk_accessors: called CC=3 SCAR=11.1
Pod documented
65 2 my $package = shift;
66 0 ▶ my @accessors = ref $_[0] eq 'HASH' ? %{ $_[0] } : map { $_ => undef } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 0 2
67  
68 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶ while ( my ($field, $default) = splice @accessors, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@accessors, 0, 2)) 6 2 8
70 6 *{"$package\::$field"} = $package->make_accessor($field, $default);
71 }
72  
73 2 return;
74 }
75  
76 11 ▶sub make_accessor {
Subroutine make_accessor: called CC=4 SCAR=13.9
Pod undocumented
77 11 my $package = shift;
78 11 my $name = shift;
79 11 my $default = shift;
80 11 ▶ my $attribute = $attribute_of{$package} ||= {};
Condition100%2/2
$attribute_of{$package} ||= {}
l!l
65
MC/DC100%1/1
$attribute_of{$package} ||= {}
$attribute_of{$package}
Truth table100%2/2
A
A= $attribute_of{$package}
Aresult
11
00
81  
82 11 ▶ if ( ref $default eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $default eq 'CODE') { } 4 7 11
elsif (defined $default) { } 6 1 7
83 4 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
84 4 ▶ return $attribute->{$name} = $_[1] if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 1 3 4
85 3 ▶ return $attribute->{$name} if exists $attribute->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attribute->{$name} 1 2 3
86 2 return $attribute->{$name} = $package->$default;
87 4 };
88 }
89 elsif ( defined $default ) {
90 7 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
91 7 ▶ return $attribute->{$name} = $_[1] if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 2 5 7
92 5 ▶ return $attribute->{$name} if exists $attribute->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attribute->{$name} 0 5 5
93 5 return $attribute->{$name} = $default;
94 6 };
95 }
96 else {
97 2 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
98 2 ▶ @_ > 1 ? $attribute->{$name} = $_[1] : $attribute->{$name};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 1 2
99 1 };
100 }
101  
102 0 return;
103 }
104  
105 2 ▶sub end { %{ $attribute_of{$_[0]} } = () if exists $attribute_of{$_[0]} }
Subroutine end: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if exists $attribute_of{$_[0]} 1 1 2
106  
107 4 ▶sub dump {
Subroutine dump: called CC=2 SCAR=7.0
Pod documented
108 4 my $package = shift;
109 4 require Data::Dumper;
110 4 ▶ local $Data::Dumper::Maxdepth = shift || 1;
Condition50%1/2
shift() || 1
l!l
04
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
111 4 Data::Dumper::Dumper( $attribute_of{$package} );
112 }
113  
114 8 ▶sub component_base_class { __PACKAGE__ }
Subroutine component_base_class: called CC=1 SCAR=0.0
Pod undocumented
115  
116 9 ▶sub load_components {
Subroutine load_components: called CC=11 SCAR=24.0
Pod documented
117 9 my $package = shift;
118 9 my @args = @_;
119 9 my $prefix = $package->component_base_class;
120  
121 9 my ( $component, %is_loaded, %has_conflict, %code_of );
122  
123 local *add_component
124 9 ▶ = sub { push @args, @_ > 2 ? @_[1, 2] : $_[1] };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 ? : 0 3 3
125  
126 16 ▶ local *add_method = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
127 16 my ( $pkg, $method, $code ) = @_;
128 16 ▶ unless ( defined &{"$package\::$method"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{"${package}::$method";}) 14 2 16
129 14 ▶ push @{ $has_conflict{$method} ||= [] }, $component;
Condition100%2/2
$has_conflict{$method} ||= []
l!l
113
MC/DC100%1/1
$has_conflict{$method} ||= []
$has_conflict{$method}
Truth table100%2/2
A
A= $has_conflict{$method}
Aresult
11
00
130 14 $code_of{ $method } = $code;
131 }
132 9 };
133  
134 9 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 17 8 25
135 17 $component = do {
136 17 my $class = shift @args;
137  
138 17 ▶ if ( $class !~ s/^\+// and $class !~ /^$prefix/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ s/^\+// or $class =~ /^$prefix/) 3 14 17
Condition66%2/3
$class =~ s/^\+// or $class =~ /^$prefix/
l!l&&r!l&&!r
1403
MC/DC50%1/2
$class =~ s/^\+// or $class =~ /^$prefix/
$class =~ s/^\+// $class =~ /^$prefix/
Truth table66%2/3
A or B
A= $class =~ s/^\+//B= $class =~ /^$prefix/
ABresult
1X1
011
000
139 3 $class = "$prefix\::$class";
140 }
141  
142 17 ▶ if ( $is_loaded{$class}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_loaded{$class}++) 2 15 17
143 2 ▶ shift @args if ref $args[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 0 2 2
144 2 next;
145 }
146  
147 15 ( my $file = $class ) =~ s{::}{/}g;
148 15 require "$file.pm";
149  
150 15 $class;
151 };
152  
153 15 ▶ my $config = ref $args[0] eq 'HASH' ? shift @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 2 13 15
154  
155 15 $component->init( $package, $config );
156 }
157  
158 8 ▶ if ( %code_of ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%code_of) 6 2 8
159 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 6 ▶ while ( my ( $method, $components ) = each %has_conflict ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %has_conflict) 13 6 19
161 13 ▶ delete $has_conflict{ $method } if @{ $components } == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$components;} == 1 12 1 13
162 13 *{ "$package\::$method" } = $code_of{ $method };
163 }
164 }
165  
166 8 ▶ if ( %has_conflict ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%has_conflict) 1 7 8
167 croak join "\n", map {
168 1 "Due to a method name conflict between components " .
169 1 "'" . join( ' and ', sort @{ $has_conflict{$_} } ) . "', " .
170 "the method '$_' must be implemented by '$package'";
171 } keys %has_conflict;
172 }
173  
174 7 return;
175 }
176  
177 5 ▶sub add_attribute {
Subroutine add_attribute: called CC=1 SCAR=0.0
Pod documented
178 5 my ( $pkg, $name, $default ) = @_;
179 5 $pkg->add_method( $name => $pkg->make_accessor($name, $default) );
180 }
181  
182 1 ▶sub has_method { defined &{"$_[0]::$_[1]"} }
Subroutine has_method: called CC=1 SCAR=0.0
Pod documented
183  
184 9 1;
185  
186 __END__
« Component.pm ↑ Summary Plugin/Web/Request.pm »