| 1 | package Perl::Analysis::Static::Analysis::Declaration::Variable; | ||||||||||
| 2 | { | ||||||||||
| 3 | 4 | $Perl::Analysis::Static::Analysis::Declaration::Variable::VERSION = '0.004'; # TRIAL | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: find all declarations of variables | ||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use English qw( -no_match_vars ); # Avoids regex performance penalty | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 4 | ▶ | use Perl::Analysis::Static::Element::Declaration::Variable::Lexical; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use Perl::Analysis::Static::Element::Declaration::Variable::Local; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use Perl::Analysis::Static::Element::Declaration::Variable::Package; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | extends 'Perl::Analysis::Static::Analysis'; | |||||||||
| 17 | |||||||||||
| 18 | 4 | has '_ppi_class' => | |||||||||
| 19 | ( is => 'rw', isa => 'Str', default => 'PPI::Statement::Variable' ); | ||||||||||
| 20 | |||||||||||
| 21 | 25 | ▶ | sub _convert { | ||||||||
|
Subroutine
_convert: called CC=4 SCAR=14.0
| |||||||||||
| 22 | 25 | my ( $self, $node ) = @_; | |||||||||
| 23 | |||||||||||
| 24 | # get significant children | ||||||||||
| 25 | 25 | my @schildren = $node->schildren(); | |||||||||
| 26 | |||||||||||
| 27 | # the first child is the keyword, for lexical | ||||||||||
| 28 | # variables this has to be 'my', for package 'our'; | ||||||||||
| 29 | 25 | my $type = _detect_type( $schildren[0] ); | |||||||||
| 30 | |||||||||||
| 31 | # maybe a warning here? | ||||||||||
| 32 | 25 | ▶ | return unless $type; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | |||||||||||
| 34 | 25 | my $class = | |||||||||
| 35 | 'Perl::Analysis::Static::Element::Declaration::Variable::' . $type; | ||||||||||
| 36 | |||||||||||
| 37 | # variable (or list of variables) is the second child | ||||||||||
| 38 | 25 | my $variables = $schildren[1]; | |||||||||
| 39 | |||||||||||
| 40 | # "my $foo"? | ||||||||||
| 41 | 25 | ▶ | if ( $variables->isa('PPI::Token::Symbol') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 20 | my $name = $variables->content; | |||||||||
| 43 | |||||||||||
| 44 | 20 | return $class->new( | |||||||||
| 45 | name => $name, | ||||||||||
| 46 | from => $node->location->[0], | ||||||||||
| 47 | to => $node->location->[0], | ||||||||||
| 48 | ppi_node => $node | ||||||||||
| 49 | ); | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | # no, not "my $foo", so check for "my ($foo, $bar)" | ||||||||||
| 53 | # list of variables is found in a PPI::Structure::List | ||||||||||
| 54 | 5 | @schildren = $node->schildren(); | |||||||||
| 55 | 5 | my $list = $schildren[1]; | |||||||||
| 56 | 5 | ▶ | unless ( $list->isa('PPI::Structure::List') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | App::Perlanalyst::die('PANIC: Expected PPI::Structure::List'); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # variable names are of class PPI::Token::Symbol | ||||||||||
| 61 | 5 | my $symbols = $list->find('PPI::Token::Symbol'); | |||||||||
| 62 | return map { | ||||||||||
| 63 | 5 | $class->new( | |||||||||
| 64 | name => $_->content, | ||||||||||
| 65 | from => $_->location->[0], | ||||||||||
| 66 | to => $_->location->[0], | ||||||||||
| 67 | ppi_node => $node | ||||||||||
| 68 | ); | ||||||||||
| 69 | |||||||||||
| 70 | } @$symbols; | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 25 | ▶ | sub _detect_type { | ||||||||
|
Subroutine
_detect_type: called CC=4 SCAR=14.1
| |||||||||||
| 74 | 25 | my ($keyword) = @_; | |||||||||
| 75 | 25 | ▶ | return 'Lexical' if $keyword eq 'my'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 5 | ▶ | return 'Package' if $keyword eq 'our'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 1 | ▶ | return 'Local' if $keyword eq 'local'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | return; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 4 | 1; | |||||||||
| 84 | |||||||||||
| 85 | __END__ | ||||||||||