blib/lib/Package/DeprecationManager.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 88.2% 30 / 34 Conditioncond 80.9% 17 / 21 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 91.9% 160 / 174 SCAR 32.2
CC
25
Cov
94%
deprecated
15.1
import
6.1
import
4.0
CRAP 25.1
↑ 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 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.16';
8  
9 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Params::Util qw( _HASH0 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Sub::Name qw( subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub import {
17 5 ▶ shift;
Subroutine import: called CC=4 SCAR=13.9
18 5 my %args = @_;
19  
20 croak
21 'You must provide a hash reference -deprecations parameter when importing Package::DeprecationManager'
22 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
23  
24 4 my %registry;
25  
26 4 my $caller = caller();
27  
28 4 my $orig_import = $caller->can('import');
29  
30 4 my $import = _build_import( \%registry, $orig_import );
31 my $warn
32 4 = _build_warn( \%registry, $args{-deprecations}, $args{-ignore} );
33  
34 # We need to remove this to prevent a 'subroutine redefined' warning.
35 4 ▶ if ($orig_import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_import) 4 0 4
36 4 Package::Stash->new($caller)->remove_symbol('&import');
37 }
38  
39 Sub::Install::install_sub(
40 {
41 4 code => subname( $caller . '::import', $import ),
42 into => $caller,
43 as => 'import',
44 }
45 );
46  
47 4 Sub::Install::install_sub(
48 {
49 code => subname( $caller . '::deprecated', $warn ),
50 into => $caller,
51 as => 'deprecated',
52 }
53 );
54  
55 4 return;
56 }
57  
58 sub _build_import {
59 4 ▶ my $registry = shift;
Subroutine _build_import: called CC=1 SCAR=0.0
60 4 my $orig_import = shift;
61  
62 return sub {
63 5 ▶ my $class = shift;
Subroutine import: called CC=6 SCAR=18.0
64  
65 5 my @args;
66  
67 my $api_version;
68 ## no critic (ControlStructures::ProhibitCStyleForLoops)
69 5 ▶ for ( my $i = 0; $i < @_; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 4 5 9
70 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
71 3 $api_version = $_[ ++$i ];
72 }
73 else {
74 1 push @args, $_[$i];
75 }
76 }
77 ## use critic
78  
79 5 my $caller = caller();
80 5 ▶ $registry->{$caller} = $api_version
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_version 3 2 5
81 if defined $api_version;
82  
83 5 ▶ if ($orig_import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_import) 5 0 5
84 5 @_ = ( $class, @args );
85 5 goto &{$orig_import};
86 }
87  
88 0 return;
89 4 };
90 }
91  
92 sub _build_warn {
93 4 ▶ my $registry = shift;
Subroutine _build_warn: called CC=3 SCAR=11.0
94 4 my $deprecated_at = shift;
95 4 my $ignore = shift;
96  
97 4 ▶ my %ignore = map { $_ => 1 } grep { !ref } @{ $ignore || [] };
Branch: 1 branch
BranchTrueFalseTotal
$ignore or [] 2 2 4
98 4 ▶ my @ignore_res = grep {ref} @{ $ignore || [] };
Branch: 1 branch
BranchTrueFalseTotal
$ignore or [] 2 2 4
99  
100 4 my %warned;
101  
102 return sub {
103 24 ▶ my %args = @_ < 2 ? ( message => shift ) : @_;
Subroutine deprecated: called CC=15 SCAR=27.1
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 ? : 23 1 24
104  
105 24 my ( $package, undef, undef, $sub ) = caller(1);
106  
107 24 my $skipped = 1;
108  
109 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
110 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
111 8 ▶ && ( $ignore{$package} || any { $package =~ $_ } @ignore_res )
Subroutine __ANON__: called CC=1 SCAR=0.0
112 ) {
113 18 $package = caller( $skipped++ );
114 }
115 }
116  
117 24 ▶ $package = 'unknown package' unless defined $package;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 0 24 24
118  
119 24 ▶ unless ( defined $args{feature} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"feature"}) 23 1 24
120 23 $args{feature} = $sub;
121 }
122  
123 24 my $compat_version = $registry->{$package};
124  
125 24 my $at = $deprecated_at->{ $args{feature} };
126  
127 return
128 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
129 && defined $deprecated_at
130 && $compat_version lt $at;
131  
132 19 my $msg;
133 19 ▶ if ( defined $args{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'message'}) { } 17 2 19
134 17 $msg = $args{message};
135 }
136 else {
137 2 $msg = "$args{feature} has been deprecated";
138 2 ▶ $msg .= " since version $at"
Branch: 1 branch
BranchTrueFalseTotal
if defined $at 2 0 2
139 if defined $at;
140 }
141  
142 19 ▶ return if $warned{$package}{ $args{feature} }{$msg};
Branch: 1 branch
BranchTrueFalseTotal
if $warned{$package}{$args{"feature"}}{$msg} 8 11 19
143  
144 11 $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 11 local $Carp::CarpLevel = $Carp::CarpLevel + $skipped;
149  
150 11 Carp::cluck($msg);
151 4 };
152 }
153  
154 2 1;
155  
156 # ABSTRACT: Manage deprecation warnings for your distribution
157  
158 __END__
↑ Summary