blib/lib/Package/DeprecationManager.pm

filter: Statementstmt 97.0% 99 / 102 Branchbran 83.3% 35 / 42 Conditioncond 75.0% 18 / 24 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 88.3% 174 / 197 CC 30 SCAR 20.4
CC
30
Cov
90%
deprecated
15.1
__ANON__
6.3
import
6.3
CRAP 7.7
↑ 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.17';
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::Name qw( subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub import {
16 5 ▶ shift;
Subroutine import: called CC=4 SCAR=13.9
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 => subname( $caller . '::import', $import ),
41 into => $caller,
42 as => 'import',
43 }
44 );
45  
46 4 Sub::Install::install_sub(
47 {
48 code => subname( $caller . '::deprecated', $warn ),
49 into => $caller,
50 as => 'deprecated',
51 }
52 );
53  
54 4 return;
55 }
56  
57 sub _build_import {
58 4 ▶ my $registry = shift;
Subroutine _build_import: called CC=1 SCAR=0.1
59 4 my $orig_import = shift;
60  
61 return sub {
62 4 ▶ my $class = shift;
Subroutine import: called CC=6 SCAR=18.4
Subroutine __ANON__: called CC=6 SCAR=18.4
63  
64 4 my @args;
65  
66 my $api_version;
67 ## no critic (ControlStructures::ProhibitCStyleForLoops)
68 4 ▶ for ( my $i = 0; $i < @_; $i++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @_) 2 4 6
if ($i < @_) 2 1 3
69 2 ▶ if ( $_[$i] eq '-api_version' || $_[$i] eq '-compatible' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[$i] eq '-api_version' or $_[$i] eq '-compatible') { } 2 0 2
if ($_[$i] eq '-api_version' or $_[$i] eq '-compatible') { } 1 1 2
Condition50%3/6
$_[$i] eq "-api_version" or $_[$i] eq "-compatible"
l!l&&r!l&&!r
200
$_[$i] eq "-api_version" or $_[$i] eq "-compatible"
l!l&&r!l&&!r
101
MC/DC0%0/2
$_[$i] eq "-api_version" or $_[$i] eq "-compatible"
$_[$i] eq "-api_version" $_[$i] eq "-compatible"
MC/DC50%1/2
$_[$i] eq "-api_version" or $_[$i] eq "-compatible"
$_[$i] eq "-api_version" $_[$i] eq "-compatible"
Truth table33%1/3
A or B
A= $_[$i] eq "-api_version"B= $_[$i] eq "-compatible"
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $_[$i] eq "-api_version"B= $_[$i] eq "-compatible"
ABresult
1X1
011
000
70 2 $api_version = $_[ ++$i ];
71 }
72 else {
73 0 push @args, $_[$i];
74 }
75 }
76 ## use critic
77  
78 4 my $caller = caller();
79 4 ▶ $registry->{$caller} = $api_version
Branch: 2 branches
BranchTrueFalseTotal
if defined $api_version 2 2 4
if defined $api_version 1 0 1
80 if defined $api_version;
81  
82 4 ▶ if ($orig_import) {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig_import) 4 0 4
if ($orig_import) 1 0 1
83 4 @_ = ( $class, @args );
84 4 goto &{$orig_import};
85 }
86  
87 0 return;
88 4 };
89 }
90  
91 sub _build_warn {
92 4 ▶ my $registry = shift;
Subroutine _build_warn: called CC=3 SCAR=11.0
93 4 my $deprecated_at = shift;
94 4 my $ignore = shift;
95  
96 4 ▶ my %ignore = map { $_ => 1 } grep { !ref } @{ $ignore || [] };
Branch: 1 branch
BranchTrueFalseTotal
$ignore or [] 2 2 4
97 4 ▶ my @ignore_res = grep {ref} @{ $ignore || [] };
Branch: 1 branch
BranchTrueFalseTotal
$ignore or [] 2 2 4
98  
99 4 my %warned;
100  
101 return sub {
102 24 ▶ my %args = @_ < 2 ? ( message => shift ) : @_;
Subroutine deprecated: called CC=15 SCAR=27.1
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 ? : 23 1 24
103  
104 24 my ( $package, undef, undef, $sub ) = caller(1);
105  
106 24 my $skipped = 1;
107  
108 24 ▶ if ( @ignore_res || keys %ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ignore_res or keys %ignore) 7 17 24
Condition100%3/3
@ignore_res or keys %ignore
l!l&&r!l&&!r
2517
MC/DC100%2/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
109 7 ▶ while ( defined $package
Branch: 1 branch
BranchTrueFalseTotal
if (defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)) 18 7 25
Condition83%5/6
defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)
!ll&&!rl&&r
0196
$ignore{$package} or &any(sub { $package =~ /$_/; } , @ignore_res)
l!l&&r!l&&!r
1267
MC/DC33%1/3
defined $package and $ignore{$package} || &any(sub { $package =~ /$_/; } , @ignore_res)
defined $package $ignore{$package} &any(sub { $package =~ /$_/; } , @ignore_res)
Truth table50%2/4
A and B or C
A= defined $packageB= $ignore{$package}C= &any(sub { $package =~ /$_/; } , @ignore_res)
ABCresult
0XX0
1000
11X1
1011
110 8 ▶ && ( $ignore{$package} || any { $package =~ $_ } @ignore_res )
Subroutine __ANON__: called CC=1 SCAR=0.0
111 ) {
112 18 $package = caller( $skipped++ );
113 }
114 }
115  
116 24 ▶ $package = 'unknown package' unless defined $package;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 0 24 24
117  
118 24 ▶ unless ( defined $args{feature} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"feature"}) 23 1 24
119 23 $args{feature} = $sub;
120 }
121  
122 24 my $compat_version = $registry->{$package};
123  
124 24 my $at = $deprecated_at->{ $args{feature} };
125  
126 return
127 24 ▶ if defined $compat_version
Branch: 1 branch
BranchTrueFalseTotal
if defined $compat_version and defined $deprecated_at and $compat_version lt $at 5 19 24
Condition83%5/6
defined $compat_version and defined $deprecated_at and $compat_version lt $at
!ll&&!rl&&r
1725
defined $compat_version and defined $deprecated_at
!ll&&!rl&&r
1707
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
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
128 && defined $deprecated_at
129 && $compat_version lt $at;
130  
131 19 my $msg;
132 19 ▶ if ( defined $args{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'message'}) { } 17 2 19
133 17 $msg = $args{message};
134 }
135 else {
136 2 $msg = "$args{feature} has been deprecated";
137 2 ▶ $msg .= " since version $at"
Branch: 1 branch
BranchTrueFalseTotal
if defined $at 2 0 2
138 if defined $at;
139 }
140  
141 19 ▶ return if $warned{$package}{ $args{feature} }{$msg};
Branch: 1 branch
BranchTrueFalseTotal
if $warned{$package}{$args{"feature"}}{$msg} 8 11 19
142  
143 11 $warned{$package}{ $args{feature} }{$msg} = 1;
144  
145 # We skip at least two levels. One for this anon sub, and one for the
146 # sub calling it.
147 11 local $Carp::CarpLevel = $Carp::CarpLevel + $skipped;
148  
149 11 Carp::cluck($msg);
150 4 };
151 }
152  
153 2 1;
154  
155 # ABSTRACT: Manage deprecation warnings for your distribution
156  
157 __END__
↑ Summary