File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitUnnecessaryUTF8Pragma.pm
Criterion Covered Total %
statement 12 32 37.5
branch 0 12 0.0
condition 0 3 0.0
subroutine 4 8 50.0
pod 3 3 100.0
total 19 58 32.7


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TooMuchCode::ProhibitUnnecessaryUTF8Pragma;
2             # ABSTRACT: "use utf8" is probably not needed if all characters in the source code are in 7bit ASCII range.
3              
4 5     5   3248 use strict;
  5         16  
  5         212  
5 5     5   28 use warnings;
  5         1168  
  5         148  
6 5     5   35 use Perl::Critic::Utils;
  5         49  
  5         83  
7 5     5   4553 use parent 'Perl::Critic::Policy';
  5         17  
  5         53  
8              
9 0     0 1   sub default_themes { return qw( bugs maintenance ) }
10 0     0 1   sub applies_to { return 'PPI::Document' }
11              
12             #---------------------------------------------------------------------------
13              
14             sub violates {
15 0     0 1   my ( $self, $elem, $doc ) = @_;
16              
17             my $use_utf8_statements = $elem->find(
18             sub {
19 0     0     my $st = $_[1];
20 0 0 0       $st->isa('PPI::Statement::Include') && $st->schild(0) eq 'use' && $st->schild(1) eq 'utf8';
21             }
22 0           );
23 0 0         return unless $use_utf8_statements;
24              
25 0           my $chars_outside_ascii_range = 0;
26 0           for (my $tok = $elem->first_token; $tok; $tok = $tok->next_token) {
27 0 0         next unless $tok->significant;
28 0           my $src = $tok->content;
29 0           utf8::decode($src);
30              
31 0           my @c = split /\s+/, $src;
32 0           for (my $i = 0; $i < @c; $i++) {
33 0 0         if (ord($c[$i]) > 127) {
34 0           $chars_outside_ascii_range++;
35             }
36             }
37 0 0         last if $chars_outside_ascii_range;
38             }
39              
40 0 0         unless ($chars_outside_ascii_range) {
41 0           return $self->violation(
42             "'use utf8;' seems to be unnecessary",
43             'All characters in the source code are within ASCII range.',
44             $use_utf8_statements->[0],
45             );
46             }
47 0           return;
48             }
49              
50             1;
51              
52             =encoding utf-8
53              
54             =head1 NAME
55              
56             TooMuchCode::ProhibitUnusedImport -- Find 'use utf8' statement that produces (almost) no effect.
57              
58             =head1 DESCRIPTION
59              
60             The utf8 pragma is used to declare that the source code itself can be decoded by utf-8 encoding rule
61             as a sequence of characters. What this means is that all the characters in the code are within the
62             ASCII range.
63              
64             =cut