File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUniversalIsa.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 47 48 97.9


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