blib/lib/B/Fathom.pm

filter: Statementstmt 59.2% 93 / 157 Branchbran 44.9% 44 / 98 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 65.0% 13 / 20 Podpod 25.0% 2 / 8 totaltotal 53.2% 156 / 293 CC 56 SCAR 34.4
CC
56
Cov
54%
score_code
67.6
tally_op
44.5
do_compile
17.8
CRAP 31.1
↑ Summary
1 package B::Fathom;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $VERSION = 0.07;
9  
10 =head1 NAME
11  
12 B::Fathom - a module to evaluate the readability of Perl code
13  
14 =head1 SYNOPSIS
15  
16 perl -MO=Fathom <script>
17  
18 or
19  
20 perl -MO=Fathom,-v <script>
21  
22 where E<lt>scriptE<gt> is the name of the Perl program that you
23 want to evaluate.
24  
25 C<-v> activates verbose mode, which currently reports which subs have been
26 skipped over because they seem to be imported. One can also indicate C<-vN>,
27 where C<N> is some number greater than zero, to provide I<even more> verbose
28 diagnostics. The specifics of these modes may change in future releases. See
29 comments in the code for further information.
30  
31 There is also an OO interface, which can be used as follows:
32  
33 my $fathom = B::Fathom->new('-v');
34 my $score = $fathom->fathom(\&foo);
35  
36 See METHODS below for a more complete explanation of the OO interface.
37  
38 =head1 DESCRIPTION
39  
40 C<B::Fathom> is a backend to the Perl compiler; it analyzes the syntax
41 of your Perl code, and estimates the readability of your program.
42  
43 Currently, this module's idea of `readability' is based on methods
44 used for analyzing readability of English prose. Further extensions
45 are intended.
46  
47 =head1 METHODS
48  
49 There is a simple object-oriented interface to B::Fathom. It consists of two
50 methods:
51  
52 =over 4
53  
54 =item new(@args)
55  
56 This method constructs a new compiler object. The optional @args indicate
57 compiler options; see SYNOPSIS for a list.
58  
59 =item fathom(@subrefs)
60  
61 This method grades the subroutines referred to by @subrefs, and returns
62 their score as a string.
63  
64 =back
65  
66 =head1 CAVEATS
67  
68 Because of the nature of the compiler, C<Fathom> has to do some
69 guessing about the syntax of your program. See the comments in the
70 module for specifics.
71  
72 C<Fathom> doesn't work very well on modules yet.
73  
74 =head1 AUTHOR
75  
76 Kurt Starsinic E<lt>F<kstar@cpan.org>E<gt>
77  
78 =head1 COPYRIGHT
79  
80 Copyright (c) 1998, 1999, 2000 Kurt Starsinic.
81 This module is free software; you may redistribute it
82 and/or modify it under the same terms as Perl itself.
83  
84 =cut
85  
86  
87 # TODO:
88 # Incorporate Halstead's effort equation and McCabe's cyclomatic metric.
89 # Process format statements, prototypes, and package statements.
90 # Do a more accurate job when processing modules, rather than scripts.
91 # Be smarter about parentheses.
92 # Find a `cooler' way to dereference CV's than using symbolic refs.
93  
94  
95 1 my (%Taken, %Name, @Skip_sub, @Subs_queue);
96 1 my ($Tok, $Expr, $State, $Sub) = (0, 0, 0, 0);
97 1 my $Verbose = 0;
98 1 my (%Boring) = (
99 pp_null => 1,
100 pp_enter => 1,
101 pp_pushmark => 1,
102 pp_unstack => 1,
103 pp_lineseq => 1,
104 pp_stub => 1,
105 );
106  
107  
108 # The `compile' subroutine is the meat of any compiler backend; see
109 # the documentation for B.pm for details.
110 sub compile
111 0 ▶{
Subroutine compile: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my (@args) = @_;
113  
114 0 _parse_args(@args);
115  
116 0 ▶ return sub { print do_compile() }
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 }
118  
119  
120 # This subroutine is called by either the compiler backend mechanism
121 # (via -MO=Fathom) or via the OO interface (via fathom()). If no
122 # parameters were passed in, this is a call from the compiler backend,
123 # and we fathom all of the code in main::. If parameters _are_ passed
124 # in, then they're a list of references to subroutines to fathom.
125 sub do_compile
126 1 ▶{
Subroutine do_compile: called CC=8 SCAR=28.8
Pod undocumented
127 1 my (@subrefs) = @_;
128 1 my $preamble = "";
129  
130 1 %Taken = %Name = @Skip_sub = @Subs_queue = ();
131 1 $Tok = $Expr = $State = $Sub = 0;
132  
133 1 ▶ if (@subrefs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subrefs) { } 1 0 1
134 1 foreach (@subrefs) {
135 1 ▶ return "$_ is not a subroutine ref" if ref ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ ne "CODE" 0 1 1
136 1 push @Subs_queue, B::svref_2object($_)->ROOT;
137 }
138 } else {
139 0 ▶ B::walksymtable(\%::, 'tally_symrefs', sub { 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 ▶ B::walksymtable(\%::, 'queue_subs', sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
141  
142 0 push @Subs_queue, B::main_root();
143  
144 0 $Sub++; # The body of the program counts as 1 subroutine.
145  
146 0 ▶ if ($Verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Verbose) 0 0 0
147 0 foreach (sort keys %Taken) {
148 $preamble .= "Skipping imported sub `$Name{$_}'\n"
149 0 ▶ if $Taken{$_} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Taken{$_} > 1 0 0 0
150 }
151 }
152 }
153  
154 1 foreach my $op (@Subs_queue) {
155 # Call the method `tally_op' on each OP in each of the
156 # optrees we're looping over:
157 1 B::walkoptree($op, 'tally_op');
158 }
159  
160 1 return $preamble . perline() . score_code();
161 }
162  
163  
164 sub score
165 1 ▶{
Subroutine score: called CC=4 SCAR=14.6
Pod undocumented
166 1 my ($tokens, $exprs, $statements, $subs) = @_;
167  
168 1 ▶ my $tok_expr = $exprs ? $tokens / $exprs : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exprs ? : 1 0 1
169 1 ▶ my $expr_state = $statements ? $exprs / $statements : 0;
Branch: 1 branch
BranchTrueFalseTotal
$statements ? : 1 0 1
170 1 ▶ my $state_sub = $subs ? $statements / $subs : 0;
Branch: 1 branch
BranchTrueFalseTotal
$subs ? : 1 0 1
171  
172 1 return ($tok_expr * .55) + ($expr_state * .28) + ($state_sub * .08);
173 }
174  
175  
176 sub score_code
177 1 ▶{
Subroutine score_code: called CC=17 SCAR=42.1
Pod undocumented
178 1 my $opinion;
179 1 my $output = "";
180  
181 0 ▶ if ($Tok == 0) { return "No tokens; score is meaningless.\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($Tok == 0) 0 1 1
182 0 ▶ if ($Expr == 0) { return "No expressions; score is meaningless.\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($Expr == 0) 0 1 1
183 0 ▶ if ($State == 0) { return "No statements; score is meaningless.\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($State == 0) 0 1 1
184 0 ▶ if ($Sub == 0) { return "No subroutines; score is meaningless.\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($Sub == 0) 0 1 1
185  
186 1 my $score = score($Tok, $Expr, $State, $Sub);
187  
188 0 ▶ if ($score < 1) { $opinion = "trivial" }
Branch: 8 branches
BranchTrueFalseTotal
if ($score < 1) { } 0 1 1
elsif ($score < 2) { } 0 1 1
elsif ($score < 3) { } 1 0 1
elsif ($score < 4) { } 0 0 0
elsif ($score < 5) { } 0 0 0
elsif ($score < 6) { } 0 0 0
elsif ($score < 7) { } 0 0 0
elsif ($score < 8) { } 0 0 0
189 0 elsif ($score < 2) { $opinion = "easy" }
190 1 elsif ($score < 3) { $opinion = "very readable" }
191 0 elsif ($score < 4) { $opinion = "readable" }
192 0 elsif ($score < 5) { $opinion = "easier than the norm" }
193 0 elsif ($score < 6) { $opinion = "mature" }
194 0 elsif ($score < 7) { $opinion = "complex" }
195 0 elsif ($score < 8) { $opinion = "very difficult" }
196 0 else { $opinion = "obfuscated" }
197  
198 1 ▶ $output .= sprintf "%5d token%s\n", $Tok, ($Tok == 1 ? "" : "s");
Branch: 1 branch
BranchTrueFalseTotal
$Tok == 1 ? : 0 1 1
199 1 ▶ $output .= sprintf "%5d expression%s\n", $Expr, ($Expr == 1 ? "" : "s");
Branch: 1 branch
BranchTrueFalseTotal
$Expr == 1 ? : 0 1 1
200 1 ▶ $output .= sprintf "%5d statement%s\n", $State, ($State == 1 ? "" : "s");
Branch: 1 branch
BranchTrueFalseTotal
$State == 1 ? : 0 1 1
201 1 ▶ $output .= sprintf "%5d subroutine%s\n", $Sub, ($Sub == 1 ? "" : "s");
Branch: 1 branch
BranchTrueFalseTotal
$Sub == 1 ? : 1 0 1
202  
203 1 $output .= sprintf "readability is %.2f (%s)\n", $score, $opinion;
204  
205 1 return $output;
206 }
207  
208  
209 # This method is called on each OP in the tree we're examining; see
210 # do_compile() above. It examines the OP, and then increments the
211 # count of tokens, expressions, statements, and subroutines as
212 # appropriate.
213  
214 1 my $linenum;
215 1 my (%TokPerLine, %ExprPerLine, %StatePerLine, %SubPerLine);
216  
217 2 ▶sub perline {
Subroutine perline: called CC=3 SCAR=16.0
Pod undocumented
218 2 ▶ if ($Verbose > 1 and defined $linenum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Verbose > 1 and defined $linenum) 0 2 2
Condition33%1/3
$Verbose > 1 and defined $linenum
!ll&&!rl&&r
200
MC/DC0%0/2
$Verbose > 1 and defined $linenum
$Verbose > 1 defined $linenum
Truth table33%1/3
A and B
A= $Verbose > 1B= defined $linenum
ABresult
0X0
100
111
219 my $output = sprintf
220 "%4d %2d tokens %2d expressions %2d statements %2d subs %s\n",
221 $linenum, $TokPerLine{$linenum}, $ExprPerLine{$linenum},
222 $StatePerLine{$linenum}, $SubPerLine{$linenum},
223 score($TokPerLine{$linenum}, $ExprPerLine{$linenum},
224 0 $StatePerLine{$linenum}, $SubPerLine{$linenum})
225 ;
226  
227 0 undef $linenum;
228  
229 0 return $output;
230 }
231  
232 2 return "";
233 }
234  
235  
236 ###
237 ### The next three subs are all in package B::OBJECT; this is so
238 ### that all OP's will inherit the subs as methods.
239 ###
240  
241  
242 sub B::OBJECT::tally_op
243 6 ▶{
Subroutine tally_op: called CC=21 SCAR=38.0
244 6 my ($self) = @_;
245 6 ▶ my $ppaddr = $self->can('ppaddr') ? $self->ppaddr : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('ppaddr') ? : 6 0 6
246 6 my $output = "";
247  
248 # Normalize EMBED and non-EMBED ppaddr's:
249 6 ▶ $ppaddr =~ s/^Perl_// or # Historic
Branch: 1 branch
BranchTrueFalseTotal
$ppaddr =~ s/^Perl_// or $ppaddr =~ s/^PL_ppaddr\[OP_(\w+)\]/'pp_' . lc($1);/e 0 6 6
250 6 $ppaddr =~ s/^PL_ppaddr\[OP_(\w+)\]/'pp_' . lc $1/e; # 5.6.0+
251  
252 6 ▶ if ($self->can('line')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("line")) 1 5 6
253 1 $output = perline();
254 1 $linenum = $self->line;
255 }
256  
257 6 ▶ $output .= sprintf("%3d %-15s %s\n", $linenum, $ppaddr, ref($self))
Branch: 1 branch
BranchTrueFalseTotal
if $Verbose > 1 0 6 6
258 if $Verbose > 1;
259  
260 6 my ($TokOld, $ExprOld, $StateOld, $SubOld) = ($Tok, $Expr, $State, $Sub);
261  
262 6 ▶ if ($Boring{$ppaddr}) {
Branch: 14 branches
BranchTrueFalseTotal
if ($Boring{$ppaddr}) { } 2 4 6
elsif ($ppaddr eq 'pp_nextstate' or $ppaddr eq 'pp_dbstate') { } 1 3 4
elsif ($ppaddr eq 'pp_leavesub') { } 1 2 3
elsif ($ppaddr =~ /^pp_leave/) { } 0 2 2
elsif ($ppaddr eq 'pp_entertry') { } 0 2 2
elsif ($ppaddr eq 'pp_anoncode') { } 0 2 2
elsif ($ppaddr eq 'pp_scope') { } 0 2 2
elsif ($ppaddr eq 'pp_entersub') { } 0 2 2
elsif ($self->isa('B::LOOP')) { } 0 2 2
elsif ($self->isa('B::LISTOP')) { } 1 1 2
elsif ($self->isa('B::BINOP')) { } 0 1 1
elsif ($self->isa('B::LOGOP')) { } 0 1 1
elsif ($self->isa('B::CONDOP')) { } 0 1 1
elsif ($self->isa('B::UNOP')) { } 0 1 1
263 # Do nothing; these OPs don't count
264 ▶ } elsif ($ppaddr eq 'pp_nextstate' or $ppaddr eq 'pp_dbstate') {
Condition66%2/3
$ppaddr eq "pp_nextstate" or $ppaddr eq "pp_dbstate"
l!l&&r!l&&!r
103
MC/DC50%1/2
$ppaddr eq "pp_nextstate" or $ppaddr eq "pp_dbstate"
$ppaddr eq "pp_nextstate" $ppaddr eq "pp_dbstate"
Truth table66%2/3
A or B
A= $ppaddr eq "pp_nextstate"B= $ppaddr eq "pp_dbstate"
ABresult
1X1
011
000
265 1 $Tok += 1; $State += 1;
266 } elsif ($ppaddr eq 'pp_leavesub') { # sub name { <xxx> }
267 1 $Tok += 4; $Expr += 1; $State += 1; $Sub += 1;
268 } elsif ($ppaddr =~ /^pp_leave/) {
269 # pp_leave* is already accounted for in its matching pp_enter*
270 } elsif ($ppaddr eq 'pp_entertry') { # eval { <xxx> }
271 0 $Tok += 3; $Expr += 1;
272 } elsif ($ppaddr eq 'pp_anoncode') { # sub { <xxx> }
273 0 $Tok += 3; $Expr += 1;
274 } elsif ($ppaddr eq 'pp_scope') { # do { <xxx> }
275 0 $Tok += 3; $Expr += 1;
276 } elsif ($ppaddr eq 'pp_entersub') { # foo()
277 0 $Tok += 3; $Expr += 1;
278 } elsif ($self->isa('B::LOOP')) { # for (<xxx>) { <yyy> }
279 0 $Tok += 5; $Expr += 2;
280 } elsif ($self->isa('B::LISTOP')) { # OP(<xxx>)
281 1 $Tok += 3; $Expr += 1;
282 } elsif ($self->isa('B::BINOP')) { # <xxx> OP <yyy>
283 0 $Tok += 1; $Expr += 1;
284 } elsif ($self->isa('B::LOGOP')) { # <xxx> OP <yyy>
285 0 $Tok += 1; $Expr += 1;
286 } elsif ($self->isa('B::CONDOP')) { # while (<xxx>) { <yyy> }
287 0 $Tok += 5; $Expr += 2;
288 } elsif ($self->isa('B::UNOP')) { # OP <xxx>
289 0 $Tok += 1; $Expr += 1;
290 } else { # OP
291 1 $Tok += 1;
292 }
293  
294 6 ▶ if (defined $linenum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $linenum) 4 2 6
295 4 $TokPerLine{$linenum} += $Tok - $TokOld;
296 4 $ExprPerLine{$linenum} += $Expr - $ExprOld;
297 4 $StatePerLine{$linenum} += $State - $StateOld;
298 4 $SubPerLine{$linenum} += $Sub - $SubOld;
299 }
300  
301 6 return $output;
302 }
303  
304  
305 # Keep track of the sub associated with each symbol. If we find multiple
306 # symbol table entries pointing to one sub, then we'll guess (in
307 # do_compile()) that the sub is imported, and we'll ignore it. Thanks
308 # to Mark-Jason Dominus for suggesting this strategy.
309 sub B::OBJECT::tally_symrefs
310 0 ▶{
Subroutine tally_symrefs: not called CC=2 SCAR=13.0
311 0 my ($symbol) = @_;
312 0 my $name = full_subname($symbol);
313  
314 # We're creating a `symbolic reference' in this block
315 # (see perlref(1)), which is why we need `no strict':
316 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
317 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
318 0 my $coderef = \&{"$name"};
319  
320 0 $Taken{$coderef}++;
321 0 $Name{$coderef} = $name;
322 }
323 }
324  
325  
326 # Create an array of OP's for introspection. These are the `root' OP's
327 # of each sub that we're going to examine.
328 sub B::OBJECT::queue_subs
329 0 ▶{
Subroutine queue_subs: not called CC=3 SCAR=19.6
330 0 my ($symbol) = @_;
331 0 my $name = full_subname($symbol);
332  
333 # We're creating a `symbolic reference' in this block
334 # (see perlref(1)), which is why we need `no strict':
335 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
336 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
337 0 my $coderef = \&{"$name"};
338  
339 0 ▶ push @Subs_queue, $symbol->CV->ROOT unless $Taken{$coderef} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $Taken{$coderef} > 1 0 0 0
340 }
341 }
342  
343  
344 # Given a symbol table entry $symbol, return the fully qualified subroutine
345 # name of the associated subroutine; if there is none, return undef.
346 sub full_subname
347 0 ▶{
Subroutine full_subname: not called CC=2 SCAR=17.9
Pod undocumented
348 0 my ($symbol) = @_;
349  
350 # Build the full subname from the stashname and the symbolname:
351 0 ▶ if ($symbol->CV->isa('B::CV')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol->CV->isa('B::CV')) { } 0 0 0
352 0 return $symbol->STASH->NAME . "::" . $symbol->NAME;
353 } else {
354 0 return undef;
355 }
356 }
357  
358  
359 sub _parse_args
360 1 ▶{
Subroutine _parse_args: called CC=4 SCAR=14.9
361 1 my (@args) = @_;
362  
363 1 foreach (@args) {
364 1 ▶ if (/-v(.*)/) { $Verbose = length($1) ? $1 : 1 }
Branch: 2 branches
BranchTrueFalseTotal
if (/-v(.*)/) { } 1 0 1
length $1 ? : 0 1 1
365 0 else { die "Unknown argument: `$_'" }
366 }
367  
368 1 return;
369 }
370  
371  
372 ###
373 ### OO interface (thanks to Stephen McCamant for the idea, and
374 ### Doug MacEachern for the encouragement):
375 ###
376 ### Please note that, for now, one can only successfully have one
377 ### B::Fathom object at a time, as compilation arguments are globals.
378 ###
379  
380 sub new
381 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
382 1 my ($class, @args) = @_;
383 1 my $self = bless {}, $class;
384  
385 1 _parse_args(@args);
386  
387 1 return $self;
388 }
389  
390  
391 sub fathom
392 1 ▶{
Subroutine fathom: called CC=1 SCAR=0.0
Pod documented
393 1 my ($self, @subrefs) = @_;
394  
395 1 return do_compile(@subrefs);
396 }
397  
398  
399 1 1;
400  
401  
↑ Summary