« lib/Perl/Critic/Exception/Configuration/Option/Policy/ParameterValue.pm
↑ Summary
lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitQuotesAsQuotelikeOperatorDelimiters.pm »
| 1 | package Perl::Critic::Utils::McCabe; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw{ :data_conversion :classification }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 40 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||||||
| 14 | |||||||||||||||
| 15 | 40 | our $VERSION = '1.156'; | |||||||||||||
| 16 | |||||||||||||||
| 17 | #----------------------------------------------------------------------------- | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 40 | Readonly::Array our @EXPORT_OK => | |||||||||||||
| 20 | qw( calculate_mccabe_of_sub calculate_mccabe_of_main ); | ||||||||||||||
| 21 | |||||||||||||||
| 22 | #----------------------------------------------------------------------------- | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 40 | Readonly::Hash my %LOGIC_OPS => | |||||||||||||
| 25 | hashify( qw( && || ||= &&= or and xor ? <<= >>= ) ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 40 | Readonly::Hash my %LOGIC_KEYWORDS => | |||||||||||||
| 28 | hashify( qw( if else elsif unless until while for foreach ) ); | ||||||||||||||
| 29 | |||||||||||||||
| 30 | #----------------------------------------------------------------------------- | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 15 | ▶ | sub calculate_mccabe_of_sub { | ||||||||||||
|
Subroutine
calculate_mccabe_of_sub: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 15 | my ( $sub ) = @_; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 15 | my $count = 1; # Minimum score is 1 | |||||||||||||
| 37 | 15 | $count += _count_logic_keywords( $sub ); | |||||||||||||
| 38 | 15 | $count += _count_logic_operators( $sub ); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 15 | return $count; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | #----------------------------------------------------------------------------- | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 31 | ▶ | sub calculate_mccabe_of_main { | ||||||||||||
|
Subroutine
calculate_mccabe_of_main: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 31 | my ( $doc ) = @_; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 31 | my $count = 1; # Minimum score is 1 | |||||||||||||
| 50 | 31 | $count += _count_main_logic_operators_and_keywords( $doc ); | |||||||||||||
| 51 | 31 | return $count; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | #----------------------------------------------------------------------------- | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 31 | ▶ | sub _count_main_logic_operators_and_keywords { | ||||||||||||
|
Subroutine
_count_main_logic_operators_and_keywords: called CC=2 SCAR=6.9
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | 31 | my ( $doc ) = @_; | |||||||||||||
| 59 | |||||||||||||||
| 60 | # I can't leverage Perl::Critic::Document's fast search mechanism here | ||||||||||||||
| 61 | # because we're not searching for elements by class name. So to speed | ||||||||||||||
| 62 | # things up, search for both keywords and operators at the same time. | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 2429 | ▶ | my $wanted = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | 2429 | my (undef, $elem) = @_; | |||||||||||||
| 67 | |||||||||||||||
| 68 | # Only count things that *are not* in a subroutine. Returning an | ||||||||||||||
| 69 | # explicit 'undef' here prevents PPI from descending into the node. | ||||||||||||||
| 70 | |||||||||||||||
| 71 | ## no critic (ProhibitExplicitReturnUndef) | ||||||||||||||
| 72 | 2429 | ▶ | return undef if $elem->isa('PPI::Statement::Sub'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | 2424 | ▶ | if ( $elem->isa('PPI::Token::Word') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 76 | 313 | ▶ | return 0 if is_hash_key( $elem ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 313 | return exists $LOGIC_KEYWORDS{$elem}; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | elsif ($elem->isa('PPI::Token::Operator') ) { | ||||||||||||||
| 80 | 133 | return exists $LOGIC_OPS{$elem}; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | 31 | }; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 31 | my $logic_operators_and_keywords = $doc->find( $wanted ); | |||||||||||||
| 85 | |||||||||||||||
| 86 | my $count = $logic_operators_and_keywords ? | ||||||||||||||
| 87 | 31 | ▶ | scalar @{$logic_operators_and_keywords} : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | |||||||||||||||
| 89 | 31 | return $count; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | #----------------------------------------------------------------------------- | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 15 | ▶ | sub _count_logic_keywords { | ||||||||||||
|
Subroutine
_count_logic_keywords: called CC=2 SCAR=6.9
| |||||||||||||||
| 95 | |||||||||||||||
| 96 | 15 | my ( $sub ) = @_; | |||||||||||||
| 97 | 15 | my $count = 0; | |||||||||||||
| 98 | |||||||||||||||
| 99 | # Here, I'm using this round-about method of finding elements so | ||||||||||||||
| 100 | # that I can take advantage of Perl::Critic::Document's faster | ||||||||||||||
| 101 | # find() mechanism. It can only search for elements by class name. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 15 | my $keywords_ref = $sub->find('PPI::Token::Word'); | |||||||||||||
| 104 | 15 | ▶ | if ( $keywords_ref ) { # should always be true due to "sub" keyword | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 15 | my @filtered = grep { ! is_hash_key($_) } @{ $keywords_ref }; | |||||||||||||
| 106 | 15 | $count = grep { exists $LOGIC_KEYWORDS{$_} } @filtered; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | 15 | return $count; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | #----------------------------------------------------------------------------- | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 15 | ▶ | sub _count_logic_operators { | ||||||||||||
|
Subroutine
_count_logic_operators: called CC=2 SCAR=6.9
| |||||||||||||||
| 114 | |||||||||||||||
| 115 | 15 | my ( $sub ) = @_; | |||||||||||||
| 116 | 15 | my $count = 0; | |||||||||||||
| 117 | |||||||||||||||
| 118 | # Here, I'm using this round-about method of finding elements so | ||||||||||||||
| 119 | # that I can take advantage of Perl::Critic::Document's faster | ||||||||||||||
| 120 | # find() mechanism. It can only search for elements by class name. | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 15 | my $operators_ref = $sub->find('PPI::Token::Operator'); | |||||||||||||
| 123 | 15 | ▶ | if ( $operators_ref ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 3 | $count = grep { exists $LOGIC_OPS{$_} } @{ $operators_ref }; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 15 | return $count; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | 40 | 1; | |||||||||||||
| 132 | |||||||||||||||
| 133 | __END__ | ||||||||||||||