| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Perl-PrereqScanner-Scanner-MooseXTypesCombine | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2012 by Randy Stauner. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package Perl::PrereqScanner::Scanner::MooseXTypesCombine; | ||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | 2 | $Perl::PrereqScanner::Scanner::MooseXTypesCombine::VERSION = '0.001'; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | # git description: b29ba10 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | $Perl::PrereqScanner::Scanner::MooseXTypesCombine::AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | with 'Perl::PrereqScanner::Scanner'; | |||||||||||||||||||||||||||
| 23 | # ABSTRACT: Scan for type libraries exported with MooseX::Types::Combine | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use List::MoreUtils qw( any ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Params::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | sub scan_for_prereqs { | ||||||||||||||||||||||||||
|
Subroutine
scan_for_prereqs: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 9 | my ($self, $ppi_doc, $req) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # * split doc into chunks by package (very tricky - PPI does not support this yet) | ||||||||||||||||||||||||||||
| 33 | # * for each package: | ||||||||||||||||||||||||||||
| 34 | # * check for base/parent/isa | ||||||||||||||||||||||||||||
| 35 | # * find provide_types_from | ||||||||||||||||||||||||||||
| 36 | # * find quoted words | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 9 | foreach my $chunk ( $ppi_doc ){ | |||||||||||||||||||||||||||
| 39 | 9 | my @prereqs = $self->_determine_inheritance_from_mxtc($chunk); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # short-circuit if it doesn't look like this package isa mxtc | ||||||||||||||||||||||||||||
| 42 | 9 | ▶ | next unless @prereqs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # find the method call that sets the types being exported | ||||||||||||||||||||||||||||
| 45 | 171 | ▶ | my $methods = $chunk->find(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 171 | ▶ | $_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'provide_types_from' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 6 | ▶ | }) || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$chunk->find(sub {
$_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'provide_types_from';
}
) || []
MC/DC0%0/1
$chunk->find(sub {
$_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'provide_types_from';
}
) || []
$chunk->find(sub {
$_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'provide_types_from';
}
)
Truth table50%1/2
A
A= $chunk->find(sub {
$_[1]->isa('PPI::Token::Word') and $_[1]->content eq 'provide_types_from';
}
)
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # parse the statements that contain the method call we just searched for | ||||||||||||||||||||||||||||
| 50 | push @prereqs, $self->_parse_mxtypes_from_statement($_) | ||||||||||||||||||||||||||||
| 51 | 6 | for map { $_->parent } @$methods; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | $req->add_minimum($_ => 0) for @prereqs; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # There should be a 'use base', 'use parent', or '@ISA = ' (or 'push @ISA') | ||||||||||||||||||||||||||||
| 58 | # somewhere that includes MooseX::Types::Combine. | ||||||||||||||||||||||||||||
| 59 | 9 | ▶ | sub _determine_inheritance_from_mxtc { | ||||||||||||||||||||||||||
|
Subroutine
_determine_inheritance_from_mxtc: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 60 | 9 | my ($self, $ppi_doc) = @_; | |||||||||||||||||||||||||||
| 61 | 9 | my @modules; | |||||||||||||||||||||||||||
| 62 | 9 | my $mxtc = 'MooseX::Types::Combine'; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # NOTE: Similar logic is found in some of the scanners; | ||||||||||||||||||||||||||||
| 65 | # perhaps this could be refactored into something reusable. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # find "use base" or "use parent" | ||||||||||||||||||||||||||||
| 68 | 9 | ▶ | my $includes = $ppi_doc->find('Statement::Include') || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ppi_doc->find('Statement::Include') || []
MC/DC100%1/1
$ppi_doc->find('Statement::Include') || []
$ppi_doc->find('Statement::Include')
Truth table100%2/2
A
A= $ppi_doc->find('Statement::Include')
| |||||||||||||||||||||||||||||
| 69 | 9 | for my $node ( @$includes ) { | |||||||||||||||||||||||||||
| 70 | 3 | ▶ | if (grep { $_ eq $node->module } qw{ base parent }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | my @meat = grep { | ||||||||||||||||||||||||||||
| 72 | 3 | ▶ | $_->isa('PPI::Token::QuoteLike::Words') | ||||||||||||||||||||||||||
|
Condition33%1/3
$_->isa('PPI::Token::QuoteLike::Words') || $_->isa('PPI::Token::Quote')
MC/DC0%0/2
$_->isa('PPI::Token::QuoteLike::Words') || $_->isa('PPI::Token::Quote')
$_->isa('PPI::Token::QuoteLike::Words')
$_->isa('PPI::Token::Quote')
Truth table33%1/3
A || B
A= $_->isa('PPI::Token::QuoteLike::Words')B= $_->isa('PPI::Token::Quote')
| |||||||||||||||||||||||||||||
| 73 | || $_->isa('PPI::Token::Quote') | ||||||||||||||||||||||||||||
| 74 | } $node->arguments; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | my @parents = map { $self->_q_contents($_) } @meat; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # the main scanner should pick up base/parent and mxtc | ||||||||||||||||||||||||||||
| 79 | # but it's easy enough to add them here, so do it for completeness | ||||||||||||||||||||||||||||
| 80 | push @modules, $node->module, $mxtc | ||||||||||||||||||||||||||||
| 81 | 3 | ▶ | if any { $_ eq $mxtc } @parents; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 9 | ▶ | return @modules if @modules; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # if there was no base/parent, look for any statement that mentions @ISA | ||||||||||||||||||||||||||||
| 88 | 171 | ▶ | my $isa = $ppi_doc->find(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 89 | 171 | ▶ | $_[1]->isa('PPI::Token::Symbol') && $_[1]->content eq '@ISA' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | }) || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ppi_doc->find(sub {
$_[1]->isa('PPI::Token::Symbol') and $_[1]->content eq '@ISA';
}
) || []
MC/DC100%1/1
$ppi_doc->find(sub {
$_[1]->isa('PPI::Token::Symbol') and $_[1]->content eq '@ISA';
}
) || []
$ppi_doc->find(sub {
$_[1]->isa('PPI::Token::Symbol') and $_[1]->content eq '@ISA';
}
)
Truth table100%2/2
A
A= $ppi_doc->find(sub {
$_[1]->isa('PPI::Token::Symbol') and $_[1]->content eq '@ISA';
}
)
| |||||||||||||||||||||||||||||
| 91 | 6 | $isa = [ map { $_->parent } @$isa ]; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # See if any of those @ISA statements include our module. | ||||||||||||||||||||||||||||
| 94 | # This is far from foolproof, but probably good enough. | ||||||||||||||||||||||||||||
| 95 | @modules = $mxtc | ||||||||||||||||||||||||||||
| 96 | 3 | ▶ | if any { $_->find_any(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 97 | ( | ||||||||||||||||||||||||||||
| 98 | $_[1]->isa('PPI::Token::QuoteLike::Words') || | ||||||||||||||||||||||||||||
| 99 | $_[1]->isa('PPI::Token::Quote') | ||||||||||||||||||||||||||||
| 100 | ) && | ||||||||||||||||||||||||||||
| 101 | 21 | ▶ | any { $_ eq $mxtc } $self->_q_contents($_[1]) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
$_[1]->isa("PPI::Token::QuoteLike::Words") or $_[1]->isa("PPI::Token::Quote")
MC/DC50%1/2
$_[1]->isa("PPI::Token::QuoteLike::Words") or $_[1]->isa("PPI::Token::Quote")
$_[1]->isa("PPI::Token::QuoteLike::Words")
$_[1]->isa("PPI::Token::Quote")
Truth table66%2/3
A or B
A= $_[1]->isa("PPI::Token::QuoteLike::Words")B= $_[1]->isa("PPI::Token::Quote")
| |||||||||||||||||||||||||||||
| 102 | 3 | }); | |||||||||||||||||||||||||||
| 103 | 6 | ▶ | } @$isa; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # take care to always return a list | ||||||||||||||||||||||||||||
| 106 | 6 | return @modules; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 6 | ▶ | sub _parse_mxtypes_from_statement { | ||||||||||||||||||||||||||
|
Subroutine
_parse_mxtypes_from_statement: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||
| 110 | 6 | my ($self, $statement) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # this is naive and very specific but it matches the MXTC synopsis | ||||||||||||||||||||||||||||
| 113 | 6 | my $wanted = [ | |||||||||||||||||||||||||||
| 114 | [ Word => '__PACKAGE__' ], | ||||||||||||||||||||||||||||
| 115 | [ Operator => '->' ], | ||||||||||||||||||||||||||||
| 116 | [ Word => 'provide_types_from' ], | ||||||||||||||||||||||||||||
| 117 | ]; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 6 | my @tokens = $statement->schildren; | |||||||||||||||||||||||||||
| 120 | 6 | my $i = 0; | |||||||||||||||||||||||||||
| 121 | # check that the statement matches $wanted | ||||||||||||||||||||||||||||
| 122 | 6 | foreach my $token ( @tokens ){ | |||||||||||||||||||||||||||
| 123 | 18 | my ($type, $content) = @{ $wanted->[$i++] }; | |||||||||||||||||||||||||||
| 124 | return | ||||||||||||||||||||||||||||
| 125 | 18 | ▶ | unless $token->isa('PPI::Token::' . $type) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$token->isa('PPI::Token::' . $type) and $token->content eq $content
MC/DC0%0/2
$token->isa('PPI::Token::' . $type) and $token->content eq $content
$token->isa('PPI::Token::' . $type)
$token->content eq $content
Truth table33%1/3
A and B
A= $token->isa('PPI::Token::' . $type)B= $token->content eq $content
| |||||||||||||||||||||||||||||
| 126 | and $token->content eq $content; | ||||||||||||||||||||||||||||
| 127 | 18 | ▶ | last if $i == @$wanted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # the list passed to this method is what we are looking for | ||||||||||||||||||||||||||||
| 131 | 6 | my $list = $tokens[$i]; | |||||||||||||||||||||||||||
| 132 | return | ||||||||||||||||||||||||||||
| 133 | 6 | ▶ | unless $list && $list->isa('PPI::Structure::List'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$list and $list->isa("PPI::Structure::List")
MC/DC0%0/2
$list and $list->isa("PPI::Structure::List")
$list
$list->isa("PPI::Structure::List")
Truth table33%1/3
A and B
A= $listB= $list->isa("PPI::Structure::List")
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # this expects quoted module names and won't work if vars are passed | ||||||||||||||||||||||||||||
| 136 | 51 | ▶ | my $words = $list->find(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 137 | 51 | ▶ | $_[1]->isa('PPI::Token::QuoteLike::Words') || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | $_[1]->isa('PPI::Token::Quote') | ||||||||||||||||||||||||||||
| 139 | 6 | ▶ | }) || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$list->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Words') or $_[1]->isa('PPI::Token::Quote');
}
) || []
MC/DC0%0/1
$list->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Words') or $_[1]->isa('PPI::Token::Quote');
}
) || []
$list->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Words') or $_[1]->isa('PPI::Token::Quote');
}
)
Truth table50%1/2
A
A= $list->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Words') or $_[1]->isa('PPI::Token::Quote');
}
)
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | return | ||||||||||||||||||||||||||||
| 142 | 12 | grep { Params::Util::_CLASS($_) } | |||||||||||||||||||||||||||
| 143 | 6 | map { $self->_q_contents($_) } | |||||||||||||||||||||||||||
| 144 | @$words; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||