1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Utils/McCabe.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Utils::McCabe;
16 use Perl::Critic::Utils qw{ :data_conversion :classification };
20 #-----------------------------------------------------------------------------
22 our $VERSION = '1.088';
24 #-----------------------------------------------------------------------------
26 Readonly::Array our @EXPORT_OK =>
27 qw( calculate_mccabe_of_sub calculate_mccabe_of_main );
29 #-----------------------------------------------------------------------------
31 Readonly::Hash my %LOGIC_OPS =>
32 hashify( qw( && || ||= &&= or and xor ? <<= >>= ) );
34 Readonly::Hash my %LOGIC_KEYWORDS =>
35 hashify( qw( if else elsif unless until while for foreach ) );
37 #-----------------------------------------------------------------------------
39 sub calculate_mccabe_of_sub {
43 my $count = 1; # Minimum score is 1
44 $count += _count_logic_keywords( $sub );
45 $count += _count_logic_operators( $sub );
50 #-----------------------------------------------------------------------------
52 sub calculate_mccabe_of_main {
56 my $count = 1; # Minimum score is 1
57 $count += _count_main_logic_operators_and_keywords( $doc );
61 #-----------------------------------------------------------------------------
63 sub _count_main_logic_operators_and_keywords {
67 # I can't leverage Perl::Critic::Document's fast search mechanism here
68 # because we're not searching for elements by class name. So to speed
69 # things up, search for both keywords and operators at the same time.
73 my (undef, $elem) = @_;
75 # Only count things that *are not* in a subroutine. Returning an
76 # explicit 'undef' here prevents PPI from descending into the node.
78 ## no critic Subroutines::ProhibitExplicitReturnUndef
79 return undef if $elem->isa('PPI::Statement::Sub');
82 if ( $elem->isa('PPI::Token::Word') ) {
83 return 0 if is_hash_key( $elem );
84 return exists $LOGIC_KEYWORDS{$elem};
86 elsif ($elem->isa('PPI::Token::Operator') ) {
87 return exists $LOGIC_OPS{$elem};
91 my $logic_operators_and_keywords = $doc->find( $wanted );
93 my $count = $logic_operators_and_keywords ?
94 scalar @{$logic_operators_and_keywords} : 0;
99 #-----------------------------------------------------------------------------
101 sub _count_logic_keywords {
106 # Here, I'm using this round-about method of finding elements so
107 # that I can take advantage of Perl::Critic::Document's faster
108 # find() mechanism. It can only search for elements by class name.
110 my $keywords_ref = $sub->find('PPI::Token::Word');
111 if ( $keywords_ref ) { # should always be true due to "sub" keyword
112 my @filtered = grep { ! is_hash_key($_) } @{ $keywords_ref };
113 $count = grep { exists $LOGIC_KEYWORDS{$_} } @filtered;
118 #-----------------------------------------------------------------------------
120 sub _count_logic_operators {
125 # Here, I'm using this round-about method of finding elements so
126 # that I can take advantage of Perl::Critic::Document's faster
127 # find() mechanism. It can only search for elements by class name.
129 my $operators_ref = $sub->find('PPI::Token::Operator');
130 if ( $operators_ref ) {
131 $count = grep { exists $LOGIC_OPS{$_} } @{ $operators_ref };
142 #-----------------------------------------------------------------------------
146 =for stopwords McCabe
150 Perl::Critic::Utils::McCabe - Functions that calculate the McCabe score of source code.
154 Provides approximations of McCabe scores. The McCabe score of a set
155 of code describes the number of possible paths through it. The
156 functions here approximate the McCabe score by summing the number of
157 conditional statements and operators within a set of code. See
158 L<http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html> for
159 some discussion about the McCabe number and other complexity metrics.
162 =head1 IMPORTABLE SUBS
166 =item C<calculate_mccabe_of_sub( $sub )>
168 Calculates an approximation of the McCabe number of the code in a
169 L<PPI::Statement::Sub>.
171 =item C<calculate_mccabe_of_main( $doc )>
173 Calculates an approximation of the McCabe number of all the code in a
174 L<PPI::Statement::Document> that is B<not> contained in a subroutine.
181 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
185 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
187 This program is free software; you can redistribute it and/or modify
188 it under the same terms as Perl itself. The full text of this license
189 can be found in the LICENSE file included with this module.
195 # cperl-indent-level: 4
197 # indent-tabs-mode: nil
198 # c-indentation-style: bsd
200 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :