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             package App::PerlNitpick::Rule::RemoveEffectlessUTF8Pragma;
2             # ABSTRACT: Re-quote strings with single quotes ('') if they look "simple"
3              
4 1     1   234103 use Moose;
  1         489099  
  1         8  
5 1     1   8431 use PPI::Document;
  1         115375  
  1         231  
6              
7             sub rewrite {
8 1     1 0 4085 my ($self, $doc) = @_;
9              
10             my $use_utf8_statements = $doc->find(
11             sub {
12 23     23   398 my $st = $_[1];
13 23 100 66     95 $st->isa('PPI::Statement::Include') && $st->schild(0) eq 'use' && $st->schild(1) eq 'utf8';
14             }
15 1         24 );
16 1 50       17 return $doc unless $use_utf8_statements;
17            
18 1         3 my $chars_outside_ascii_range = 0;
19 1   66     17 for (my $tok = $doc->first_token; $tok && $chars_outside_ascii_range == 0; $tok = $tok->next_token) {
20 19 100       713 next unless $tok->significant;
21 12         28 my $src = $tok->content;
22 12         57 utf8::decode($src);
23              
24 12         19 my $len = length($src);
25 12   66     44 for (my $i = 0; $i < $len && $chars_outside_ascii_range == 0; $i++) {
26 34 50       126 if (ord(substr($src, $i, 1)) > 127) {
27 0         0 $chars_outside_ascii_range++;
28             }
29             }
30             }
31              
32 1 50       50 unless ($chars_outside_ascii_range) {
33 1         20 $_->remove for @$use_utf8_statements;
34             }
35              
36 1         89 return $doc;
37             }
38              
39             1;