| 1 | |||||||||||||||||||||||||||||
| 2 | package IOC::Visitor::SearchForContainer; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 12 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use IOC::Interfaces; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use IOC::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use base 'IOC::Visitor'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 6 | my ($_class, $container_name) = @_; | |||||||||||||||||||||||||||
| 18 | 6 | ▶ | ($container_name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | || throw IOC::InsufficientArguments "You must provide a name of a container to find"; | ||||||||||||||||||||||||||||
| 20 | 5 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||
| 21 | 5 | my $visitor = { | |||||||||||||||||||||||||||
| 22 | container_to_find => $container_name | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | 5 | bless($visitor, $class); | |||||||||||||||||||||||||||
| 25 | 5 | return $visitor; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 8 | my ($self, $container) = @_; | |||||||||||||||||||||||||||
| 30 | 8 | ▶ | (blessed($container) && $container->isa('IOC::Container')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($container) and $container->isa("IOC::Container")
MC/DC100%2/2
builtin::blessed($container) and $container->isa("IOC::Container")
builtin::blessed($container)
$container->isa("IOC::Container")
Truth table100%3/3
A and B
A= builtin::blessed($container)B= $container->isa("IOC::Container")
| |||||||||||||||||||||||||||||
| 31 | || throw IOC::InsufficientArguments "You must provide an IOC::Container object to search"; | ||||||||||||||||||||||||||||
| 32 | 4 | my $container_to_find = $self->{container_to_find}; | |||||||||||||||||||||||||||
| 33 | 4 | return $self->_recursiveSearch($container, $container_to_find); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | sub _recursiveSearch { | ||||||||||||||||||||||||||
|
Subroutine
_recursiveSearch: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 37 | 6 | my ($self, $container, $container_to_find) = @_; | |||||||||||||||||||||||||||
| 38 | # if we have it stored, then return it | ||||||||||||||||||||||||||||
| 39 | 6 | ▶ | return $container->getSubContainer($container_to_find) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | if $container->hasSubContainer($container_to_find); | ||||||||||||||||||||||||||||
| 41 | # otherwise, loop through all the sub-containers | ||||||||||||||||||||||||||||
| 42 | # sort the names too, which will make sure the | ||||||||||||||||||||||||||||
| 43 | # search will roughly take the same amount of time | ||||||||||||||||||||||||||||
| 44 | # each time we do it, otherwise we are at the | ||||||||||||||||||||||||||||
| 45 | # mercy of the hash ordering | ||||||||||||||||||||||||||||
| 46 | 5 | foreach my $sub_container_name (sort $container->getSubContainerList()) { | |||||||||||||||||||||||||||
| 47 | # otherwise we need to search the next level | ||||||||||||||||||||||||||||
| 48 | 8 | my $sub_container = $container->getSubContainer($sub_container_name); | |||||||||||||||||||||||||||
| 49 | 8 | ▶ | $self->_recursiveSearch($sub_container, $container_to_find) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | if $sub_container->hasSubContainers(); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 5 | return undef; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 12 | 1; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | __END__ | ||||||||||||||||||||||||||||