blib/lib/Package/DeprecationManager.pm

filter: Statementstmt 89.0% 98 / 110 Branchbran 64.5% 31 / 48 Conditioncond 60.0% 24 / 40 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 75.7% 178 / 235 CC 39 SCAR 27.8
CC
39
Cov
77%
__ANON__
26.1
deprecated
26.1
__ANON__
6.1
CRAP 16.2
↑ Summary
1 package Package::DeprecationManager;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.18';
7  
8 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Params::Util qw( _HASH0 );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Sub::Util qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
16 5 shift;
17 5 my %args = @_;
18  
19 croak
20 'You must provide a hash reference -deprecations parameter when importing Package::DeprecationManager'
21 5 ▶ unless $args{-deprecations} && _HASH0( $args{-deprecations} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"-deprecations"} and &_HASH0($args{"-deprecations"}) 1 4 5
Condition66%2/3
$args{"-deprecations"} and &_HASH0($args{"-deprecations"})
!ll&&!rl&&r
104
MC/DC50%1/2
$args{"-deprecations"} and &_HASH0($args{"-deprecations"})
$args{"-deprecations"} &_HASH0($args{"-deprecations"})
Truth table66%2/3
A and B
A= $args{"-deprecations"}B= &_HASH0($args{"-deprecations"})
ABresult
0X0
100
111
22  
23 4 my %registry;
24  
25 4 my $caller = caller();
26  
27 4 my $orig_import = $caller->can('import');
28  
29 4 my $import = _build_import( \%registry, $orig_import );
30 my $warn
31 4 = _build_warn( \%registry, $args{-deprecations}, $args{-ignore} );
32  
33 # We need to remove this to prevent a 'subroutine redefined' warning.
34 4 ▶ if ($orig_import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_import) 4 0 4
35 4 Package::Stash->new($caller)->remove_symbol('&import');
36 }
37  
38 Sub::Install::install_sub(
39 {
40 4 code => set_subname( $caller . '::import', $import ),
41 into => $caller,
42 as => 'import',
43 }
44 );
45  
46 4 Sub::Install::install_sub(
47 {
48 code => set_subname( $caller . '::deprecated', $warn ),
49 into => $caller,
50 as => 'deprecated',
51 }
52 );
53  
54 4 return;
55 }
56  
57 4 ▶sub _build_import {
Subroutine _build_import: called CC=1 SCAR=0.0
58 4 my $registry = shift;
59 4 my $orig_import = shift;
60  
61 5 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
62 5 my $class = shift;
63  
64 5 my @args;
65  
66 my $api_version;
67 ## no critic (ControlStructures::ProhibitCStyleForLoops)
68 5 ▶ for ( my $i = 0; $i < @_; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 4 5 9
69 4 ▶ if ( $_[$i] eq '-api_version' || $_[$i] eq '-compatible' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq '-api_version' or $_[$i] eq '-compatible') { } 3 1 4
Condition66%2/3
$_[$i] eq "-api_version" or $_[$i] eq "-compatible"
l!l&&r!l&&!r
301
MC/DC50%1/2
$_[$i] eq "-api_version" or $_[$i] eq "-compatible"
$_[$i] eq "-api_version" $_[$i] eq "-compatible"
Truth table66%2/3
A or B
A= $_[$i] eq "-api_version"B= $_[$i] eq "-compatible"
ABresult
1X1
011
000
70 3 $api_version = $_[ ++$i ];
71 }
72 else {
73 1 push @args, $_[$i];
74 }
75 }
76 ## use critic
77  
78 5 my $caller = caller();
79 5 ▶ $registry->{$caller} = $api_version
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_version 3 2 5
80 if defined $api_version;
81  
82 5 ▶ if ($orig_import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_import) 5 0 5
83 5 @_ = ( $class, @args );
84 5 goto &{$orig_import};
85 }
86  
87 0 return;
88 4 };
89 }
90  
91 4 ▶sub _build_warn {
Subroutine _build_warn: called CC=3 SCAR=11.9
92 4 my $registry = shift;
93 4 my $deprecated_at = shift;
94 4 my $ignore = shift;
95  
96 4 ▶ my %ignore = map { $_ => 1 } grep { !ref } @{ $ignore || [] };
Condition100%2/2
$ignore || []
l!l
22
MC/DC100%1/1
$ignore || []
$ignore
Truth table100%2/2
A
A= $ignore
Aresult
11
00
97 4 ▶ my @ignore_res = grep {ref} @{ $ignore || [] };
Condition100%2/2
$ignore || []
l!l
22
MC/DC100%1/1
$ignore || []
$ignore
Truth table100%2/2
A
A= $ignore
Aresult
11
00
98  
99 4 my %warned;
100  
101 23 ▶ return sub {
Subroutine deprecated: called CC=15 SCAR=32.6
Subroutine __ANON__: called CC=15 SCAR=32.6
102 23 ▶ my %args = @_ < 2 ? ( message => shift ) : @_;
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 ? : 22 1 23
@_ < 2 ? : 1 0 1
103  
104 23 my ( $package, undef, undef, $sub ) = caller(1);
105  
106 23 my $skipped = 1;
107  
108 23 ▶ if ( @ignore_res || keys %ignore ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ignore_res or keys %ignore) 7 16 23
if (@ignore_res or keys %ignore) 0 1 1
Condition66%4/6
@ignore_res or keys %ignore
l!l&&r!l&&!r
2516
@ignore_res or keys %ignore
l!l&&r!l&&!r
001
MC/DC100%2/2
@ignore_res or keys %ignore
@ignore_res keys %ignore
MC/DC0%0/2
@ignore_res or keys %ignore
@ignore_res keys %ignore
Truth table100%3/3
A or B
A= @ignore_resB= keys %ignore
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= @ignore_resB= keys %ignore
ABresult
1X1
011
000
109 0 ▶ while (
Branch: 2 branches
BranchTrueFalseTotal
if (defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)) 18 7 25
if (defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)) 0 0 0
Condition41%5/12
defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)
!ll&&!rl&&r
0718
$ignore{$package} or &any(sub { $package =~ /$_/; } , @ignore_res)
l!l&&r!l&&!r
1267
defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)
!ll&&!rl&&r
000
$ignore{$package} or &any(sub { $package =~ /$_/; } , @ignore_res)
l!l&&r!l&&!r
000
MC/DC66%2/3
defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)
defined $package $ignore{$package} &any(sub { $package =~ /$_/; } , @ignore_res)
MC/DC0%0/3
defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)
defined $package $ignore{$package} &any(sub { $package =~ /$_/; } , @ignore_res)
Truth table75%3/4
A and B or C
A= defined $packageB= $ignore{$package}C= &any(sub { $package =~ /$_/; } , @ignore_res)
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= defined $packageB= $ignore{$package}C= &any(sub { $package =~ /$_/; } , @ignore_res)
ABCresult
0XX0
1000
11X1
1011
110 defined $package
111 8 ▶ && ( $ignore{$package} || any { $package =~ $_ } @ignore_res )
Subroutine __ANON__: called CC=1 SCAR=0.0
112 ) {
113 0 $package = caller( $skipped++ );
114 }
115 }
116  
117 23 ▶ $package = 'unknown package' unless defined $package;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $package 0 23 23
unless defined $package 0 1 1
118  
119 23 ▶ unless ( defined $args{feature} ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $args{"feature"}) 22 1 23
unless (defined $args{"feature"}) 1 0 1
120 22 $args{feature} = $sub;
121 }
122  
123 23 my $compat_version = $registry->{$package};
124  
125 23 my $at = $deprecated_at->{ $args{feature} };
126  
127 return
128 23 ▶ if defined $compat_version
Branch: 2 branches
BranchTrueFalseTotal
if defined $compat_version and defined $deprecated_at and $compat_version lt $at 4 19 23
if defined $compat_version and defined $deprecated_at and $compat_version lt $at 1 0 1
Condition58%7/12
defined $compat_version and defined $deprecated_at and $compat_version lt $at
!ll&&!rl&&r
1724
defined $compat_version and defined $deprecated_at
!ll&&!rl&&r
1706
defined $compat_version and defined $deprecated_at and $compat_version lt $at
!ll&&!rl&&r
001
defined $compat_version and defined $deprecated_at
!ll&&!rl&&r
001
MC/DC66%2/3
defined $compat_version and defined $deprecated_at and $compat_version lt $at
defined $compat_version defined $deprecated_at $compat_version lt $at
MC/DC0%0/3
defined $compat_version and defined $deprecated_at and $compat_version lt $at
defined $compat_version defined $deprecated_at $compat_version lt $at
Truth table75%3/4
A and B and C
A= defined $compat_versionB= defined $deprecated_atC= $compat_version lt $at
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and B and C
A= defined $compat_versionB= defined $deprecated_atC= $compat_version lt $at
ABCresult
0XX0
10X0
1100
1111
129 && defined $deprecated_at
130 && $compat_version lt $at;
131  
132 0 my $msg;
133 0 ▶ if ( defined $args{message} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'message'}) { } 17 2 19
if (defined $args{'message'}) { } 0 0 0
134 0 $msg = $args{message};
135 }
136 else {
137 0 $msg = "$args{feature} has been deprecated";
138 0 ▶ $msg .= " since version $at"
Branch: 2 branches
BranchTrueFalseTotal
if defined $at 2 0 2
if defined $at 0 0 0
139 if defined $at;
140 }
141  
142 0 ▶ return if $warned{$package}{ $args{feature} }{$msg};
Branch: 2 branches
BranchTrueFalseTotal
if $warned{$package}{$args{"feature"}}{$msg} 8 11 19
if $warned{$package}{$args{"feature"}}{$msg} 0 0 0
143  
144 0 $warned{$package}{ $args{feature} }{$msg} = 1;
145  
146 # We skip at least two levels. One for this anon sub, and one for the
147 # sub calling it.
148 0 local $Carp::CarpLevel = $Carp::CarpLevel + $skipped;
149  
150 0 Carp::cluck($msg);
151 4 };
152 }
153  
154 2 1;
155  
156 # ABSTRACT: Manage deprecation warnings for your distribution
157  
158 __END__
↑ Summary