| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PHPSA\Compiler\Expression\Operators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpParser\Node\Name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PHPSA\CompiledExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPSA\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPSA\Compiler\Expression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPSA\Compiler\Expression\AbstractExpressionCompiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class PreDec extends AbstractExpressionCompiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $name = 'PhpParser\Node\Expr\PreDec'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * --{expr} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param \PhpParser\Node\Expr\PreDec $expr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param Context $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @return CompiledExpression | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 22 | 8 |  |     protected function compile($expr, Context $context) | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 24 | 8 |  |         if ($expr->var instanceof \PHPParser\Node\Expr\Variable) { | 
            
                                                        
            
                                    
            
            
                | 25 | 8 |  |             $variableName = $expr->var->name; | 
            
                                                        
            
                                    
            
            
                | 26 | 8 |  |             if ($variableName instanceof Name) { | 
            
                                                        
            
                                    
            
            
                | 27 | 8 |  |                 $variableName = $variableName->parts[0]; | 
            
                                                        
            
                                    
            
            
                | 28 | 8 |  |             } | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 | 8 |  |             $variable = $context->getSymbol($variableName); | 
            
                                                        
            
                                    
            
            
                | 31 | 8 |  |             if ($variable) { | 
            
                                                        
            
                                    
            
            
                | 32 | 8 |  |                 $variable->incUse(); | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 | 8 |  |                 switch ($variable->getType()) { | 
            
                                                        
            
                                    
            
            
                | 35 | 8 |  |                     case CompiledExpression::INTEGER: | 
            
                                                        
            
                                    
            
            
                | 36 | 8 |  |                     case CompiledExpression::DOUBLE: | 
            
                                                        
            
                                    
            
            
                | 37 | 8 |  |                     case CompiledExpression::NUMBER: | 
            
                                                        
            
                                    
            
            
                | 38 | 8 |  |                         $variable->dec(); | 
            
                                                        
            
                                    
            
            
                | 39 | 8 |  |                         return CompiledExpression::fromZvalValue($variable->getValue()); | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |                 $context->notice( | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |                     'predec.variable.wrong-type', | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |                     'You are trying to use pre decrement operator on variable $' . $variableName . | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |                     ' with type: ' . $variable->getTypeName(), | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |                     $expr | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             } else { | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                 $context->notice( | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                     'predec.undefined-variable', | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                     'You are trying to use pre decrement operator on undefined variable: ' . $variableName, | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |                     $expr | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |             return new CompiledExpression(CompiledExpression::UNKNOWN); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         $compiledExpression = $context->getExpressionCompiler()->compile($expr->var); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         switch ($compiledExpression->getType()) { | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             case CompiledExpression::INTEGER: | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             case CompiledExpression::DOUBLE: | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             case CompiledExpression::NUMBER: | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |                 $value = $compiledExpression->getValue(); | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |                 return CompiledExpression::fromZvalValue(--$value); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         return new CompiledExpression(CompiledExpression::UNKNOWN); | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |