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             package Perl::Critic::Mardem::Util;
2              
3 10     10   2825 use utf8;
  10         52  
  10         97  
4              
5 10     10   537 use 5.010;
  10         37  
6              
7 10     10   53 use strict;
  10         20  
  10         272  
8 10     10   50 use warnings;
  10         17  
  10         803  
9              
10             our $VERSION = '0.06';
11              
12 10     10   1812 use Readonly;
  10         12355  
  10         723  
13 10     10   64 use List::Util qw( first );
  10         16  
  10         966  
14              
15 10     10   2541 use Perl::Critic::Utils qw{ is_hash_key };
  10         327788  
  10         592  
16              
17 10     10   1982 use base 'Exporter';
  10         28  
  10         5266  
18              
19             our @EXPORT_OK = qw( search_for_block_keyword );
20              
21             Readonly::Array my @BLOCK_SEARCH_KEYWORD => qw(
22             SUB
23             IF ELSIF UNLESS
24             WHILE UNTIL
25             DO
26             FOR FOREACH
27             EVAL
28             SORT MAP GREP
29             BEGIN UNITCHECK CHECK INIT END
30             PACKAGE );
31              
32             Readonly::Scalar my $MAX_KEYWORD_LOOKUP_DEPTH => 10;
33              
34             sub _keyword_in_searchlist
35             {
36 67     67   176 my ( $keyword ) = @_;
37              
38 67         198 $keyword = uc $keyword;
39              
40 67     760   570 my $found = first { $_ eq $keyword } @BLOCK_SEARCH_KEYWORD;
  760         5246  
41              
42 67         849 return $found;
43             }
44              
45             sub search_for_block_keyword
46             {
47 49     49 0 166 my ( $elem ) = @_;
48              
49 49 50       191 if ( !ref $elem ) {
50 0         0 last;
51             }
52              
53 49         163 my $word_search = $elem;
54 49         121 my $block_keyword = q{};
55              
56 49         112 my $i = 1;
57              
58 49         173 while ( !$block_keyword ) {
59 67 50       231 if ( $i >= $MAX_KEYWORD_LOOKUP_DEPTH ) {
60 0         0 last; # recurse abort!
61             }
62              
63 67         269 my $sprevious = $word_search->sprevious_sibling;
64              
65 67 50 33     2530 if ( !$sprevious || $sprevious == $word_search ) {
66 0         0 last;
67             }
68              
69 67 50       711 if ( !is_hash_key( $sprevious ) ) {
70 67         4168 $word_search = $sprevious;
71              
72 67         258 my $content_search = $word_search->content;
73              
74 67         1029 $block_keyword = _keyword_in_searchlist( $content_search );
75             }
76              
77 67         232 $i++;
78             }
79              
80 49         163 return $block_keyword;
81             }
82              
83             1;
84              
85             __END__
86              
87             #-----------------------------------------------------------------------------
88              
89             =pod
90              
91             =encoding utf8
92              
93             =head1 NAME
94              
95             Perl::Critic::Mardem::Util - Internal Util module!
96              
97             =head1 DESCRIPTION
98              
99             Util module with internal subroutines for the
100             L<Perl::Critic::Policy::Mardem> modules.
101              
102             =head1 AFFILIATION
103              
104             This policy is part of L<Perl::Critic::Mardem>.
105              
106             =head1 AUTHOR
107              
108             Markus Demml, mardem@cpan.com
109              
110             =head1 LICENSE AND COPYRIGHT
111              
112             Copyright (c) 2024, Markus Demml
113              
114             This library is free software; you can redistribute it and/or modify it
115             under the same terms as the Perl 5 programming language system itself.
116             The full text of this license can be found in the LICENSE file included
117             with this module.
118              
119             =cut