| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  |  | 
| 2 |  |  |  |  |  |  | use utf8; | 
| 3 | 8 |  |  | 8 |  | 4915 |  | 
|  | 8 |  |  |  |  | 19 |  | 
|  | 8 |  |  |  |  | 36 |  | 
| 4 |  |  |  |  |  |  | use 5.010; | 
| 5 | 8 |  |  | 8 |  | 280 |  | 
|  | 8 |  |  |  |  | 23 |  | 
| 6 |  |  |  |  |  |  | use strict; | 
| 7 | 8 |  |  | 8 |  | 38 | use warnings; | 
|  | 8 |  |  |  |  | 13 |  | 
|  | 8 |  |  |  |  | 140 |  | 
| 8 | 8 |  |  | 8 |  | 36 |  | 
|  | 8 |  |  |  |  | 24 |  | 
|  | 8 |  |  |  |  | 439 |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use Readonly; | 
| 12 | 8 |  |  | 8 |  | 54 |  | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 395 |  | 
| 13 |  |  |  |  |  |  | use Perl::Critic::Utils qw{ :severities :data_conversion :classification }; | 
| 14 | 8 |  |  | 8 |  | 56 | use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_main }; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 371 |  | 
| 15 | 8 |  |  | 8 |  | 2720 |  | 
|  | 8 |  |  |  |  | 18 |  | 
|  | 8 |  |  |  |  | 338 |  | 
| 16 |  |  |  |  |  |  | use Mardem::RefactoringPerlCriticPolicies::Util qw( search_for_block_keyword ); | 
| 17 | 8 |  |  | 8 |  | 42 |  | 
|  | 8 |  |  |  |  | 25 |  | 
|  | 8 |  |  |  |  | 330 |  | 
| 18 |  |  |  |  |  |  | use base 'Perl::Critic::Policy'; | 
| 19 | 8 |  |  | 8 |  | 45 |  | 
|  | 8 |  |  |  |  | 13 |  | 
|  | 8 |  |  |  |  | 2563 |  | 
| 20 |  |  |  |  |  |  | Readonly::Scalar my $EXPL => q{Consider refactoring}; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | { | 
| 23 |  |  |  |  |  |  | return $SEVERITY_MEDIUM; | 
| 24 |  |  |  |  |  |  | } | 
| 25 | 18 |  |  | 18 | 1 | 175 |  | 
| 26 |  |  |  |  |  |  | { | 
| 27 |  |  |  |  |  |  | return qw(complexity maintenance); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 0 |  |  | 0 | 1 | 0 | { | 
| 31 |  |  |  |  |  |  | return 'PPI::Structure::Block'; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | { | 
| 35 | 23 |  |  | 23 | 1 | 252694 | return ( | 
| 36 |  |  |  |  |  |  | {   'name'            => 'max_mccabe', | 
| 37 |  |  |  |  |  |  | 'description'     => 'The maximum complexity score allowed.', | 
| 38 |  |  |  |  |  |  | 'default_string'  => '10', | 
| 39 |  |  |  |  |  |  | 'behavior'        => 'integer', | 
| 40 |  |  |  |  |  |  | 'integer_minimum' => 1, | 
| 41 | 23 |  |  | 23 | 0 | 118496 | }, | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | { | 
| 46 |  |  |  |  |  |  | my ( $self, $elem, undef ) = @_; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | my $score = calculate_mccabe_of_main( $elem ); | 
| 49 |  |  |  |  |  |  | if ( $score <= $self->{ '_max_mccabe' } ) { | 
| 50 |  |  |  |  |  |  | return; | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 24 |  |  | 24 | 1 | 870 |  | 
| 53 |  |  |  |  |  |  | my $block_keyword = search_for_block_keyword( $elem ); | 
| 54 | 24 |  |  |  |  | 71 | if ( !$block_keyword ) { | 
| 55 | 24 | 100 |  |  |  | 14636 | $block_keyword = 'no-keyword-found'; | 
| 56 | 5 |  |  |  |  | 14 | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | if ( 'SUB' eq $block_keyword ) { | 
| 59 | 19 |  |  |  |  | 50 | return;    # no sub -> see SUB Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity ! | 
| 60 | 19 | 50 |  |  |  | 39 | } | 
| 61 | 0 |  |  |  |  | 0 |  | 
| 62 |  |  |  |  |  |  | my $desc = qq<"$block_keyword" code-block has a high complexity score ($score)>; | 
| 63 |  |  |  |  |  |  | return $self->violation( $desc, $EXPL, $elem ); | 
| 64 | 19 | 100 |  |  |  | 37 | } | 
| 65 | 1 |  |  |  |  | 5 |  | 
| 66 |  |  |  |  |  |  | 1; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 18 |  |  |  |  | 48 |  | 
| 69 | 18 |  |  |  |  | 58 | #----------------------------------------------------------------------------- | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =pod | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =encoding utf8 | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head1 NAME | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Perl::Critic::Policy::Mardem::ProhibitBlockComplexity | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | This Policy approximates the McCabe score within a code block "eg if() {...}". | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | See L<http://en.wikipedia.org/wiki/Cyclomatic_complexity> | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | It should help to find complex code block, which should be extracted | 
| 86 |  |  |  |  |  |  | into subs, to be more testable. | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | eg. from | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | if( $a ) { | 
| 91 |  |  |  |  |  |  | ... | 
| 92 |  |  |  |  |  |  | ... | 
| 93 |  |  |  |  |  |  | ... | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | to | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | if( $a ) { | 
| 99 |  |  |  |  |  |  | do_something(); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =head1 CONFIGURATION | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | The maximum acceptable McCabe can be set with the C<max_mccabe> | 
| 105 |  |  |  |  |  |  | configuration item. Any block with a McCabe score higher than | 
| 106 |  |  |  |  |  |  | this number will generate a policy violation. The default is 10. | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | An example section for a F<.perlcriticrc>: | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | [Mardem::ProhibitBlockComplexity] | 
| 111 |  |  |  |  |  |  | max_mccabe = 1 | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head1 AFFILIATION | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | This policy is part of L<Mardem::RefactoringPerlCriticPolicies>. | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head1 AUTHOR | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | Markus Demml, mardem@cpan.com | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | Copyright (c) 2022, Markus Demml | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify it | 
| 126 |  |  |  |  |  |  | under the same terms as the Perl 5 programming language system itself. | 
| 127 |  |  |  |  |  |  | The full text of this license can be found in the LICENSE file included | 
| 128 |  |  |  |  |  |  | with this module. | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =cut |