File Coverage

blib/lib/Perl/Critic/Mardem/Util.pm
Criterion Covered Total %
statement 43 46 93.4
branch 4 8 50.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 0 1 0.0
total 59 69 85.5


line stmt bran cond sub pod time code
1              
2             use utf8;
3 8     8   1559  
  8         16  
  8         34  
4             use 5.010;
5 8     8   260  
  8         23  
6             use strict;
7 8     8   45 use warnings;
  8         33  
  8         188  
8 8     8   40  
  8         13  
  8         325  
9             our $VERSION = '0.03';
10              
11             use Readonly;
12 8     8   906 use List::Util qw( first );
  8         6548  
  8         378  
13 8     8   47  
  8         15  
  8         649  
14             use Perl::Critic::Utils qw{ is_hash_key };
15 8     8   1117  
  8         219832  
  8         320  
16             use base 'Exporter';
17 8     8   1032  
  8         19  
  8         2838  
18             our @EXPORT_OK = qw( search_for_block_keyword );
19              
20             Readonly::Array my @BLOCK_SEARCH_KEYWORD => qw(
21             SUB
22             IF ELSIF UNLESS
23             WHILE UNTIL
24             DO
25             FOR FOREACH
26             EVAL
27             SORT MAP GREP
28             BEGIN UNITCHECK CHECK INIT END
29             PACKAGE );
30              
31             Readonly::Scalar my $MAX_KEYWORD_LOOKUP_DEPTH => 10;
32              
33             {
34             my ( $keyword ) = @_;
35              
36 67     67   154 $keyword = uc $keyword;
37              
38 67         201 my $found = first { $_ eq $keyword } @BLOCK_SEARCH_KEYWORD;
39              
40 67     760   451 return $found;
  760         3947  
41             }
42 67         576  
43             {
44             my ( $elem ) = @_;
45              
46             if ( !ref $elem ) {
47 49     49 0 117 last;
48             }
49 49 50       150  
50 0         0 my $word_search = $elem;
51             my $block_keyword = q{};
52              
53 49         89 my $i = 1;
54 49         94  
55             while ( !$block_keyword ) {
56 49         80 if ( $i >= $MAX_KEYWORD_LOOKUP_DEPTH ) {
57             last; # recurse abort!
58 49         132 }
59 67 50       167  
60 0         0 my $sprevious = $word_search->sprevious_sibling;
61              
62             if ( !$sprevious || $sprevious == $word_search ) {
63 67         309 last;
64             }
65 67 50 33     2084  
66 0         0 if ( !is_hash_key( $sprevious ) ) {
67             $word_search = $sprevious;
68              
69 67 50       666 my $content_search = $word_search->content;
70 67         4465  
71             $block_keyword = _keyword_in_searchlist( $content_search );
72 67         232 }
73              
74 67         875 $i++;
75             }
76              
77 67         157 return $block_keyword;
78             }
79              
80 49         117 1;
81              
82              
83             #-----------------------------------------------------------------------------
84              
85             =pod
86              
87             =encoding utf8
88              
89             =head1 NAME
90              
91             Perl::Critic::Mardem::Util - Internal Util module!
92              
93             =head1 DESCRIPTION
94              
95             Util module with internal subroutines for the
96             L<Perl::Critic::Policy::Mardem>> modules.
97              
98             =head1 AFFILIATION
99              
100             This policy is part of L<Perl::Critic::Mardem>.
101              
102             =head1 AUTHOR
103              
104             Markus Demml, mardem@cpan.com
105              
106             =head1 LICENSE AND COPYRIGHT
107              
108             Copyright (c) 2022, Markus Demml
109              
110             This library is free software; you can redistribute it and/or modify it
111             under the same terms as the Perl 5 programming language system itself.
112             The full text of this license can be found in the LICENSE file included
113             with this module.
114              
115             =cut