| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Relation trait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Define a relation between two models. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Caffeina srl - 2016 - http://caffeina.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | trait Relation { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |    * [Internal] : Retrieve/Set relation options | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |    * This function can be used to get all options passing null, setting options passing an associative | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |    * array or retrieve a single value passing a string | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |    * @param  mixed $options The options passed to the relation layer. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |    * @return mixed          All options array or a single value | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |   protected static function & relationOptions(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     static $_options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     if ($_options === null) $_options = (object)[ 'links' => [], 'relations' => [] ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     return $_options; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |    * [Internal] : Assigns or retrieve the Save callback | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |    * The save callback interface is | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |    *   function($table, array $options) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |    * @param  callable $callback The callback to use on model save | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |    * @return callable           Current save callback | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |   private static function relationAddRelationshipTo($link, $plurality, $extra=[]){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     $options = static::relationOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     preg_match('((?<FOREIGN_CLASS>\w+)(\.(?<FOREIGN_KEY>\w+))?(:(?<LOCAL_KEY>\w+))?)', $link, $parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     $foreign_class = isset($parts['FOREIGN_CLASS']) ? $parts['FOREIGN_CLASS'] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     $foreign_key   = isset($parts['FOREIGN_KEY'])   ? $parts['FOREIGN_KEY']   : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     $local_key     = isset($parts['LOCAL_KEY'])     ? $parts['LOCAL_KEY']     : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     if ( ! $foreign_class ) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |        throw new Exception("[Core.Relation] Class ".get_called_class()." must define a foreign Model when assigning a relation.", 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     if ( ! is_subclass_of($foreign_class,"Model") ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |        throw new Exception("[Core.Relation] Class ".get_called_class()." want to relate to $foreign_class but it's not a Model.", 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     if ( ! $foreign_key ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |       // Retrieve primary key from foreign class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |       $foreign_key = $foreign_class::persistenceOptions("key"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     if ( ! $local_key ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |       // Retrieve local primary key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |       $local_key = static::persistenceOptions("key"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     $single = $plurality == 'single'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     $method = preg_replace_callback('([A-Z])', function($m){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |       return "_" . strtolower($m[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     }, lcfirst($foreign_class) . ($single ? '' : 's')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     $hh = [$foreign_class,$foreign_key,$local_key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     sort($hh); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     $options->links[md5(serialize($hh))] = $rel = (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |       'foreign_class' =>  $foreign_class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |       'foreign_key'   =>  $foreign_key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |       'local_key'     =>  $local_key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |       'single'        =>  $single, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |       'method'        =>  $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |       'extra'         =>  (object) $extra, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     if (empty($options->relations)) $options->relations = (object)[]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     $options->relations->$method = $getset = (object)[ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |       'get' => function($self) use ($foreign_class, $rel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |          $val = $self->{$rel->local_key}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |          $val = is_numeric($val) ? $val : "'" . addslashes($val) . "'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |          $data = $foreign_class::where("{$rel->foreign_key} = {$val}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |          return $rel->single ? current($data) : $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |       'set' => function($value, $self) use ($foreign_class, $rel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if (!is_a($value, $foreign_class)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |           throw new Exception("[Core.Relation] Relationship for {$rel->method} must be of class $foreign_class.", 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $self->local_key = $value->foreign_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |   public function __get($name){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     $options = static::relationOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     if (isset($options->relations->$name)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |       return call_user_func($options->relations->$name->get, $this); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |   public function __set($name, $value){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     $options = static::relationOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     if (isset($options->relations->$name)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |       call_user_func($options->relations->$name->set, $value, $this); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |   public function __isset($name){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     $options = static::relationOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     return isset($options->relations->$name); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |   public static function hasOne($modelName, $extra=[]){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     return static::relationAddRelationshipTo($modelName, 'single', $extra); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |   } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |   public static function hasMany($modelName, $extra=[]){ | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     return static::relationAddRelationshipTo($modelName, 'multiple', $extra); | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |   } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                        
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.