| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  |  | 
| 2 |  |  |  |  |  |  | use utf8; | 
| 3 | 8 |  |  | 8 |  | 4727 |  | 
|  | 8 |  |  |  |  | 19 |  | 
|  | 8 |  |  |  |  | 39 |  | 
| 4 |  |  |  |  |  |  | use 5.010; | 
| 5 | 8 |  |  | 8 |  | 268 |  | 
|  | 8 |  |  |  |  | 24 |  | 
| 6 |  |  |  |  |  |  | use strict; | 
| 7 | 8 |  |  | 8 |  | 33 | use warnings; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 121 |  | 
| 8 | 8 |  |  | 8 |  | 33 |  | 
|  | 8 |  |  |  |  | 13 |  | 
|  | 8 |  |  |  |  | 323 |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use Readonly; | 
| 12 | 8 |  |  | 8 |  | 43 |  | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 385 |  | 
| 13 |  |  |  |  |  |  | use Perl::Critic::Utils qw{ :severities :data_conversion :classification }; | 
| 14 | 8 |  |  | 8 |  | 66 |  | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 361 |  | 
| 15 |  |  |  |  |  |  | use base 'Perl::Critic::Policy'; | 
| 16 | 8 |  |  | 8 |  | 2607 |  | 
|  | 8 |  |  |  |  | 32 |  | 
|  | 8 |  |  |  |  | 2824 |  | 
| 17 |  |  |  |  |  |  | Readonly::Scalar my $EXPL => q{Consider refactoring}; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | { | 
| 20 |  |  |  |  |  |  | return $SEVERITY_MEDIUM; | 
| 21 |  |  |  |  |  |  | } | 
| 22 | 8 |  |  | 8 | 1 | 80 |  | 
| 23 |  |  |  |  |  |  | { | 
| 24 |  |  |  |  |  |  | return qw(complexity maintenance); | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 0 |  |  | 0 | 1 | 0 | { | 
| 28 |  |  |  |  |  |  | return 'PPI::Statement::Sub'; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | { | 
| 32 | 13 |  |  | 13 | 1 | 186466 | return ( | 
| 33 |  |  |  |  |  |  | {   'name'            => 'condition_count_limit', | 
| 34 |  |  |  |  |  |  | 'description'     => 'The maximum condition count allowed.', | 
| 35 |  |  |  |  |  |  | 'default_string'  => '3', | 
| 36 |  |  |  |  |  |  | 'behavior'        => 'integer', | 
| 37 |  |  |  |  |  |  | 'integer_minimum' => 1, | 
| 38 | 13 |  |  | 13 | 0 | 93466 | }, | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | { | 
| 43 |  |  |  |  |  |  | my ( $self, $elem, undef ) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | my $s = $elem->find( | 
| 46 |  |  |  |  |  |  | sub | 
| 47 |  |  |  |  |  |  | { | 
| 48 |  |  |  |  |  |  | my ( undef, $element ) = @_; | 
| 49 | 11 |  |  | 11 | 1 | 211 |  | 
| 50 |  |  |  |  |  |  | my $interesting = | 
| 51 |  |  |  |  |  |  | $element->isa( 'PPI::Structure::Condition' ) | 
| 52 |  |  |  |  |  |  | || $element->isa( 'PPI::Structure::For' ) | 
| 53 |  |  |  |  |  |  | || $element->isa( 'PPI::Structure::Given' ); | 
| 54 | 783 |  |  | 783 |  | 6333 |  | 
| 55 |  |  |  |  |  |  | return $interesting; | 
| 56 | 783 |  | 100 |  |  | 3734 | } | 
| 57 |  |  |  |  |  |  | ); | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | if ( !$s ) { | 
| 60 |  |  |  |  |  |  | return; | 
| 61 | 783 |  |  |  |  | 1412 | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 11 |  |  |  |  | 68 | my $condition_count = @{ $s }; | 
| 64 |  |  |  |  |  |  | if ( $condition_count <= $self->{ '_condition_count_limit' } ) { | 
| 65 | 11 | 100 |  |  |  | 138 | return; | 
| 66 | 1 |  |  |  |  | 3 | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | my $desc; | 
| 69 | 10 |  |  |  |  | 17 | if ( my $name = $elem->name() ) { | 
|  | 10 |  |  |  |  | 28 |  | 
| 70 | 10 | 100 |  |  |  | 37 | $desc = qq<Subroutine "$name" with high condition count ($condition_count)>; | 
| 71 | 2 |  |  |  |  | 7 | } | 
| 72 |  |  |  |  |  |  | else { | 
| 73 |  |  |  |  |  |  | # never the case becaus no PPI::Statement::Sub | 
| 74 | 8 |  |  |  |  | 13 | $desc = qq<Anonymous subroutine with high condition count ($condition_count)>; | 
| 75 | 8 | 50 |  |  |  | 25 | } | 
| 76 | 8 |  |  |  |  | 455 |  | 
| 77 |  |  |  |  |  |  | return $self->violation( $desc, $EXPL, $elem ); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  |  |  | 0 | 1; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 8 |  |  |  |  | 34 | #----------------------------------------------------------------------------- | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =pod | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =encoding utf8 | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head1 NAME | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Perl::Critic::Policy::Mardem::ProhibitManyConditionsInSub | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | This Policy counts the conditions within a sub. | 
| 96 |  |  |  |  |  |  | (more precise the PPI::Structure::Condition's, | 
| 97 |  |  |  |  |  |  | PPI::Structure::For's and PPI::Structure::Given's) | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head1 CONFIGURATION | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | The maximum acceptable Condition-Count can be set with the | 
| 102 |  |  |  |  |  |  | C<condition_count_limit> configuration item. Any sub with a count higher than | 
| 103 |  |  |  |  |  |  | this number will generate a policy violation. The default is 3. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | An example section for a F<.perlcriticrc>: | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | [Mardem::ProhibitManyConditionsInSub] | 
| 108 |  |  |  |  |  |  | condition_count_limit = 1 | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =head1 AFFILIATION | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | This policy is part of L<Perl::Critic::Mardem>. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 AUTHOR | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Markus Demml, mardem@cpan.com | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Copyright (c) 2022, Markus Demml | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify it | 
| 123 |  |  |  |  |  |  | under the same terms as the Perl 5 programming language system itself. | 
| 124 |  |  |  |  |  |  | The full text of this license can be found in the LICENSE file included | 
| 125 |  |  |  |  |  |  | with this module. | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =cut |