File Coverage

blib/lib/Perl/Critic/Policy/TryTiny/RequireCatch.pm
Criterion Covered Total %
statement 38 40 95.0
branch 9 12 75.0
condition 5 9 55.5
subroutine 12 13 92.3
pod 5 6 83.3
total 69 80 86.2


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TryTiny::RequireCatch;
2             $Perl::Critic::Policy::TryTiny::RequireCatch::VERSION = '0.003';
3 1     1   711 use strict;
  1         9  
  1         26  
4 1     1   4 use warnings;
  1         2  
  1         24  
5 1     1   4 use utf8;
  1         2  
  1         6  
6              
7             # ABSTRACT: Always include a "catch" block when using "try"
8              
9 1     1   18 use Readonly;
  1         2  
  1         47  
10 1     1   5 use Perl::Critic::Utils qw( :severities :classification :ppi );
  1         2  
  1         67  
11              
12 1     1   363 use base 'Perl::Critic::Policy';
  1         3  
  1         315  
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 6     6 0 31511 return ();
19             }
20              
21             sub default_severity {
22 3     3 1 59 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 6     6 1 70270 my $self = shift;
31 6         16 my $document = shift;
32              
33             return $document->find_any(sub {
34 36     36   505 my $element = $_[1];
35 36 100       146 return 0 if ! $element->isa('PPI::Statement::Include');
36 5         22 my @children = grep { $_->significant } $element->children;
  20         83  
37 5 50 33     63 if ($children[1] && $children[1]->isa('PPI::Token::Word') && $children[1] eq 'Try::Tiny') {
      33        
38 5         117 return 1;
39             }
40 0         0 return 0;
41 6         58 });
42             }
43              
44             sub applies_to {
45 5     5 1 201 return 'PPI::Token::Word';
46             }
47              
48             sub violates {
49 25     25 1 689 my ($self, $elem, undef) = @_;
50              
51 25 100       65 return if $elem->content ne 'try';
52 5 50       40 return if ! is_function_call($elem);
53              
54 5 50       2011 my $try_block = $elem->snext_sibling() or return;
55 5         153 my $sib = $try_block->snext_sibling();
56 5 100 100     204 if (!$sib || $sib->content ne 'catch') {
57 3         26 return $self->violation($DESC, $EXPL, $elem);
58             }
59 2         21 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.003
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             This policy assumes that L<Try::Tiny> is being used, and it doesn't run if it
109             can't find it being imported.
110              
111             =head1 CONFIGURATION
112              
113             This Policy is not configurable except for the standard options.
114              
115             =head1 AUTHOR
116              
117             David D Lowe <flimm@cpan.org>
118              
119             =head1 COPYRIGHT AND LICENSE
120              
121             This software is copyright (c) 2017 by Lokku <cpan@lokku.com>.
122              
123             This is free software; you can redistribute it and/or modify it under
124             the same terms as the Perl 5 programming language system itself.
125              
126             =cut