MongoDB/Role/_DeprecationWarner.pm

filter: Statementstmt 30.0% 18 / 60 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 5 / 10 Podpod n/a totaltotal 22.3% 23 / 103 CC 15 SCAR 32.5
CC
15
Cov
20%
_get_trace
56.0
_get_alternative
30.0
_warn_deprecated_method
12.0
CRAP 25.9
« MongoDB/Op/_DropCollection.pm ↑ Summary MongoDB/Op/_DropIndexes.pm »
1 # Copyright 2016 - present MongoDB, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package MongoDB::Role::_DeprecationWarner;
18  
19 # MongoDB interface for issuing deprecation warnings
20  
21 59 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 59 our $VERSION = 'v2.2.2';
23  
24 59 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 59 my %CALL_SITES;
29  
30 0 ▶sub _warn_deprecated_method {
Subroutine _warn_deprecated_method: not called CC=3 SCAR=24.8
31 0 my ( $self, $old, $new ) = @_;
32  
33 0 ▶ return if $ENV{PERL_MONGO_NO_DEP_WARNINGS};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PERL_MONGO_NO_DEP_WARNINGS"} 0 0 0
34 0 my $trace = _get_trace();
35 0 ▶ return unless defined $trace; # already warned from this location
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trace 0 0 0
36  
37 0 my $msg = "# The '$old' method will be removed in a future major release.";
38 0 $msg .= _get_alternative($new);
39  
40 0 return __warn_deprecated($msg, $trace);
41 }
42  
43 # Expected to be called from BUILD
44 0 ▶sub _warn_deprecated_class {
Subroutine _warn_deprecated_class: not called CC=3 SCAR=24.8
45 0 my ( $self, $old, $new, $uplevel ) = @_;
46  
47 0 ▶ return if $ENV{PERL_MONGO_NO_DEP_WARNINGS};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PERL_MONGO_NO_DEP_WARNINGS"} 0 0 0
48  
49 0 my $trace = _get_trace(2);
50 0 ▶ return unless defined $trace; # already warned from this location
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trace 0 0 0
51  
52 0 my $msg = "# The '$old' class will be removed in a future major release.";
53 0 $msg .= _get_alternative($new);
54  
55 # fixup name of constructor
56 0 my $class = ref($self);
57 0 $trace =~ s/\S+ called at/${class}::new called at/;
58  
59 0 return __warn_deprecated($msg, $trace);
60 }
61  
62 0 ▶sub __warn_deprecated {
Subroutine __warn_deprecated: not called CC=1 SCAR=6.9
63 0 my ( $msg, $trace ) = @_;
64 0 chomp $msg;
65 0 warn("#\n# *** DEPRECATION WARNING ***\n#\n$msg\n$trace");
66 0 return;
67 }
68  
69 0 ▶sub _get_alternative {
Subroutine _get_alternative: not called CC=5 SCAR=34.0
70 0 my ($new) = @_;
71 # Arrayref is a list of replacement methods; string is just a message
72 0 ▶ if ( ref $new eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new eq "ARRAY") 0 0 0
73 0 ▶ if ( @$new == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$new == 1) { } 0 0 0
elsif (@$new > 1) { } 0 0 0
74 0 return "\n# Use '$new->[0]' instead.";
75 }
76 elsif (@$new > 1) {
77 0 my $last = pop @$new;
78 0 my $list = join(", ", map { "'$_'" } @$new);
79 0 return "\n# Use $list or '$last' instead.";
80 }
81 else {
82 0 return "";
83 }
84 }
85 0 ▶ return "\n # $new" // "";
Condition0%0/2
"\n # $new" // ""
l!l
00
MC/DC0%0/1
"\n # $new" // ""
"\n # $new"
Truth table0%0/2
A
A= "\n # $new"
Aresult
11
00
86 }
87  
88 0 ▶sub _get_trace {
Subroutine _get_trace: not called CC=7 SCAR=40.3
89 0 my ($uplevel) = @_;
90 0 ▶ $uplevel //= 0;
Condition0%0/2
$uplevel //= 0
l!l
00
MC/DC0%0/1
$uplevel //= 0
$uplevel
Truth table0%0/2
A
A= $uplevel
Aresult
11
00
91  
92 0 my ( $callsite_found, $pkg, $file, $line, $sub );
93 0 my ( $trace, $i ) = ( "", $uplevel + 1 );
94  
95 # Accumulate the stack trace. Start at uplevel + caller(2) to skip
96 # '__warn_deprecated' and its internal caller in the stack trace
97 0 ▶ while ( ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i) 0 0 0
98 # Use CORE::caller to get a real stack-trace, not one overridden by
99 # CORE::GLOBAL::caller
100 0 ( $pkg, $file, $line, $sub ) = CORE::caller($i);
101 0 ▶ last unless defined $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pkg 0 0 0
102  
103 # We want to check the deprecated function's caller and shortcut if
104 # we've already reported from that location. As we walk up the
105 # stack to build the trace, the first caller is usually the
106 # call-site, but we ignore Sub::Uplevel and use the first
107 # non-uplevel caller as the call-site.
108  
109 0 ▶ if ( !$callsite_found && $pkg ne 'Sub::Uplevel' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $callsite_found and $pkg ne "Sub::Uplevel") 0 0 0
Condition0%0/3
not $callsite_found and $pkg ne "Sub::Uplevel"
!ll&&!rl&&r
000
MC/DC0%0/2
not $callsite_found and $pkg ne "Sub::Uplevel"
not $callsite_found $pkg ne "Sub::Uplevel"
Truth table0%0/3
not(A) and B
A= not $callsite_foundB= $pkg ne "Sub::Uplevel"
ABresult
0X0
100
111
110 0 $callsite_found++;
111 0 ▶ return if $CALL_SITES{ $pkg, $line, $file }++;
Branch: 1 branch
BranchTrueFalseTotal
if $CALL_SITES{join $;, $pkg, $line, $file}++ 0 0 0
112 }
113  
114 0 $trace .= "# $sub called at $file line $line\n";
115 }
116  
117 0 return $trace;
118 }
119  
120  
121 59 1;
« MongoDB/Op/_DropCollection.pm ↑ Summary MongoDB/Op/_DropIndexes.pm »