Decouple.pm

filter: Statementstmt 100% 138 / 138 Branchbran 89.2% 25 / 28 Conditioncond 78.9% 15 / 19 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 34 / 34 Podpod 100% 15 / 15 totaltotal 95.1% 233 / 245 CC 30 SCAR 13.5
CC
30
Cov
96%
_set_code_substitutions
9.0
decouple
7.1
_build_imports
7.0
CRAP 3.8
↑ Summary Decouple/DB.pm »
1 # $Id$
2 package Devel::Decouple;
3
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use version; our $VERSION = qv(0.0.3);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 our @EXPORT = qw{ from function functions as default_sub preserved };
11
12 4 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use PPI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Monkey::Patch qw{ patch_package };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use List::MoreUtils qw{ uniq };
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 ### PUBLIC METHODS: ########################
19
20 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 17 my $class = shift;
22 17 return bless {}, $class;
23 }
24
25 15 ▶sub decouple {
Subroutine decouple: called CC=7 SCAR=19.6
Pod documented
26 15 my $self = shift;
27 15 ▶ my $module = shift || croak q{You must supply a module name to the 'decouple' method};
Condition50%1/2
shift() || croak(q[You must supply a module name to the 'decouple' method])
l!l
150
MC/DC0%0/1
shift() || croak(q[You must supply a module name to the 'decouple' method])
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
28 15 my @args = @_;
29 15 ▶ my $modules = shift @args if ref $args[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "ARRAY" 6 9 15
30
31 # build the params hash
32 15 my %params;
33 15 $params{_MODULE_} = $module;
34 15 ▶ $params{_DOCUMENT_} = Class::Inspector->resolved_filename( $module )
Branch: 1 branch
BranchTrueFalseTotal
$params{"_DOCUMENT_"} = "Class::Inspector"->resolved_filename($module) or croak("could not resolve the canonical name of $module") 15 0 15
35 or croak "could not resolve the canonical name of $module";
36 15 ▶ $params{_MODULES_} = $modules || '_ALL_';
Condition100%2/2
$modules || '_ALL_'
l!l
69
MC/DC100%1/1
$modules || '_ALL_'
$modules
Truth table100%2/2
A
A= $modules
Aresult
11
00
37 15 $params{_CODE_} = { @args };
38 15 ▶ $params{_CODE_}{_DEFAULT_} ||= sub { return };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$params{"_CODE_"}{"_DEFAULT_"} ||= sub { return; }
l!l&&r!l&&!r
690
MC/DC0%0/2
$params{"_CODE_"}{"_DEFAULT_"} ||= sub { return; }
$params{"_CODE_"}{"_DEFAULT_"} sub { return; }
Truth table66%2/3
A ||= B
A= $params{"_CODE_"}{"_DEFAULT_"}B= sub { return; }
ABresult
1X1
011
000
39
40 15 $self->{$_} = $params{$_} for keys %params;
41
42 15 $self->_build;
43
44 15 return $self;
45 }
46
47 34 ▶sub module {
Subroutine module: called CC=1 SCAR=0.0
Pod documented
48 34 my $self = shift;
49 34 return $self->{_MODULE_};
50 }
51
52 18 ▶sub document {
Subroutine document: called CC=1 SCAR=0.0
Pod documented
53 18 my $self = shift;
54 18 return $self->{_DOCUMENT_};
55 }
56
57 18 ▶sub modules {
Subroutine modules: called CC=2 SCAR=6.9
Pod documented
58 18 my $self = shift;
59 return exists $self->{_MODULES_}
60 18 ▶ ? @{ $self->{_MODULES_} }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_MODULES_'} ? : 17 1 18
61 : undef;
62 }
63
64 17 ▶sub called_imports{
Subroutine called_imports: called CC=2 SCAR=6.9
Pod documented
65 17 my $self = shift;
66 return exists $self->{_CALLED_IMPORTS_}
67 17 ▶ ? @{ $self->{_CALLED_IMPORTS_} }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_CALLED_IMPORTS_'} ? : 16 1 17
68 : undef;
69 }
70
71 2 ▶sub all_functions {
Subroutine all_functions: called CC=2 SCAR=6.9
Pod documented
72 2 my $self = shift;
73
74 return $self->module
75 2 ▶ ? @{Class::Inspector->functions( $self->module )}
Branch: 1 branch
BranchTrueFalseTotal
$self->module ? : 1 1 2
76 : undef;
77 }
78
79 2 ▶sub revert{
Subroutine revert: called CC=1 SCAR=0.0
Pod documented
80 2 my $self = shift;
81 2 my @functions = @_;
82
83 2 map { $self->{_PATCHES_}{$_} = undef } @functions;
84
85 2 return $self;
86 }
87
88 2 ▶sub report{
Subroutine report: called CC=2 SCAR=6.9
Pod documented
89 2 my $self = shift;
90
91 2 ▶ return $self->document
Branch: 1 branch
BranchTrueFalseTotal
$self->document ? : 1 1 2
92 ? $self->_build_report
93 : croak qq{ 'report' called on uninitialized object };
94 }
95
96 ### EXPORTS FOR SYNTACTIC SUGAR: ###########
97
98 10 ▶sub from(\@;%) {
Subroutine from: called CC=1 SCAR=0.0
Pod documented
99 10 return @_;
100 }
101
102 18 ▶sub function($) {
Subroutine function: called CC=1 SCAR=0.0
Pod documented
103 18 return shift;
104 }
105
106 5 ▶sub functions(\@$;%) {
Subroutine functions: called CC=1 SCAR=0.0
Pod documented
107 5 my $functions = shift;
108 5 my $code = shift;
109 5 my %args = @_;
110
111 5 my %map = map { $_ => $code } @{$functions};
112
113 5 return %map, %args;
114 }
115
116 27 ▶sub as(&) {
Subroutine as: called CC=1 SCAR=0.0
Pod documented
117 27 return shift;
118 }
119
120 10 ▶sub default_sub() {
Subroutine default_sub: called CC=1 SCAR=0.0
Pod documented
121 10 return '_DEFAULT_';
122 }
123
124 6 ▶sub preserved() {
Subroutine preserved: called CC=1 SCAR=0.0
Pod documented
125 6 return '_PRESERVED_';
126 }
127
128 ### PRIVATE METHODS: #######################
129
130 15 ▶sub _build {
Subroutine _build: called CC=1 SCAR=0.0
131 15 my $self = shift;
132
133 15 $self->_build_imports();
134 15 $self->_set_code_substitutions();
135 }
136
137 1 ▶sub _build_report {
Subroutine _build_report: called CC=3 SCAR=11.1
138 1 my $self = shift;
139 1 my $spacing = 20;
140 1 my $indent = 4;
141
142 # format a simplistic report...
143 1 ▶ my $report = "\nFunction-import usage statistics for ".($self->module||$self->document).":\n";
Condition33%1/3
$self->module || $self->document
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->module || $self->document
$self->module $self->document
Truth table33%1/3
A || B
A= $self->moduleB= $self->document
ABresult
1X1
011
000
144
145 1 for my $module ( $self->modules ){
146 2 $report .= " "x($indent)."$module\n";
147 map { $report .= " "x(2*$indent)."$_"." "x($spacing-length($_))."calls: ".
148 $self->{_CALLED_IMPORT_STATS_}{$module}{$_}{ _NUMBER_OF_CALLS_ }."\tlines: ".
149 2 (join ',', @{$self->{_CALLED_IMPORT_STATS_}{$module}{$_}{_LINE_NUMBERS_}}).".\n" }
150 2 sort keys %{$self->{_CALLED_IMPORT_STATS_}{$module}};
151 }
152
153 1 return $report;
154 }
155
156 15 ▶sub _build_imports {
Subroutine _build_imports: called CC=7 SCAR=19.5
157 15 my $self = shift;
158
159 15 my ($CallFinder,$Document) = $self->_create_call_finder;
160 15 my @found = $CallFinder->in( $Document );
161
162 15 for my $module ( $self->_get_modules ){
163 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 2610 for my $function ( @{$module.'::EXPORT'}, @{$module.'::EXPORT_OK'} ){
165 13716 for my $token ( @found ){
166 178308 ▶ if ( $token eq $function ){
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq $function) 30 178278 178308
167 30 $self->{_CALLED_IMPORT_STATS_}{$module}{$function}{_NUMBER_OF_CALLS_}++;
168 30 push @{$self->{_CALLED_IMPORT_STATS_}{$module}{$function}{_LINE_NUMBERS_}}, $token->line_number;
169 }
170 }
171 }
172 }
173
174 15 $self->{ _MODULES_ } = [ keys %{ $self->{_CALLED_IMPORT_STATS_} }];
175 15 for my $module ( $self->modules ){
176 30 push @{$self->{_CALLED_IMPORTS_}}, $_ for keys %{$self->{_CALLED_IMPORT_STATS_}{$module}};
177 }
178
179 15 return $self;
180 }
181
182 15 ▶sub _get_modules {
Subroutine _get_modules: called CC=2 SCAR=6.9
183 15 my $self = shift;
184
185 $self->{_MODULES_} eq '_ALL_'
186 2598 ? return map { my $module = $_; $module =~ s{\.pm}{}; $module }
187 2598 map { my $module = $_; $module =~ s{[\/\\]}{::}g; $module }
188 2616 grep { m{\.pm$} } keys %INC
189 15 ▶ : return @{ $self->{_MODULES_} };
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_MODULES_'} eq '_ALL_' ? : 9 6 15
190 }
191
192 15 ▶sub _create_call_finder {
Subroutine _create_call_finder: called CC=1 SCAR=0.0
193 15 my $self = shift;
194
195 15 my $Document = PPI::Document->new( $self->document );
196 my $Finder = PPI::Find->new(
197 990 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
198 990 ▶ $_[1]->isa( 'PPI::Element' ) | $_[1]->isa( 'PPI::Token' )
Branch: 2 branches
BranchTrueFalseTotal
$_[1]->isa('PPI::Element') | $_[1]->isa('PPI::Token') ? : 990 0 990
$_[0]->isa('PPI::Token::Word') ? : 195 795 990
199 ? $_[0]->isa( 'PPI::Token::Word' )
200 ? return 1
201 : return 0
202 : return;
203 15 });
204
205 15 return $Finder, $Document;
206 }
207
208 15 ▶sub _set_code_substitutions{
Subroutine _set_code_substitutions: called CC=9 SCAR=22.0
209 15 my $self = shift;
210
211 15 for my $function ( uniq $self->called_imports, keys %{$self->{_CODE_}} ){
212 46 ▶ next if ( (defined $self->{_CODE_}{$function} && ref $self->{_CODE_}{$function} ne 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_CODE_"}{$function} and ref $self->{"_CODE_"}{$function} ne "CODE" or $function eq "_DEFAULT_" 16 30 46
Condition100%6/6
defined $self->{"_CODE_"}{$function} and ref $self->{"_CODE_"}{$function} ne "CODE" or $function eq "_DEFAULT_"
l!l&&r!l&&!r
21430
defined $self->{"_CODE_"}{$function} and ref $self->{"_CODE_"}{$function} ne "CODE"
!ll&&!rl&&r
11332
MC/DC100%3/3
defined $self->{"_CODE_"}{$function} and ref $self->{"_CODE_"}{$function} ne "CODE" or $function eq "_DEFAULT_"
defined $self->{"_CODE_"}{$function} ref $self->{"_CODE_"}{$function} ne "CODE" $function eq "_DEFAULT_"
Truth table80%4/5
A and B or C
A= defined $self->{"_CODE_"}{$function}B= ref $self->{"_CODE_"}{$function} ne "CODE"C= $function eq "_DEFAULT_"
ABCresult
11X1
0X11
1011
0X00
1000
213 || $function eq '_DEFAULT_');
214
215 30 ▶ if ( ref $self->{_CODE_}{$function} eq 'CODE' || ref $self->{_CODE_}{_DEFAULT_} eq 'CODE' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"_CODE_"}{$function} eq "CODE" or ref $self->{"_CODE_"}{"_DEFAULT_"} eq "CODE") 28 2 30
Condition100%3/3
ref $self->{"_CODE_"}{$function} eq "CODE" or ref $self->{"_CODE_"}{"_DEFAULT_"} eq "CODE"
l!l&&r!l&&!r
1992
MC/DC100%2/2
ref $self->{"_CODE_"}{$function} eq "CODE" or ref $self->{"_CODE_"}{"_DEFAULT_"} eq "CODE"
ref $self->{"_CODE_"}{$function} eq "CODE" ref $self->{"_CODE_"}{"_DEFAULT_"} eq "CODE"
Truth table100%3/3
A or B
A= ref $self->{"_CODE_"}{$function} eq "CODE"B= ref $self->{"_CODE_"}{"_DEFAULT_"} eq "CODE"
ABresult
1X1
011
000
216 $self->{_PATCHES_}{$function} = patch_package(
217 $self->module, # module
218 $function, # function-name
219 ref $self->{_CODE_}{$function} eq 'CODE' # code
220 ? $self->{_CODE_}{$function}
221 : $self->{_CODE_}{_DEFAULT_}
222 28 ▶ ) or croak "couldn't install the code stub for '$function'";
Branch: 2 branches
BranchTrueFalseTotal
$self->{"_PATCHES_"}{$function} = patch_package($self->module, $function, ref $self->{"_CODE_"}{$function} eq "CODE" ? $self->{"_CODE_"}{$function} : $self->{"_CODE_"}{"_DEFAULT_"}) or croak("couldn't install the code stub for '${function}'") 28 0 28
ref $self->{'_CODE_'}{$function} eq 'CODE' ? : 19 9 28
223 }
224 }
225 }
226
227 4 1; # return true
228 __END__
↑ Summary Decouple/DB.pm »