File Coverage

blib/lib/App/PerlNitpick/Rule/RemoveEffectlessUTF8Pragma.pm
Criterion Covered Total %
statement 22 23 95.6
branch 7 10 70.0
condition 6 9 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 39 47 82.9


line stmt bran cond sub pod time code
1             # ABSTRACT: Re-quote strings with single quotes ('') if they look "simple"
2              
3             use Moose;
4 1     1   174893 use PPI::Document;
  1         367230  
  1         7  
5 1     1   6763  
  1         86977  
  1         203  
6             my ($self, $doc) = @_;
7              
8 1     1 0 3244 my $use_utf8_statements = $doc->find(
9             sub {
10             my $st = $_[1];
11             $st->isa('PPI::Statement::Include') && $st->schild(0) eq 'use' && $st->schild(1) eq 'utf8';
12 23     23   363 }
13 23 100 66     79 );
14             return $doc unless $use_utf8_statements;
15 1         9
16 1 50       14 my $chars_outside_ascii_range = 0;
17             for (my $tok = $doc->first_token; $tok && $chars_outside_ascii_range == 0; $tok = $tok->next_token) {
18 1         3 next unless $tok->significant;
19 1   66     6 my $src = $tok->content;
20 19 100       575 utf8::decode($src);
21 12         20  
22 12         45 my $len = length($src);
23             for (my $i = 0; $i < $len && $chars_outside_ascii_range == 0; $i++) {
24 12         14 if (ord(substr($src, $i, 1)) > 127) {
25 12   66     35 $chars_outside_ascii_range++;
26 34 50       104 }
27 0         0 }
28             }
29              
30             unless ($chars_outside_ascii_range) {
31             $_->remove for @$use_utf8_statements;
32 1 50       49 }
33 1         8  
34             return $doc;
35             }
36 1         66  
37             1;