| 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
Condition42%3/7
($elem->type // "") eq "use" and ($elem->module // "") eq "overload"
$elem->type // ""
$elem->module // ""
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | my @args = $elem->arguments; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | 13 | my ($has_bool, $has_fallback); | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 13 | my @options; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 13 | ▶ | while (@args) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 90 | my $arg = shift @args; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | # use overload qw(...); | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 90 | ▶ | if ($arg->isa('PPI::Token::QuoteLike::Words')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 11 | unshift @args, $arg->schildren; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # use overload; or use overload (); | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 13 | ▶ | return () unless @options; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$item eq "fallback" and defined $options[$i + 1] and $options[$i + 1] ne "undef"
$item eq "fallback" and defined $options[$i + 1]
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$has_bool and $has_fallback
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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> | ||||||||||||||||||||||||||||||||||||||||||||||