| 1 | package Aspect::Pointcut::And; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 21 | ▶ | use Aspect::Pointcut::Logic (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 21 | our $VERSION = '0.97_06'; | |||||||||||||
| 8 | 21 | our @ISA = 'Aspect::Pointcut::Logic'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | |||||||||||||||
| 14 | ###################################################################### | ||||||||||||||
| 15 | # Constructor | ||||||||||||||
| 16 | |||||||||||||||
| 17 | sub new { | ||||||||||||||
| 18 | 97 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 19 | 97 | my @parts = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | # Validate the pointcut subexpressions | ||||||||||||||
| 22 | 97 | foreach my $part ( @parts ) { | |||||||||||||
| 23 | 195 | ▶ | next if Params::Util::_INSTANCE($part, 'Aspect::Pointcut'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 0 | Carp::croak("Attempted to apply pointcut logic to non-pointcut '$part'"); | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | # Collapse nested and statements at constructor time so we don't have | ||||||||||||||
| 28 | # to do so multiple times later on during currying. | ||||||||||||||
| 29 | 97 | ▶ | while ( scalar grep { $_->isa('Aspect::Pointcut::And') } @parts ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | @parts = map { | ||||||||||||||
| 31 | 14 | ▶ | $_->isa('Aspect::Pointcut::And') ? @$_ : $_ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | } @parts; | ||||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 97 | $class->SUPER::new(@parts); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | |||||||||||||||
| 39 | |||||||||||||||
| 40 | |||||||||||||||
| 41 | |||||||||||||||
| 42 | ###################################################################### | ||||||||||||||
| 43 | # Weaving Methods | ||||||||||||||
| 44 | |||||||||||||||
| 45 | sub compile_weave { | ||||||||||||||
| 46 | 65 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
compile_weave: called CC=9 SCAR=28.2
Pod
documented
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | # Handle special cases | ||||||||||||||
| 49 | my @children = grep { | ||||||||||||||
| 50 | 138 | ▶ | ref $_ or $_ ne 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | } map { | ||||||||||||||
| 52 | 65 | $_->compile_weave | |||||||||||||
| 53 | } @$self; | ||||||||||||||
| 54 | 65 | ▶ | unless ( @children ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | # Potential bug, but why would we legitimately be empty | ||||||||||||||
| 56 | 0 | return 1; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | 65 | ▶ | if ( @children == 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 61 | return $children[0]; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | # Collapse string conditions together, | ||||||||||||||
| 63 | # and further collapse code conditions together. | ||||||||||||||
| 64 | 4 | my @string = (); | |||||||||||||
| 65 | 4 | my @code = (); | |||||||||||||
| 66 | 4 | foreach my $child ( @children ) { | |||||||||||||
| 67 | 8 | ▶ | unless ( ref $child ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 8 | push @string, $child; | |||||||||||||
| 69 | 8 | next; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | 0 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | my $group = join ' and ', map { "( $_ )" } @string; | |||||||||||||
| 73 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 74 | 0 | @string = (); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | 0 | push @code, $child; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 4 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 4 | my $group = join ' and ', map { "( $_ )" } @string; | |||||||||||||
| 81 | 4 | ▶ | unless ( @code ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | # This is the only thing we have | ||||||||||||||
| 83 | 4 | return $group; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | # Join the groups | ||||||||||||||
| 89 | return sub { | ||||||||||||||
| 90 | 0 | ▶ | foreach my $child ( @code ) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||
| 91 | 0 | ▶ | return 0 unless $child->(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | } | ||||||||||||||
| 93 | 0 | return 1; | |||||||||||||
| 94 | 0 | }; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | sub compile_runtime { | ||||||||||||||
| 98 | 10 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
compile_runtime: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | # Handle special cases | ||||||||||||||
| 101 | my @children = grep { | ||||||||||||||
| 102 | 20 | ▶ | ref $_ or $_ ne 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | } map { | ||||||||||||||
| 104 | 10 | $_->compile_runtime | |||||||||||||
| 105 | } @$self; | ||||||||||||||
| 106 | 10 | ▶ | unless ( @children ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | # Potential bug, but why would we legitimately be empty | ||||||||||||||
| 108 | 0 | return 1; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | 10 | ▶ | if ( @children == 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 0 | return $children[0]; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | # Collapse string conditions together, | ||||||||||||||
| 115 | # and further collapse code conditions together. | ||||||||||||||
| 116 | 10 | my @string = (); | |||||||||||||
| 117 | 10 | my @code = (); | |||||||||||||
| 118 | 10 | foreach my $child ( @children ) { | |||||||||||||
| 119 | 20 | ▶ | unless ( ref $child ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 18 | push @string, $child; | |||||||||||||
| 121 | 18 | next; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | 2 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 2 | my $group = join ' and ', map { "( $_ )" } @string; | |||||||||||||
| 125 | 2 | push @code, eval "sub () { $group }"; | |||||||||||||
| 126 | 2 | @string = (); | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | 2 | push @code, $child; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 10 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 8 | my $group = join ' and ', map { "( $_ )" } @string; | |||||||||||||
| 133 | 8 | ▶ | unless ( @code ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | # This is the only thing we have | ||||||||||||||
| 135 | 8 | return $group; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | # Join the groups | ||||||||||||||
| 141 | return sub { | ||||||||||||||
| 142 | 4 | ▶ | foreach my $child ( @code ) { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 143 | 8 | ▶ | return 0 unless $child->(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | } | ||||||||||||||
| 145 | 2 | return 1; | |||||||||||||
| 146 | 2 | }; | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | sub match_contains { | ||||||||||||||
| 150 | 117 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
match_contains: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 151 | 117 | my $type = shift; | |||||||||||||
| 152 | 117 | ▶ | my $count = $self->isa($type) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 117 | foreach my $child ( @$self ) { | |||||||||||||
| 154 | 242 | $count += $child->match_contains($type); | |||||||||||||
| 155 | } | ||||||||||||||
| 156 | 117 | return $count; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | sub match_runtime { | ||||||||||||||
| 160 | 68 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
match_runtime: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 161 | 68 | foreach my $child ( @$self ) { | |||||||||||||
| 162 | 102 | ▶ | return 1 if $child->match_runtime; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | } | ||||||||||||||
| 164 | 3 | return 0; | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | sub curry_weave { | ||||||||||||||
| 168 | 66 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
curry_weave: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 169 | 66 | my @list = @$self; | |||||||||||||
| 170 | |||||||||||||||
| 171 | # Curry down our children. Anything that is not relevant at weave | ||||||||||||||
| 172 | # time is considered to always match, but curries to null. | ||||||||||||||
| 173 | # In an AND scenario, any "always" match can be savely removed. | ||||||||||||||
| 174 | 66 | @list = grep { defined $_ } map { $_->curry_weave } @list; | |||||||||||||
| 175 | |||||||||||||||
| 176 | # If none are left, curry us away to nothing | ||||||||||||||
| 177 | 66 | ▶ | return unless @list; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | |||||||||||||||
| 179 | # If only one remains, curry us away to just that child | ||||||||||||||
| 180 | 66 | ▶ | return $list[0] if @list == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | |||||||||||||||
| 182 | # Create our clone to hold the curried subset | ||||||||||||||
| 183 | 4 | return ref($self)->new( @list ); | |||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | sub curry_runtime { | ||||||||||||||
| 187 | 68 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
curry_runtime: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 188 | 68 | my @list = @$self; | |||||||||||||
| 189 | |||||||||||||||
| 190 | # Should we strip out the call pointcuts | ||||||||||||||
| 191 | 68 | my $strip = shift; | |||||||||||||
| 192 | 68 | ▶ | unless ( defined $strip ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | # Are there any elements that MUST exist at run-time? | ||||||||||||||
| 194 | 67 | ▶ | if ( $self->match_runtime ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | # If we have any nested logic that themselves contain | ||||||||||||||
| 196 | # call pointcuts, we can't strip. | ||||||||||||||
| 197 | $strip = not scalar grep { | ||||||||||||||
| 198 | 64 | ▶ | $_->isa('Aspect::Pointcut::Logic') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | and | ||||||||||||||
| 200 | $_->match_contains('Aspect::Pointcut::Call') | ||||||||||||||
| 201 | } @list; | ||||||||||||||
| 202 | } else { | ||||||||||||||
| 203 | # Nothing at runtime, so we can strip | ||||||||||||||
| 204 | 3 | $strip = 1; | |||||||||||||
| 205 | } | ||||||||||||||
| 206 | } | ||||||||||||||
| 207 | |||||||||||||||
| 208 | # Curry down our children | ||||||||||||||
| 209 | 77 | @list = grep { defined $_ } map { | |||||||||||||
| 210 | 68 | ▶ | $_->isa('Aspect::Pointcut::Call') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 211 | ? $strip | ||||||||||||||
| 212 | ? $_->curry_runtime($strip) | ||||||||||||||
| 213 | : $_ | ||||||||||||||
| 214 | : $_->curry_runtime($strip) | ||||||||||||||
| 215 | } @list; | ||||||||||||||
| 216 | |||||||||||||||
| 217 | # If none are left, curry us away to nothing | ||||||||||||||
| 218 | 68 | ▶ | return unless @list; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | |||||||||||||||
| 220 | # If only one remains, curry us away to just that child | ||||||||||||||
| 221 | 65 | ▶ | return $list[0] if @list == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 222 | |||||||||||||||
| 223 | # Create our clone to hold the curried subset | ||||||||||||||
| 224 | 11 | return ref($self)->new( @list ); | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | 21 | 1; | |||||||||||||
| 228 | |||||||||||||||
| 229 | __END__ | ||||||||||||||