File Coverage

blib/lib/App/PerlNitpick/Rule/RemoveEffectlessUTF8Pragma.pm
Criterion Covered Total %
statement 21 22 95.4
branch 7 10 70.0
condition 6 9 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 38 46 82.6


line stmt bran cond sub pod time code
1             package App::PerlNitpick::Rule::RemoveEffectlessUTF8Pragma;
2             # ABSTRACT: Remove `use utf8;` if all characters in the source file file are in ASCII.
3              
4 1     1   237797 use Moose;
  1         480846  
  1         8  
5 1     1   9187 use PPI::Document;
  1         117357  
  1         260  
6              
7             sub rewrite {
8 1     1 0 10657 my ($self, $doc) = @_;
9              
10             my $use_utf8_statements = $doc->find(
11             sub {
12 23     23   406 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 50       26 ) or return $doc;
16              
17 1         18 my $chars_outside_ascii_range = 0;
18 1   66     9 for (my $tok = $doc->first_token; $tok && $chars_outside_ascii_range == 0; $tok = $tok->next_token) {
19 19 100       783 next unless $tok->significant;
20 12         33 my $src = $tok->content;
21 12         56 utf8::decode($src);
22              
23 12         20 my $len = length($src);
24 12   66     41 for (my $i = 0; $i < $len && $chars_outside_ascii_range == 0; $i++) {
25 34 50       160 if (ord(substr($src, $i, 1)) > 127) {
26 0         0 $chars_outside_ascii_range++;
27             }
28             }
29             }
30              
31 1 50       57 unless ($chars_outside_ascii_range) {
32 1         20 $_->remove for @$use_utf8_statements;
33             }
34              
35 1         87 return $doc;
36             }
37              
38             1;