blib/lib/WWW/Shopify/Liquid/Tag/Assign.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 39 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 169 CC 28 SCAR 62.3
CC
28
Cov
0%
process
702.0
verify
12.0
min_arguments
2.0
CRAP 508.4
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4  
5 use WWW::Shopify::Liquid::Tag;
6  
7 package WWW::Shopify::Liquid::Tag::Assign;
8 use base 'WWW::Shopify::Liquid::Tag::Free';
9 use Scalar::Util qw(looks_like_number);
10 sub min_arguments { return 1; }
11 sub max_arguments { return 1; }
12 sub verify {
13 my ($self) = @_;
14 die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires assignment operator to be the first thing in an assign tag.") unless
15 $self->{arguments}->[0]->isa('WWW::Shopify::Liquid::Operator::Assignment');
16 die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires variable for what you're assigning to.") unless
17 $self->{arguments}->[0]->{operands}->[0]->isa('WWW::Shopify::Liquid::Token::Variable');
18 }
19 sub process {
20 my ($self, $hash, $action, $pipeline) = @_;
21 my @vars = map { $self->is_processed($_) ? $_ : $_->$action($pipeline, $hash) } @{$self->{arguments}->[0]->{operands}->[0]->{core}};
22 return $self if $action eq "optimize" && int(grep { !$self->is_processed($_) } @vars) > 0;
23
24
25 my $inner_hash = $hash;
26 for (0..$#vars-1) {
27 return $self if ref($inner_hash) && ref($inner_hash) eq "HASH" && !exists $inner_hash->{$vars[$_]} && $action eq 'optimize';
28 if (looks_like_number($vars[$_]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") {
29 $inner_hash->[$vars[$_]] = {} if !defined $inner_hash->[$vars[$_]];
30 $inner_hash = $inner_hash->[$vars[$_]];
31 } else {
32 $inner_hash->{$vars[$_]} = {} if !exists $inner_hash->{$vars[$_]};
33 $inner_hash = $inner_hash->{$vars[$_]};
34 }
35 }
36
37
38 my $result = $self->{arguments}->[0]->{operands}->[1];
39 # For now, only do renders.
40 if ($action eq "optimize") {
41 if (exists $inner_hash->{$vars[-1]}) {
42 my $value = delete $inner_hash->{$vars[-1]};
43 $result = $result->$action($pipeline, $hash) if !$self->is_processed($result);
44 $inner_hash->{$vars[-1]} = $result if $self->is_processed($result);
45 } else {
46 $result = $result->$action($pipeline, $hash) if !$self->is_processed($result);
47 }
48 $self->{arguments}->[0]->{operands}->[1] = $result if $self->is_processed($result);
49 # If we run across something that should be assigned, we must delete it in the hash to preserve uncertainty.
50 # OK, no. We still return ourselves, but we do a bit of a deeper analysis. If the assignment is out the in the open, we assign.
51 # If the assignment is contingent upon a conditional, (i.e. inside something that isn't fully resolved). Then we delete it.
52 $pipeline->flag_conditional_uncertainty(\@vars);
53 $inner_hash->{$vars[-1]} = $result if $self->is_processed($result);
54 return undef if $pipeline->remove_assignment && !defined $pipeline->conditional_state;
55 return $self;
56 } else {
57 $result = $result->$action($pipeline, $hash) if !$self->is_processed($result);
58 }
59 return $self unless $self->is_processed($result);
60
61 my $assignment = $self->{arguments}->[0];
62 if (looks_like_number($vars[-1]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") {
63 if ($assignment->isa('WWW::Shopify::Liquid::Operator::PlusAssignment')) {
64 $inner_hash->[$vars[-1]] += ($result || 0);
65 } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MinusAssignment')) {
66 $inner_hash->[$vars[-1]] -= ($result || 0);
67 } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MultiplyAssignment')) {
68 $inner_hash->[$vars[-1]] *= ($result || 0);
69 } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::DivideAssignment')) {
70 $inner_hash->[$vars[-1]] /= $result;
71 } else {
72 $inner_hash->[$vars[-1]] = $result;
73 }
74 } else {
75 if ($assignment->isa('WWW::Shopify::Liquid::Operator::PlusAssignment')) {
76 $inner_hash->{$vars[-1]} += ($result || 0);
77 } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MinusAssignment')) {
78 $inner_hash->{$vars[-1]} -= ($result || 0);
79 } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MultiplyAssignment')) {
80 $inner_hash->{$vars[-1]} *= ($result || 0);
81 } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::DivideAssignment')) {
82 $inner_hash->{$vars[-1]} /= $result;
83 } else {
84 $inner_hash->{$vars[-1]} = $result;
85 }
86 }
87 return '';
88 }
89  
90  
91  
92 1;