blib/lib/Perl/Critic/Policy/Modules/RequireExplicitPackage/ObjectPad.pm

filter: Statementstmt 89.1% 33 / 37 Branchbran 90.0% 9 / 10 Conditioncond 75.0% 12 / 16 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 81.0% 64 / 79 CC 12 SCAR 13.6
CC
12
Cov
86%
__ANON__
6.4
_replace_class
4.0
__ANON__
4.0
CRAP 3.9
↑ Summary
1 package Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw(Perl::Critic::Policy::Modules::RequireExplicitPackage);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = "0.001";
7  
8 =head1 NAME
9  
10 Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad - Always make the package/Object::Pad class explicit.
11  
12 =cut
13  
14 =head1 METHOLDS
15  
16 =head2 violates
17  
18 Please see L<Perl::Critic::Policy::Modules::RequireExplicitPackage::violates>
19  
20 =cut
21  
22 # PODNAME: Perl::Critic::Policy::Modules::RequireExplicitPackage
23  
24 0 ▶around violates => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 my $orig = shift;
26 0 my ($self, $elem, $doc) = @_;
27 0 $doc = _replace_class($doc);
28 0 return $orig->($self, $elem, $doc);
29 2 };
30  
31 =head2 _replace_class
32  
33 replace 'use Object::Pad' and 'class XXXX' with `package XXX` in L<PPI::Document> object, to make it be processable by L<Perl::Critic::Policy::Modules::RequireExplicitPackage::violates>
34  
35 Argument: PPI::Document object
36 Return: cloned PPI::document object
37  
38 =cut
39  
40 7 ▶sub _replace_class {
Subroutine _replace_class: called CC=4 SCAR=13.9
41 7 my $doc = shift;
42 7 my $cloned_doc = $doc->clone();
43 my $object_pad = $cloned_doc->find_first(
44 33 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
45 33 ▶ $_[1]->parent == $_[0]
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Include") and ($_[1]->type // "") eq "use" and ($_[1]->module // "") eq "Object::Pad" 6 27 33
Condition70%7/10
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Include") and ($_[1]->type // "") eq "use"
!ll&&!rl&&r
2706
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Include")
!ll&&!rl&&r
13146
$_[1]->type // ""
l!l
60
$_[1]->module // ""
l!l
60
MC/DC0%0/3
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Include") and ($_[1]->type // "") eq "use"
$_[1]->parent == $_[0] $_[1]->isa("PPI::Statement::Include") $_[1]->type
MC/DC0%0/1
$_[1]->module // ""
$_[1]->module
Truth table0%0/4
A and B and C
A= $_[1]->parent == $_[0]B= $_[1]->isa("PPI::Statement::Include")C= $_[1]->type
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $_[1]->module
Aresult
11
00
46 and $_[1]->isa('PPI::Statement::Include')
47 and ($_[1]->type // '') eq 'use'
48 and ($_[1]->module // '') eq 'Object::Pad';
49 7 });
50 7 ▶ return $cloned_doc unless $object_pad;
Branch: 1 branch
BranchTrueFalseTotal
unless $object_pad 2 5 7
51 my $class = $cloned_doc->find_first(
52 45 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
53 45 ▶ $_[1]->parent == $_[0]
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement") and $_[1]->child(0)->isa("PPI::Token::Word") and $_[1]->child(0)->literal eq "class" 9 36 45
Condition83%5/6
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement") and $_[1]->child(0)->isa("PPI::Token::Word")
!ll&&!rl&&r
3609
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement")
!ll&&!rl&&r
20169
MC/DC66%2/3
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement") and $_[1]->child(0)->isa("PPI::Token::Word")
$_[1]->parent == $_[0] $_[1]->isa("PPI::Statement") $_[1]->child(0)->isa("PPI::Token::Word")
Truth table75%3/4
A and B and C
A= $_[1]->parent == $_[0]B= $_[1]->isa("PPI::Statement")C= $_[1]->child(0)->isa("PPI::Token::Word")
ABCresult
0XX0
10X0
1100
1111
54 and $_[1]->isa('PPI::Statement')
55 and $_[1]->child(0)->isa('PPI::Token::Word')
56 and $_[1]->child(0)->literal eq 'class';
57 5 });
58 5 ▶ return $cloned_doc unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 1 4 5
59 4 ▶ return $cloned_doc unless "$class" =~ /class\s+(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless "$class" =~ /class\s+(\w+)/ 0 4 4
60 4 my $class_name = $1;
61 4 $cloned_doc->remove_child($object_pad);
62 4 my $package_code = "package $class_name;";
63 4 my $package_doc = PPI::Document->new(\$package_code);
64 4 ▶ my $package_statement = $package_doc->find_first(sub { $_[1]->isa('PPI::Statement::Package') });
Subroutine __ANON__: called CC=1 SCAR=0.0
65 4 $package_doc->remove_child($package_statement);
66 4 $class->insert_before($package_statement);
67 4 return $cloned_doc;
68 }
69 2 1;
70  
↑ Summary