File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUniversalCan.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::ProhibitUniversalCan;
2              
3 40     40   26743 use 5.010001;
  40         191  
4 40     40   259 use strict;
  40         123  
  40         885  
5 40     40   242 use warnings;
  40         124  
  40         1025  
6 40     40   269 use Readonly;
  40         141  
  40         2373  
7              
8 40     40   309 use Perl::Critic::Utils qw{ :severities :classification };
  40         152  
  40         2165  
9 40     40   14078 use parent 'Perl::Critic::Policy';
  40         117  
  40         276  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{UNIVERSAL::can should not be used as a function};
16             Readonly::Scalar my $EXPL => q{Use eval{$obj->can($pkg)} instead}; ## no critic (RequireInterp);
17              
18             #-----------------------------------------------------------------------------
19              
20 91     91 0 1677 sub supported_parameters { return () }
21 76     76 1 371 sub default_severity { return $SEVERITY_MEDIUM }
22 74     74 1 406 sub default_themes { return qw( core maintenance certrule ) }
23 32     32 1 113 sub applies_to { return 'PPI::Token::Word' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 336     336 1 688 my ( $self, $elem, undef ) = @_;
29 336 100 100     773 return if !($elem eq 'can' || $elem eq 'UNIVERSAL::can');
30 5 100       110 return if ! is_function_call($elem); # this also permits 'use UNIVERSAL::can;'
31              
32 2         16 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::ProhibitUniversalCan - Write C<< eval { $foo->can($name) } >> instead of C<UNIVERSAL::can($foo, $name)>.
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::can($obj, 'Foo::Bar') ? 'yes' : 'no'; #not ok
58             print eval { $obj->can('Foo::Bar') } ? 'yes' : 'no'; #ok
59              
60             As of Perl 5.9.3, the use of UNIVERSAL::can 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<can()>,
65             a technique which is crucial for implementing mock objects and some
66             facades.
67              
68             See L<UNIVERSAL::can|UNIVERSAL::can> for a more thorough discussion of
69             this topic.
70              
71              
72             =head1 CONFIGURATION
73              
74             This Policy is not configurable except for the standard options.
75              
76              
77             =head1 SEE ALSO
78              
79             L<Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa|Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa>
80              
81              
82             =head1 AUTHOR
83              
84             Chris Dolan <cdolan@cpan.org>
85              
86              
87             =head1 COPYRIGHT
88              
89             Copyright (c) 2006-2011 Chris Dolan.
90              
91             This program is free software; you can redistribute it and/or modify
92             it under the same terms as Perl itself. The full text of this license
93             can be found in the LICENSE file included with this module.
94              
95             =cut
96              
97             # Local Variables:
98             # mode: cperl
99             # cperl-indent-level: 4
100             # fill-column: 78
101             # indent-tabs-mode: nil
102             # c-indentation-style: bsd
103             # End:
104             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :