File Coverage

blib/lib/Perl/Critic/Policy/TryTiny/RequireCatch.pm
Criterion Covered Total %
statement 38 40 95.0
branch 9 12 75.0
condition 2 6 33.3
subroutine 12 13 92.3
pod 5 6 83.3
total 66 77 85.7


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TryTiny::RequireCatch;
2             $Perl::Critic::Policy::TryTiny::RequireCatch::VERSION = '0.002';
3 1     1   811 use strict;
  1         2  
  1         39  
4 1     1   5 use warnings;
  1         2  
  1         52  
5 1     1   7 use utf8;
  1         2  
  1         9  
6              
7             # ABSTRACT: Always include a "catch" block when using "try"
8              
9 1     1   34 use Readonly;
  1         1  
  1         59  
10 1     1   4 use Perl::Critic::Utils qw( :severities :classification :ppi );
  1         1  
  1         52  
11              
12 1     1   359 use base 'Perl::Critic::Policy';
  1         2  
  1         391  
13              
14             Readonly::Scalar my $DESC => "Try::Tiny \"try\" invoked without a corresponding \"catch\" block";
15             Readonly::Scalar my $EXPL => "Try::Tiny's \"try\" without a \"catch\" block will swallow exceptions. Use an empty block if this is intended.";
16              
17             sub supported_parameters {
18 5     5 0 18793 return ();
19             }
20              
21             sub default_severity {
22 2     2 1 22 return $SEVERITY_HIGH;
23             }
24              
25             sub default_themes {
26 0     0 1 0 return qw(bugs);
27             }
28              
29             sub prepare_to_scan_document {
30 5     5 1 33211 my $self = shift;
31 5         8 my $document = shift;
32              
33             return $document->find_any(sub {
34 33     33   284 my $element = $_[1];
35 33 100       133 return 0 if ! $element->isa('PPI::Statement::Include');
36 4         14 my @children = grep { $_->significant } $element->children;
  16         49  
37 4 50 33     51 if ($children[1] && $children[1]->isa('PPI::Token::Word') && $children[1] eq 'Try::Tiny') {
      33        
38 4         75 return 1;
39             }
40 0         0 return 0;
41 5         42 });
42             }
43              
44             sub applies_to {
45 4     4 1 124 return 'PPI::Token::Word';
46             }
47              
48             sub violates {
49 20     20 1 432 my ($self, $elem, undef) = @_;
50              
51 20 100       35 return if $elem->content ne 'try';
52 4 50       24 return if ! is_function_call($elem);
53              
54 4 50       852 my $try_block = $elem->snext_sibling() or return;
55 4         54 my $sib = $try_block->snext_sibling();
56 4 100       58 if ($sib->content ne 'catch') {
57 2         19 return $self->violation($DESC, $EXPL, $elem);
58             }
59 2         13 return;
60             }
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =encoding UTF-8
69              
70             =head1 NAME
71              
72             Perl::Critic::Policy::TryTiny::RequireCatch - Always include a "catch" block when using "try"
73              
74             =head1 VERSION
75              
76             version 0.002
77              
78             =head1 DESCRIPTION
79              
80             Programmers from other programming languages may assume that the following code
81             does not swallow exceptions:
82              
83             use Try::Tiny;
84             try {
85             # ...
86             }
87             finally {
88             # ...
89             };
90              
91             However, Try::Tiny's implementation always swallows exceptions unless a catch
92             block has been specified.
93              
94             The programmer should always include a catch block. The block may be empty, to
95             indicate that exceptions are deliberately ignored.
96              
97             use Try::Tiny;
98             try {
99             # ...
100             }
101             catch {
102             # ...
103             }
104             finally {
105             # ...
106             };
107              
108             =head1 CONFIGURATION
109              
110             This Policy is not configurable except for the standard options.
111              
112             =head1 KNOWN BUGS
113              
114             This policy assumes that L<Try::Tiny> is being used, and it doesn't run if it
115             can't find it being imported.
116              
117             =head1 AUTHOR
118              
119             David D Lowe <flimm@cpan.org>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2015 by Lokku <cpan@lokku.com>.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut