File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUniversalIsa.pm
Criterion Covered Total %
statement 24 26 92.3
branch 1 4 25.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 40 46 86.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa;
2              
3 40     40   26499 use 5.010001;
  40         222  
4 40     40   358 use strict;
  40         150  
  40         866  
5 40     40   268 use warnings;
  40         130  
  40         1258  
6 40     40   306 use Readonly;
  40         141  
  40         2341  
7              
8 40     40   418 use Perl::Critic::Utils qw{ :severities :classification hashify };
  40         164  
  40         2354  
9 40     40   14399 use parent 'Perl::Critic::Policy';
  40         143  
  40         280  
10              
11             our $VERSION = '1.150';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{UNIVERSAL::isa should not be used as a function};
16             Readonly::Scalar my $EXPL => q{Use eval{$obj->isa($pkg)} instead}; ## no critic (RequireInterp);
17              
18             #-----------------------------------------------------------------------------
19              
20 89     89 0 1632 sub supported_parameters { return () }
21 74     74 1 307 sub default_severity { return $SEVERITY_MEDIUM }
22 74     74 1 331 sub default_themes { return qw( core maintenance certrule ) }
23 30     30 1 83 sub applies_to { return 'PPI::Token::Word' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 329     329 1 546 my ( $self, $elem, undef ) = @_;
29              
30 329         415 state $isa_isa = { hashify( qw( isa UNIVERSAL::isa ) ) };
31 329 50       523 return if !$isa_isa->{$elem};
32 0 0         return if ! is_function_call($elem); # this also permits 'use UNIVERSAL::isa;'
33              
34 0           return $self->violation( $DESC, $EXPL, $elem );
35             }
36              
37              
38             1;
39              
40             __END__
41              
42             #-----------------------------------------------------------------------------
43              
44             =pod
45              
46             =head1 NAME
47              
48             Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa - Write C<< eval { $foo->isa($pkg) } >> instead of C<UNIVERSAL::isa($foo, $pkg)>.
49              
50              
51             =head1 AFFILIATION
52              
53             This Policy is part of the core L<Perl::Critic|Perl::Critic>
54             distribution.
55              
56              
57             =head1 DESCRIPTION
58              
59             print UNIVERSAL::isa($obj, 'Foo::Bar') ? 'yes' : 'no'; #not ok
60             print eval { $obj->isa('Foo::Bar') } ? 'yes' : 'no'; #ok
61              
62             As of Perl 5.9.3, the use of C<UNIVERSAL::isa> as a function has been
63             deprecated and the method form is preferred instead. Formerly, the
64             functional form was recommended because it gave valid results even
65             when the object was C<undef> or an unblessed scalar. However, the
66             functional form makes it impossible for packages to override C<isa()>,
67             a technique which is crucial for implementing mock objects and some
68             facades.
69              
70             Another alternative to UNIVERSAL::isa is the C<_INSTANCE> method of
71             Param::Util, which is faster.
72              
73             See the CPAN module L<UNIVERSAL::isa|UNIVERSAL::isa> for an incendiary
74             discussion of this topic.
75              
76              
77             =head1 CONFIGURATION
78              
79             This Policy is not configurable except for the standard options.
80              
81              
82             =head1 SEE ALSO
83              
84             L<Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan|Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan>
85              
86             =head1 AUTHOR
87              
88             Chris Dolan <cdolan@cpan.org>
89              
90             =head1 COPYRIGHT
91              
92             Copyright (c) 2006-2023 Chris Dolan.
93              
94             This program is free software; you can redistribute it and/or modify
95             it under the same terms as Perl itself. The full text of this license
96             can be found in the LICENSE file included with this module.
97              
98             =cut
99              
100             # Local Variables:
101             # mode: cperl
102             # cperl-indent-level: 4
103             # fill-column: 78
104             # indent-tabs-mode: nil
105             # c-indentation-style: bsd
106             # End:
107             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :