File Coverage

blib/lib/Test2/Tools/PerlCritic/Util.pm
Criterion Covered Total %
statement 54 54 100.0
branch 7 10 70.0
condition 6 11 54.5
subroutine 11 11 100.0
pod 1 1 100.0
total 79 87 90.8


line stmt bran cond sub pod time code
1             package Test2::Tools::PerlCritic::Util;
2              
3 1     1   2438971 use strict;
  1         3  
  1         42  
4 1     1   7 use warnings;
  1         2  
  1         50  
5 1     1   21 use 5.020;
  1         4  
6 1     1   739 use experimental qw( signatures postderef );
  1         2379  
  1         31  
7 1     1   931 use Ref::Util qw( is_blessed_ref );
  1         3543  
  1         106  
8 1     1   10 use Carp qw( croak );
  1         2  
  1         70  
9 1     1   8 use Exporter qw( import );
  1         3  
  1         68  
10 1     1   9 use Data::Dumper ();
  1         2  
  1         32  
11 1     1   6 use Digest::MD5 qw( md5_hex );
  1         2  
  1         88  
12 1     1   7 use Perl::Critic;
  1         3  
  1         983  
13              
14             our @EXPORT_OK = qw( perl_critic_config_id );
15              
16             # ABSTRACT: Utility functions
17             our $VERSION = '0.08'; # VERSION
18              
19              
20             sub perl_critic_config_id ($config=undef)
21 9     9 1 5337158 {
  9         23  
  9         18  
22 9         19   my @policies = sort { $a->get_short_name cmp $b->get_short_name } do {
  63         1592  
23 9   66     41     $config //= Perl::Critic->new;
24 9 100 66     675663     $config = $config->config if is_blessed_ref($config) and $config->isa('Perl::Critic');
25 9 50 33     107     croak "Argument must be a Perl::Critic or Perl::Critic::Config"
26                   unless is_blessed_ref($config) and $config->isa('Perl::Critic::Config');
27 9         30     $config->policies;
28               };
29              
30 9         329   my %config = (
31                 perl_critic_version => Perl::Critic->VERSION,
32                 test2_tools_perl_critic_version => __PACKAEG__->VERSION,
33                 policies => {},
34               );
35              
36 9         41   foreach my $policy (@policies)
37               {
38 71 50       509     next unless $policy->is_enabled;
39 71         523     my $name = $policy->get_short_name;
40              
41 71         1063     my $severity = $policy->get_severity;
42 71         835     my $maximum_violations_per_document = $policy->get_maximum_violations_per_document;
43              
44             # we are assuming that we aren't using the same policy twice
45 71   50     1336     my $policy_config = $config{policies}->{$policy->get_short_name} = {
46                   version => $policy->VERSION // '',
47                   parameters => {},
48                 };
49              
50 71 50       1282     $policy_config->{severity} = $severity if defined $severity;
51 71 100       178     $maximum_violations_per_document = $maximum_violations_per_document if defined $maximum_violations_per_document;
52              
53 71         252     foreach my $parameter ($policy->get_parameters->@*)
54                 {
55 29         254       my $name = $parameter->get_name;
56             # NOTE: this is private data to the policy, but
57             # the convential way to store a parameter seems
58             # to be with _$name
59 29         223       my $value = $policy->{"_$name"};
60 29         139       $policy_config->{parameters}->{$name} = $value;
61                 }
62               }
63              
64 9         125   my $dumper = Data::Dumper
65                 ->new([\%config], ['config'])
66                 ->Sortkeys(1)
67                 ->Indent(1);
68              
69 9         693   my $dump = $dumper->Dump;
70              
71 9         4731   return md5_hex($dump);
72              
73             }
74              
75             1;
76              
77             __END__
78            
79             =pod
80            
81             =encoding UTF-8
82            
83             =head1 NAME
84            
85             Test2::Tools::PerlCritic::Util - Utility functions
86            
87             =head1 VERSION
88            
89             version 0.08
90            
91             =head1 SYNOPSIS
92            
93             use Test2::Tools::PerlCritic::Util qw( perl_critic_config_id );
94            
95             =head1 DESCRIPTION
96            
97             This module provides some utility functions useful when working with L<Perl::Critic> testing.
98            
99             =head1 FUNCTIONS
100            
101             =head2 perl_critic_config_id
102            
103             my $id = perl_critic_config_id $config;
104             my $id = perl_critic_config_id;
105            
106             Computes an id of the L<Perl::Critic> configuration. The argument C<$config>
107             should be either an instance of L<Perl::Critic> or L<Perl::Critic::Config>.
108             If not provided then a default L<Perl::Critic::Config> will be created.
109            
110             CAVEAT: This isn't really possible with 100% accuracy with the L<Perl::Critic>
111             API, so we make some assumptions common conventions that typically do hold
112             in virtually all cases.
113            
114             =head1 AUTHOR
115            
116             Graham Ollis <plicease@cpan.org>
117            
118             =head1 COPYRIGHT AND LICENSE
119            
120             This software is copyright (c) 2019-2024 by Graham Ollis.
121            
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124            
125             =cut
126