| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016, 2015 Richard Klees <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This software is licensed under The MIT License. You should have received | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * a copy of the licence along with the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Lechimp\Dicto\Indexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Lechimp\Dicto\Indexer as I; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Lechimp\Dicto\Variables\Variable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PhpParser\Node as N; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Implementation of Indexer with PhpParser. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Indexer implements Location, ListenerRegistry, \PhpParser\NodeVisitor { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $project_root_path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var I\Insert|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $insert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var \PhpParser\Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var array   string => array() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $listeners; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     // state for parsing a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $file_path = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var string[]|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $file_content = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * This contains the stack of ids were currently in, i.e. the nesting of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * known code blocks we are in. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @var array|null  contain ($entity_type, $entity_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected $entity_stack = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 22 |  |     public function __construct(\PhpParser\Parser $parser, $project_root_path, Insert $insert) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 22 |  |         $this->parser = $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 22 |  |         assert('is_string($project_root_path)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 22 |  |         $this->project_root_path = $project_root_path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 22 |  |         $this->insert = $insert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // TODO: This could contain class names from PhpParser as optimisation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 22 |  |         $this->listeners = array("misc" => array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // TODO: This should be more dynamic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 22 |  |         $this->register_listeners(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 22 |  |     protected function register_listeners() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 22 |  |         $d = new \Lechimp\Dicto\Rules\DependOn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 22 |  |         $d->register_listeners($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 22 |  |         $i = new \Lechimp\Dicto\Rules\Invoke(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 22 |  |         $i->register_listeners($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 21 |  |     public function index_file($path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 21 |  |         if ($this->insert === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 "Set an inserter to be used before starting to index files."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 21 |  |         $content = file_get_contents($this->project_root_path."/$path"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 21 |  |         if ($content === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             throw \InvalidArgumentException("Can't read file $path."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 21 |  |         $stmts = $this->parser->parse($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 21 |  |         if ($stmts === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             throw new \RuntimeException("Can't parse file $path."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 21 |  |         if ($stmts === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             throw new \RuntimeException("Could not parse file '$path'."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 21 |  |         $traverser = new \PhpParser\NodeTraverser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 21 |  |         $traverser->addVisitor($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 21 |  |         $this->entity_stack = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 21 |  |         $this->file_path = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 21 |  |         $this->file_content = explode("\n", $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 21 |  |         $this->reference_cache = array(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 21 |  |         $traverser->traverse($stmts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 21 |  |         $this->entity_stack = null;  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 21 |  |         $this->file_path = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 21 |  |         $this->file_content = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 21 |  |         $this->reference_cache = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     // helper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 21 |  |     private function lines_from_to($start, $end) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 21 |  |         assert('is_int($start)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 21 |  |         assert('is_int($end)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 21 |  |         return implode("\n", array_slice($this->file_content, $start-1, $end-$start+1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |    // from ListenerRegistry  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 22 |  |     public function on_enter_misc(\Closure $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 22 |  |         $this->listeners["misc"][] = $listener; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |    // from Location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 16 |  |     public function file_path() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 16 |  |         return $this->file_path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 16 |  |     public function file_content($from_line = null, $to_line = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 16 |  |         if ($from_line !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 16 |  |             assert('$to_line !== null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 16 |  |             return $this->lines_from_to($from_line, $to_line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             assert('$to_line === null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             return $this->implode("\n", $this->file_content); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 16 |  |     public function in_entities() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 16 |  |         return $this->entity_stack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     // from \PhpParser\NodeVisitor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 21 |  |     public function beforeTraverse(array $nodes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         // for sure found a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 21 |  |         $id = $this->insert->entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 21 |  |             ( Variable::FILE_TYPE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 21 |  |             , $this->file_path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 21 |  |             , $this->file_path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 21 |  |             , 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 21 |  |             , count($this->file_content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 21 |  |             , implode("\n", $this->file_content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 21 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 21 |  |         $this->entity_stack[] = array(Variable::FILE_TYPE, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         // TODO: reimplement this in some other way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         foreach ($this->listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $listener->on_enter_file($id, $this->file_path, implode("\n", $this->file_content)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 21 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 21 |  |     public function afterTraverse(array $nodes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 21 |  |         $type_and_id = array_pop($this->entity_stack); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         // TODO: reimplement this in some other way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         foreach ($this->listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             $listener->on_leave_file($type_and_id[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 21 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 207 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 208 | 21 |  |     public function enterNode(\PhpParser\Node $node) { | 
            
                                                                        
                            
            
                                    
            
            
                | 209 | 21 |  |         $start_line = $node->getAttribute("startLine"); | 
            
                                                                        
                            
            
                                    
            
            
                | 210 | 21 |  |         $end_line = $node->getAttribute("endLine"); | 
            
                                                                        
                            
            
                                    
            
            
                | 211 | 21 |  |         $source = $this->lines_from_to($start_line, $end_line); | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 213 | 21 |  |         $id = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 214 | 21 |  |         $type = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |         // Class | 
            
                                                                        
                            
            
                                    
            
            
                | 217 | 21 |  |         if ($node instanceof N\Stmt\Class_) { | 
            
                                                                        
                            
            
                                    
            
            
                | 218 | 20 |  |             $type = Variable::CLASS_TYPE; | 
            
                                                                        
                            
            
                                    
            
            
                | 219 | 20 |  |             $id = $this->insert->entity | 
            
                                                                        
                            
            
                                    
            
            
                | 220 | 20 |  |                 ( $type | 
            
                                                                        
                            
            
                                    
            
            
                | 221 | 20 |  |                 , $node->name | 
            
                                                                        
                            
            
                                    
            
            
                | 222 | 20 |  |                 , $this->file_path | 
            
                                                                        
                            
            
                                    
            
            
                | 223 | 20 |  |                 , $start_line | 
            
                                                                        
                            
            
                                    
            
            
                | 224 | 20 |  |                 , $end_line | 
            
                                                                        
                            
            
                                    
            
            
                | 225 | 20 |  |                 , $source | 
            
                                                                        
                            
            
                                    
            
            
                | 226 | 20 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 228 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                        
                            
            
                                    
            
            
                | 229 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 230 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                        
                            
            
                                    
            
            
                | 231 |  |  |                 $listener->on_enter_class($id, $node); | 
            
                                                                        
                            
            
                                    
            
            
                | 232 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 233 |  |  |             */ | 
            
                                                                        
                            
            
                                    
            
            
                | 234 | 20 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 235 |  |  |         // Method or Function | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 236 | 21 | View Code Duplication |         elseif ($node instanceof N\Stmt\ClassMethod) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 237 | 20 |  |             $type = Variable::METHOD_TYPE; | 
            
                                                                        
                            
            
                                    
            
            
                | 238 | 20 |  |             $id = $this->insert->entity | 
            
                                                                        
                            
            
                                    
            
            
                | 239 | 20 |  |                 ( $type | 
            
                                                                        
                            
            
                                    
            
            
                | 240 | 20 |  |                 , $node->name | 
            
                                                                        
                            
            
                                    
            
            
                | 241 | 20 |  |                 , $this->file_path | 
            
                                                                        
                            
            
                                    
            
            
                | 242 | 20 |  |                 , $start_line | 
            
                                                                        
                            
            
                                    
            
            
                | 243 | 20 |  |                 , $end_line | 
            
                                                                        
                            
            
                                    
            
            
                | 244 | 20 |  |                 , $source | 
            
                                                                        
                            
            
                                    
            
            
                | 245 | 20 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |                 $listener->on_enter_method($id, $node); | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  |             */ | 
            
                                                                        
                            
            
                                    
            
            
                | 253 | 20 |  |         } | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 254 | 21 | View Code Duplication |         elseif ($node instanceof N\Stmt\Function_) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |             $type = Variable::FUNCTION_TYPE; | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  |             $id = $this->insert->entity | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |                 ( $type  | 
            
                                                                        
                            
            
                                    
            
            
                | 258 |  |  |                 , $node->name | 
            
                                                                        
                            
            
                                    
            
            
                | 259 |  |  |                 , $this->file_path | 
            
                                                                        
                            
            
                                    
            
            
                | 260 |  |  |                 , $start_line | 
            
                                                                        
                            
            
                                    
            
            
                | 261 |  |  |                 , $end_line | 
            
                                                                        
                            
            
                                    
            
            
                | 262 |  |  |                 , $source | 
            
                                                                        
                            
            
                                    
            
            
                | 263 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 265 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                        
                            
            
                                    
            
            
                | 266 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 267 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |                 $listener->on_enter_function($id, $node); | 
            
                                                                        
                            
            
                                    
            
            
                | 269 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |             */ | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |         else { | 
            
                                                                        
                            
            
                                    
            
            
                | 273 | 21 |  |             foreach ($this->listeners["misc"] as $listener) { | 
            
                                                                        
                            
            
                                    
            
            
                | 274 | 21 |  |                 $listener($this->insert, $this, $node); | 
            
                                                                        
                            
            
                                    
            
            
                | 275 | 21 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 276 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 278 | 21 |  |         if ($id !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 279 | 20 |  |             $this->entity_stack[] = array($type, $id); | 
            
                                                                        
                            
            
                                    
            
            
                | 280 | 20 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 281 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 | 21 |  |     public function leaveNode(\PhpParser\Node $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |         // Class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 | 21 |  |         if ($node instanceof N\Stmt\Class_) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |             // We pushed it, we need to pop it now, as we are leaving the class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 | 20 |  |             $type_and_id = array_pop($this->entity_stack); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |                 $listener->on_leave_class($type_and_id[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 20 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |         // Method or Function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 21 |  |         elseif ($node instanceof N\Stmt\ClassMethod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |             // We pushed it, we need to pop it now, as we are leaving the method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |             // or function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 | 20 |  |             $type_and_id = array_pop($this->entity_stack); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |                 $listener->on_leave_method($type_and_id[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 | 20 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 | 21 |  |         elseif ($node instanceof N\Stmt\Function_) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |             // We pushed it, we need to pop it now, as we are leaving the method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |             // or function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |             $type_and_id = array_pop($this->entity_stack); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |                 $listener->on_leave_function($type_and_id[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         else { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             // TODO: reimplement this in some other way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |             /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |             foreach ($this->listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |                 $listener->on_leave_misc($this->insert, $this, $node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 | 21 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 333 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 334 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: