| 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
Condition70%7/10
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Include") and ($_[1]->type // "") eq "use"
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Include")
$_[1]->type // ""
$_[1]->module // ""
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
Truth table50%1/2
A
A= $_[1]->module
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement") and $_[1]->child(0)->isa("PPI::Token::Word")
$_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | ▶ | return $cloned_doc unless "$class" =~ /class\s+(\w+)/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||