blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitLvalueSubstr.pm

filter: Statementstmt 81.4% 35 / 43 Branchbran 8.3% 1 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 83.3% 5 / 6 totaltotal 69.6% 55 / 79 CC 8 SCAR 26.6
CC
8
Cov
64%
violates
38.6
prepare_to_scan_document
2.1
BEGIN
1.0
CRAP 14.3
« blib/lib/Perl/Critic/Utils.pm ↑ Summary blib/lib/Perl/Critic/Policy/Documentation/RequirePodSections.pm »
1 package Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use version 0.77 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw{ :severities :classification :language };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Lvalue form of "substr" used};
17 40 Readonly::Scalar my $EXPL => [ 165 ];
18  
19 40 Readonly::Scalar my $ASSIGNMENT_PRECEDENCE => precedence_of( q{=} );
20 40 Readonly::Scalar my $MINIMUM_PERL_VERSION => version->new( 5.005 );
21  
22 #-----------------------------------------------------------------------------
23  
24 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
26 86 ▶sub default_themes { return qw( core maintenance pbp ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
27 31 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
28  
29 #-----------------------------------------------------------------------------
30  
31 sub prepare_to_scan_document {
32 31 ▶ my ( $self, $document ) = @_;
Subroutine prepare_to_scan_document: called CC=2 SCAR=7.6
Pod documented
33 # perl5005delta says that is when the fourth argument to substr()
34 # was introduced, so ... (RT #59112)
35 31 my $version = $document->highest_explicit_perl_version();
36 31 ▶ return ! $version || $version >= $MINIMUM_PERL_VERSION;
Condition33%1/3
!$version || $version >= $MINIMUM_PERL_VERSION
l!l&&r!l&&!r
3100
MC/DC0%0/2
!$version || $version >= $MINIMUM_PERL_VERSION
!$version $version >= $MINIMUM_PERL_VERSION
Truth table33%1/3
not(A) || B
A= !$versionB= $version >= $MINIMUM_PERL_VERSION
ABresult
1X1
011
000
37 }
38  
39 #-----------------------------------------------------------------------------
40  
41 sub violates {
42 332 ▶ my ($self, $elem, undef) = @_;
Subroutine violates: called CC=7 SCAR=36.5
Pod documented
43  
44 332 ▶ return if $elem->content() ne 'substr';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "substr" 332 0 332
45 0 ▶ return if ! is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 0 0 0
46  
47 0 my $sib = $elem;
48 0 ▶ while ($sib = $sib->snext_sibling()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib = $sib->snext_sibling) 0 0 0
49 0 ▶ if ( $sib->isa( 'PPI::Token::Operator' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->isa("PPI::Token::Operator")) 0 0 0
50 0 my $rslt = $ASSIGNMENT_PRECEDENCE <=> precedence_of(
51 $sib->content() );
52 0 ▶ return if $rslt < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rslt < 0 0 0 0
53 0 ▶ return $self->violation( $DESC, $EXPL, $sib ) if $rslt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rslt == 0 0 0 0
54 }
55 }
56 0 return; #ok!
57 }
58  
59 40 1;
60  
61 #-----------------------------------------------------------------------------
62  
63 __END__
64  
65 =pod
66  
67 =for stopwords perlfunc substr 4th
68  
69 =head1 NAME
70  
71 Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr - Use 4-argument C<substr> instead of writing C<substr($foo, 2, 6) = $bar>.
72  
73  
74 =head1 AFFILIATION
75  
76 This Policy is part of the core L<Perl::Critic|Perl::Critic>
77 distribution.
78  
79  
80 =head1 DESCRIPTION
81  
82 Conway discourages the use of C<substr()> as an lvalue, instead
83 recommending that the 4-argument version of C<substr()> be used
84 instead.
85  
86 substr($something, 1, 2) = $newvalue; # not ok
87 substr($something, 1, 2, $newvalue); # ok
88  
89 The four-argument form of C<substr()> was introduced in Perl 5.005.
90 This policy does not report violations on code which explicitly
91 specifies an earlier version of Perl (e.g. C<use 5.004;>).
92  
93 =head1 CONFIGURATION
94  
95 This Policy is not configurable except for the standard options.
96  
97  
98 =head1 SEE ALSO
99  
100 L<"substr" in perlfunc|perlfunc/substr> (or C<perldoc -f substr>).
101  
102 L<"4th argument to substr" in perl5005delta|perl5005delta/4th argument to substr>
103  
104  
105 =head1 AUTHOR
106  
107 Graham TerMarsch <graham@howlingfrog.com>
108  
109  
110 =head1 COPYRIGHT
111  
112 Copyright (c) 2005-2011 Graham TerMarsch. All rights reserved.
113  
114 This program is free software; you can redistribute it and/or modify
115 it under the same terms as Perl itself.
116  
117 =cut
118  
119 # Local Variables:
120 # mode: cperl
121 # cperl-indent-level: 4
122 # fill-column: 78
123 # indent-tabs-mode: nil
124 # c-indentation-style: bsd
125 # End:
126 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Utils.pm ↑ Summary blib/lib/Perl/Critic/Policy/Documentation/RequirePodSections.pm »