Policy/Community/OverloadOptions.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 95.0% 19 / 20 Conditioncond 81.8% 18 / 22 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 91.1% 103 / 113 CC 20 SCAR 26.3
CC
20
Cov
93%
violates
20.2
default_themes
2.0
BEGIN
1.0
CRAP 13.8
« Policy/Community/MultidimensionalArrayEmulation.pm ↑ Summary Policy/Community/StrictWarnings.pm »
1 package Perl::Critic::Policy::Community::OverloadOptions;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v1.0.4';
10  
11 1 ▶use constant DESC => 'Using overload.pm without a boolean overload or fallback';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant EXPL => 'When using overload.pm to define overloads for an object class, always define an overload on "bool" explicitly and set the fallback option. This prevents objects from autogenerating a potentially surprising boolean overload, and causes operators for which overloads can\'t be autogenerated to act on the object as they normally would.';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶sub supported_parameters { () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
15 8 ▶sub default_severity { $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
16 0 ▶sub default_themes { 'community' }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
17 6 ▶sub applies_to { 'PPI::Statement::Include' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
18  
19 13 ▶sub violates {
Subroutine violates: called CC=20 SCAR=30.1
Pod documented
20 13 my ($self, $elem) = @_;
21 13 ▶ return () unless ($elem->type // '') eq 'use' and ($elem->module // '') eq 'overload';
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->type // "") eq "use" and ($elem->module // "") eq "overload" 0 13 13
Condition42%3/7
($elem->type // "") eq "use" and ($elem->module // "") eq "overload"
!ll&&!rl&&r
0013
$elem->type // ""
l!l
130
$elem->module // ""
l!l
130
MC/DC0%0/2
($elem->type // "") eq "use" and ($elem->module // "") eq "overload"
$elem->type $elem->module
Truth table33%1/3
A and B
A= $elem->typeB= $elem->module
ABresult
0X0
100
111
22 13 my @args = $elem->arguments;
23 13 my ($has_bool, $has_fallback);
24 13 my @options;
25 13 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 90 13 103
26 90 my $arg = shift @args;
27 # use overload qw(...);
28 90 ▶ if ($arg->isa('PPI::Token::QuoteLike::Words')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($arg->isa('PPI::Token::QuoteLike::Words')) { } 3 87 90
elsif ($arg->isa('PPI::Token::Quote')) { } 11 76 87
elsif ($arg->isa('PPI::Token::Word') or $arg->isa('PPI::Token::Number')) { } 24 52 76
elsif ($arg->isa('PPI::Structure::List') or $arg->isa('PPI::Statement::Expression')) { } 11 41 52
29 3 push @options, $arg->literal;
30 # use overload 'foo', 1;
31 } elsif ($arg->isa('PPI::Token::Quote')) {
32 11 push @options, $arg->string;
33 # use overload foo => 1;
34 ▶ } elsif ($arg->isa('PPI::Token::Word') or $arg->isa('PPI::Token::Number')) {
Condition100%3/3
$arg->isa("PPI::Token::Word") or $arg->isa("PPI::Token::Number")
l!l&&r!l&&!r
22252
MC/DC100%2/2
$arg->isa("PPI::Token::Word") or $arg->isa("PPI::Token::Number")
$arg->isa("PPI::Token::Word") $arg->isa("PPI::Token::Number")
Truth table100%3/3
A or B
A= $arg->isa("PPI::Token::Word")B= $arg->isa("PPI::Token::Number")
ABresult
1X1
011
000
35 24 push @options, $arg->literal;
36 # unpack lists and expressions
37 ▶ } elsif ($arg->isa('PPI::Structure::List') or $arg->isa('PPI::Statement::Expression')) {
Condition100%3/3
$arg->isa("PPI::Structure::List") or $arg->isa("PPI::Statement::Expression")
l!l&&r!l&&!r
6541
MC/DC100%2/2
$arg->isa("PPI::Structure::List") or $arg->isa("PPI::Statement::Expression")
$arg->isa("PPI::Structure::List") $arg->isa("PPI::Statement::Expression")
Truth table100%3/3
A or B
A= $arg->isa("PPI::Structure::List")B= $arg->isa("PPI::Statement::Expression")
ABresult
1X1
011
000
38 11 unshift @args, $arg->schildren;
39 }
40 }
41 # use overload; or use overload ();
42 13 ▶ return () unless @options;
Branch: 1 branch
BranchTrueFalseTotal
unless @options 3 10 13
43 10 foreach my $i (0..$#options) {
44 39 my $item = $options[$i];
45 39 ▶ if ($item eq 'fallback' and defined $options[$i+1] and $options[$i+1] ne 'undef') {
Branch: 2 branches
BranchTrueFalseTotal
if ($item eq 'fallback' and defined $options[$i + 1] and $options[$i + 1] ne 'undef') { } 4 35 39
elsif ($item eq 'bool') { } 6 29 35
Condition100%6/6
$item eq "fallback" and defined $options[$i + 1] and $options[$i + 1] ne "undef"
!ll&&!rl&&r
3414
$item eq "fallback" and defined $options[$i + 1]
!ll&&!rl&&r
3315
MC/DC100%3/3
$item eq "fallback" and defined $options[$i + 1] and $options[$i + 1] ne "undef"
$item eq "fallback" defined $options[$i + 1] $options[$i + 1] ne "undef"
Truth table100%4/4
A and B and C
A= $item eq "fallback"B= defined $options[$i + 1]C= $options[$i + 1] ne "undef"
ABCresult
0XX0
10X0
1100
1111
46 4 $has_fallback = 1;
47 } elsif ($item eq 'bool') {
48 6 $has_bool = 1;
49 }
50 }
51 10 ▶ return $self->violation(DESC, EXPL, $elem) unless $has_bool and $has_fallback;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_bool and $has_fallback 8 2 10
Condition100%3/3
$has_bool and $has_fallback
!ll&&!rl&&r
442
MC/DC100%2/2
$has_bool and $has_fallback
$has_bool $has_fallback
Truth table100%3/3
A and B
A= $has_boolB= $has_fallback
ABresult
0X0
100
111
52 2 return ();
53 }
54  
55 1 1;
56  
57 =head1 NAME
58  
59 Perl::Critic::Policy::Community::OverloadOptions - Don't use overload without
60 specifying a bool overload and enabling fallback
61  
62 =head1 DESCRIPTION
63  
64 The L<overload> module allows an object class to specify behavior for an object
65 used in various operations. However, when activated it enables additional
66 behavior by default: it L<autogenerates|overload/"Magic Autogeneration">
67 overload behavior for operators that are not specified, and if it cannot
68 autogenerate an overload for an operator, using that operator on the object
69 will throw an exception.
70  
71 An autogenerated boolean overload can lead to surprising behavior where an
72 object is considered "false" because of another overloaded value. For example,
73 if a class overloads stringification to return the object's name, but the
74 object's name is C<0>, then the object will be considered false due to an
75 autogenerated overload using the boolean value of the string. This is rarely
76 desired behavior, and if needed, it can be set as an explicit boolean overload.
77  
78 Without setting the C<fallback> option, any operators that cannot be
79 autogenerated from defined overloads will result in an exception when used.
80 By setting C<fallback> to C<1>, the operator will instead fall back to standard
81 behavior as if no overload was defined, which is generally the expected
82 behavior when only overloading a few operations.
83  
84 use overload '""' => sub { $_[0]->name }; # not ok
85 use overload '""' => sub { $_[0]->name }, bool => sub { 1 }; # not ok
86 use overload '""' => sub { $_[0]->name }, fallback => 1; # not ok
87 use overload '""' => sub { $_[0]->name }, bool => sub { 1 }, fallback => 1; # ok
88  
89 =head1 AFFILIATION
90  
91 This policy is part of L<Perl::Critic::Community>.
92  
93 =head1 CONFIGURATION
94  
95 This policy is not configurable except for the standard options.
96  
97 =head1 AUTHOR
98  
99 Dan Book, C<dbook@cpan.org>
100  
101 =head1 COPYRIGHT AND LICENSE
102  
103 Copyright 2015, Dan Book.
104  
105 This library is free software; you may redistribute it and/or modify it under
106 the terms of the Artistic License version 2.0.
107  
108 =head1 SEE ALSO
109  
110 L<Perl::Critic>
« Policy/Community/MultidimensionalArrayEmulation.pm ↑ Summary Policy/Community/StrictWarnings.pm »