Policy/Community/Prototypes.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 87.5% 14 / 16 Conditioncond 77.2% 17 / 22 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 86.6% 97 / 112 CC 19 SCAR 25.6
CC
19
Cov
90%
violates
19.8
default_themes
2.0
BEGIN
1.0
CRAP 12.9
« Policy/Community/WhileDiamondDefaultAssignment.pm ↑ Summary Community/Utils.pm »
1 package Perl::Critic::Policy::Community::Prototypes;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = 'v1.0.4';
11  
12 1 ▶use constant DESC => 'Using function prototypes';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant EXPL => 'Function prototypes (sub foo ($@) { ... }) will usually not do what you want. Omit the prototype, or use signatures instead.';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
16 (
17 {
18 10 name => 'signature_enablers',
19 description => 'Non-standard modules to recognize as enabling signatures',
20 behavior => 'string list',
21 },
22 )
23 }
24  
25 1 ▶sub default_severity { $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
26 0 ▶sub default_themes { 'community' }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
27 10 ▶sub applies_to { 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
28  
29 10 ▶sub violates {
Subroutine violates: called CC=19 SCAR=29.8
Pod documented
30 10 my ($self, $elem) = @_;
31  
32 # Check if signatures are enabled
33 10 ▶ my $includes = $elem->find('PPI::Statement::Include') || [];
Condition100%2/2
$elem->find('PPI::Statement::Include') || []
l!l
64
MC/DC100%1/1
$elem->find('PPI::Statement::Include') || []
$elem->find('PPI::Statement::Include')
Truth table100%2/2
A
A= $elem->find('PPI::Statement::Include')
Aresult
11
00
34 10 foreach my $include (@$includes) {
35 6 ▶ next unless $include->type eq 'use';
Branch: 1 branch
BranchTrueFalseTotal
unless $include->type eq "use" 0 6 6
36 6 ▶ return () if $include->version and version->parse($include->version) >= version->parse('v5.36');
Branch: 1 branch
BranchTrueFalseTotal
if $include->version and "version"->parse($include->version) >= "version"->parse("v5.36") 1 5 6
Condition66%2/3
$include->version and "version"->parse($include->version) >= "version"->parse("v5.36")
!ll&&!rl&&r
501
MC/DC50%1/2
$include->version and "version"->parse($include->version) >= "version"->parse("v5.36")
$include->version "version"->parse($include->version) >= "version"->parse("v5.36")
Truth table66%2/3
A and B
A= $include->versionB= "version"->parse($include->version) >= "version"->parse("v5.36")
ABresult
0X0
100
111
37 5 ▶ return () if $include->pragma eq 'feature' and $include =~ m/\bsignatures\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $include->pragma eq "feature" and $include =~ /\bsignatures\b/ 1 4 5
Condition66%2/3
$include->pragma eq "feature" and $include =~ /\bsignatures\b/
!ll&&!rl&&r
401
MC/DC50%1/2
$include->pragma eq "feature" and $include =~ /\bsignatures\b/
$include->pragma eq "feature" $include =~ /\bsignatures\b/
Truth table66%2/3
A and B
A= $include->pragma eq "feature"B= $include =~ /\bsignatures\b/
ABresult
0X0
100
111
38 4 ▶ return () if $include->pragma eq 'experimental' and $include =~ m/\bsignatures\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $include->pragma eq "experimental" and $include =~ /\bsignatures\b/ 1 3 4
Condition66%2/3
$include->pragma eq "experimental" and $include =~ /\bsignatures\b/
!ll&&!rl&&r
301
MC/DC50%1/2
$include->pragma eq "experimental" and $include =~ /\bsignatures\b/
$include->pragma eq "experimental" $include =~ /\bsignatures\b/
Truth table66%2/3
A and B
A= $include->pragma eq "experimental"B= $include =~ /\bsignatures\b/
ABresult
0X0
100
111
39 3 ▶ return () if $include->module eq 'Mojo::Base' and $include =~ m/-signatures\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $include->module eq "Mojo::Base" and $include =~ /-signatures\b/ 1 2 3
Condition66%2/3
$include->module eq "Mojo::Base" and $include =~ /-signatures\b/
!ll&&!rl&&r
201
MC/DC50%1/2
$include->module eq "Mojo::Base" and $include =~ /-signatures\b/
$include->module eq "Mojo::Base" $include =~ /-signatures\b/
Truth table66%2/3
A and B
A= $include->module eq "Mojo::Base"B= $include =~ /-signatures\b/
ABresult
0X0
100
111
40 2 ▶ return () if $include->module eq 'Mojolicious::Lite' and $include =~ m/-signatures\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $include->module eq "Mojolicious::Lite" and $include =~ /-signatures\b/ 1 1 2
Condition66%2/3
$include->module eq "Mojolicious::Lite" and $include =~ /-signatures\b/
!ll&&!rl&&r
101
MC/DC50%1/2
$include->module eq "Mojolicious::Lite" and $include =~ /-signatures\b/
$include->module eq "Mojolicious::Lite" $include =~ /-signatures\b/
Truth table66%2/3
A and B
A= $include->module eq "Mojolicious::Lite"B= $include =~ /-signatures\b/
ABresult
0X0
100
111
41 1 ▶ return () if exists $self->{_signature_enablers}{$include->module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_signature_enablers"}{$include->module} 1 0 1
42 }
43
44 4 ▶ my $prototypes = $elem->find('PPI::Token::Prototype') || [];
Condition100%2/2
$elem->find('PPI::Token::Prototype') || []
l!l
31
MC/DC100%1/1
$elem->find('PPI::Token::Prototype') || []
$elem->find('PPI::Token::Prototype')
Truth table100%2/2
A
A= $elem->find('PPI::Token::Prototype')
Aresult
11
00
45 4 my @violations;
46 4 foreach my $prototype (@$prototypes) {
47 # Empty prototypes and prototypes containing & can be useful
48 3 ▶ next if $prototype->prototype eq '' or $prototype->prototype =~ /&/;
Branch: 1 branch
BranchTrueFalseTotal
if $prototype->prototype eq "" or $prototype->prototype =~ /&/ 2 1 3
Condition100%3/3
$prototype->prototype eq "" or $prototype->prototype =~ /&/
l!l&&r!l&&!r
111
MC/DC100%2/2
$prototype->prototype eq "" or $prototype->prototype =~ /&/
$prototype->prototype eq "" $prototype->prototype =~ /&/
Truth table100%3/3
A or B
A= $prototype->prototype eq ""B= $prototype->prototype =~ /&/
ABresult
1X1
011
000
49 1 push @violations, $self->violation(DESC, EXPL, $prototype);
50 }
51
52 4 return @violations;
53 }
54  
55 1 1;
56  
57 =head1 NAME
58  
59 Perl::Critic::Policy::Community::Prototypes - Don't use function prototypes
60  
61 =head1 DESCRIPTION
62  
63 Function prototypes are primarily a hint to the Perl parser for parsing the
64 function's argument list. They are not a way to validate or count the arguments
65 passed to the function, and will cause confusion if used this way. Often, the
66 prototype can simply be left out, but see L<perlsub/"Signatures"> for a more
67 modern method of declaring arguments.
68  
69 sub foo ($$) { ... } # not ok
70 sub foo { ... } # ok
71 use feature 'signatures'; sub foo ($bar, $baz) { ... } # ok
72 use experimental 'signatures'; sub foo ($bar, $baz) { ... } # ok
73  
74 This policy is similar to the core policy
75 L<Perl::Critic::Policy::Subroutines::ProhibitSubroutinePrototypes>, but
76 additionally ignores files using the C<signatures> feature (which is also
77 enabled by a C<use> declaration of perl version 5.36 or higher), and allows
78 empty prototypes and prototypes containing C<&>, as these are often useful for
79 structural behavior.
80  
81 =head1 AFFILIATION
82  
83 This policy is part of L<Perl::Critic::Community>.
84  
85 =head1 CONFIGURATION
86  
87 This policy can be configured to recognize additional modules as enabling the
88 C<signatures> feature, by putting an entry in a C<.perlcriticrc> file like
89 this:
90  
91 [Community::Prototypes]
92 signature_enablers = MyApp::Base
93  
94 =head1 AUTHOR
95  
96 Dan Book, C<dbook@cpan.org>
97  
98 =head1 COPYRIGHT AND LICENSE
99  
100 Copyright 2015, Dan Book.
101  
102 This library is free software; you may redistribute it and/or modify it under
103 the terms of the Artistic License version 2.0.
104  
105 =head1 SEE ALSO
106  
107 L<Perl::Critic>
« Policy/Community/WhileDiamondDefaultAssignment.pm ↑ Summary Community/Utils.pm »