blib/lib/Number/Stars.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 43 / 43 SCAR 16.1
CC
5
Cov
100%
percent_stars
4.0
new
2.0
BEGIN
1.0
CRAP 5.0
↑ Summary
1 package Number::Stars;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = 0.01;
10  
11 # Constructor.
12 sub new {
13 6 ▶ my ($class, @params) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
14  
15 # Create object.
16 6 my $self = bless {}, $class;
17  
18 # Number of stars.
19 6 $self->{'number_of_stars'} = 10;
20  
21 # Process parameters.
22 6 set_params($self, @params);
23  
24 5 ▶ if ($self->{'number_of_stars'} !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"number_of_stars"} =~ /^\d+$/) 1 4 5
25 1 err "Parameter 'number_of_stars' must be a number.";
26 }
27  
28 4 return $self;
29 }
30  
31 # Convert percent number to stars definition.
32 sub percent_stars {
33 6 ▶ my ($self, $percent) = @_;
Subroutine percent_stars: called CC=4 SCAR=13.9
Pod documented
34  
35 6 my $stars_hr = {};
36 6 my $star_percent = 100 / $self->{'number_of_stars'};
37 6 foreach my $star_id (1 .. $self->{'number_of_stars'}) {
38 53 ▶ if ($percent >= $star_id * $star_percent) {
Branch: 2 branches
BranchTrueFalseTotal
if ($percent >= $star_id * $star_percent) { } 26 27 53
elsif ($percent >= $star_id * $star_percent - $star_percent / 2) { } 2 25 27
39 26 $stars_hr->{$star_id} = 'full';
40 } elsif ($percent >= ($star_id * $star_percent) - ($star_percent / 2)) {
41 2 $stars_hr->{$star_id} = 'half',
42 } else {
43 25 $stars_hr->{$star_id} = 'nothing',
44 }
45 }
46  
47 6 return $stars_hr;
48 }
49  
50 __END__
↑ Summary