| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PHPSA\Analyzer\Pass\Expression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpParser\Node\Expr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PHPSA\Analyzer\Pass\AnalyzerPassInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPSA\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPSA\CompiledExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Casts implements AnalyzerPassInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param Expr $expr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param Context $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 1 |  |     public function pass(Expr $expr, Context $context) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 1 |  |         $castType = CompiledExpression::UNKNOWN; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |         switch (true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 1 |  |             case ($expr instanceof Expr\Cast\Array_): | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |                 $castType = CompiledExpression::ARR; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 1 |  |             case ($expr instanceof Expr\Cast\Bool_): | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 1 |  |                 $castType = CompiledExpression::BOOLEAN; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 1 |  |             case ($expr instanceof Expr\Cast\Int_): | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |                 $castType = CompiledExpression::INTEGER; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |             case ($expr instanceof Expr\Cast\Double): | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 1 |  |                 $castType = CompiledExpression::DOUBLE; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 1 |  |             case ($expr instanceof Expr\Cast\Object_): | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 1 |  |                 $castType = CompiledExpression::OBJECT; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |             case ($expr instanceof Expr\Cast\String_): | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |                 $castType = CompiledExpression::STRING; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         $compiledExpression = $context->getExpressionCompiler()->compile($expr->expr); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |         $ExprType = $compiledExpression->getType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |         $typeName = $compiledExpression->getTypeName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |         if ($castType === $ExprType) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |             $context->notice( | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |                 'stupid.cast', | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |                 'You are trying to cast \''.$typeName.'\' to \''.$typeName.'\'.', | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 $expr | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |         } elseif ($expr instanceof Expr\Cast\Unset_ && $ExprType === CompiledExpression::NULL) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |             $context->notice( | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |                 'stupid.cast', | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |                 'You are trying to cast \'unset\' to \'null\'.', | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 $expr | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return TreeBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |     public function getRegister() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             Expr\Cast\Array_::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             Expr\Cast\Bool_::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             Expr\Cast\Int_::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             Expr\Cast\Double::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             Expr\Cast\Object_::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             Expr\Cast\String_::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             Expr\Cast\Unset_::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.