blib/lib/WWW/HTMLTagAttributeCounter.pm

filter: Statementstmt 88.3% 53 / 60 Branchbran 73.0% 19 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 82.0% 87 / 106 CC 20 SCAR 19.4
CC
20
Cov
82%
_count
11.0
count
10.4
result_readable
3.0
CRAP 7.0
↑ Summary
1 package WWW::HTMLTagAttributeCounter;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.001001'; # VERSION
7  
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use overload q|""| => sub { shift->result_readable };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 2 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 __PACKAGE__->mk_group_accessors( simple => qw/
14 ua
15 result
16 error
17 /);
18  
19 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
20 1 my ( $class, %args ) = @_;
21  
22 1 ▶ $args{ua} ||= LWP::UserAgent->new(
Condition33%1/3
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
$args{"ua"} "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
Truth table33%1/3
A ||= B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
ABresult
1X1
011
000
23 timeout => 30,
24 agent => 'Opera 9.5',
25 );
26  
27 1 my $self = bless {}, $class;
28 1 $self->ua( $args{ua} );
29  
30 1 return $self;
31 }
32  
33 4 ▶sub count {
Subroutine count: called CC=7 SCAR=23.4
Pod documented
34 4 my ( $self, $where, $what, $type, ) = @_;
35  
36 $self->$_(undef)
37 4 for qw/error result/;
38  
39 4 ▶ $what = [ $what ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $what eq "ARRAY" 2 2 4
40 unless ref $what eq 'ARRAY';
41  
42 4 ▶ defined $type
Branch: 1 branch
BranchTrueFalseTotal
defined $type or $type = "tag" 1 3 4
43 or $type = 'tag';
44  
45 4 my $content;
46 4 ▶ if ( ref $where eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where eq 'SCALAR') { } 4 0 4
47 4 $content = $$where;
48 }
49 else {
50 0 ▶ $where =~ m{^https?://}
Branch: 1 branch
BranchTrueFalseTotal
$where =~ m[^https?://] or $where = "http://$where" 0 0 0
51 or $where = "http://$where";
52  
53 0 my $response = $self->ua->get( $where );
54  
55 0 ▶ $response->is_success
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or return $self->_set_error($response) 0 0 0
56 or return $self->_set_error( $response );
57  
58 0 $content = $response->decoded_content;
59 }
60  
61 4 return $self->result( $self->_count( $what, $type, $content ) );
62 }
63  
64 3 ▶sub result_readable {
Subroutine result_readable: called CC=3 SCAR=11.0
Pod documented
65 3 my $result = shift->result;
66  
67 3 my @out;
68 3 for ( sort keys %$result ) {
69 9 push @out, "$result->{$_} $_";
70 }
71  
72 3 ▶ return $out[0]
Branch: 1 branch
BranchTrueFalseTotal
if @out == 1 0 3 3
73 if @out == 1;
74  
75 3 return (join q|, |, @out[0..$#out-1]) . ' and ' . $out[-1];
76 }
77  
78 4 ▶sub _count {
Subroutine _count: called CC=11 SCAR=24.0
79 4 my ( $self, $what, $type, $content ) = @_;
80  
81 4 my $p = HTML::TokeParser::Simple->new( \$content );
82 4 my $count = {};
83 4 ▶ while ( my $t = $p->get_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $p->get_token) 52 4 56
84 next
85 52 ▶ unless $t->is_start_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $t->is_start_tag 36 16 52
86  
87 16 ▶ if ( $type eq 'tag' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'tag') { } 12 4 16
elsif ($type eq 'attr') { } 4 0 4
88 12 for ( @$what ) {
89 $t->is_start_tag($_)
90 28 ▶ and $count->{$_}++;
Branch: 1 branch
BranchTrueFalseTotal
$t->is_start_tag($_) and ++$count->{$_} 9 19 28
91 }
92 }
93 elsif ( $type eq 'attr') {
94 4 for ( @$what ) {
95 defined $t->get_attr($_)
96 4 ▶ and $count->{$_}++;
Branch: 1 branch
BranchTrueFalseTotal
defined $t->get_attr($_) and ++$count->{$_} 3 1 4
97 }
98 }
99 }
100  
101 defined $count->{$_}
102 or $count->{$_} = 0
103 4 ▶ for @$what;
Branch: 1 branch
BranchTrueFalseTotal
defined $count->{$_} or $count->{$_} = 0 6 2 8
104  
105 4 return $count;
106 }
107  
108 0 ▶sub _set_error {
Subroutine _set_error: not called CC=1 SCAR=6.9
109 0 my ( $self, $response ) = @_;
110 0 $self->error('Network error: ' . $response->status_line );
111 0 return;
112 }
113  
114 2 1;
115 __END__
↑ Summary