| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Hexogen\KDTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class FileTreePersister implements TreePersisterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * @var string path to the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var resource file handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $dimensions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $nodeMemorySize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(string $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->path = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param KDTreeInterface $tree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $identifier that identifies persisted tree(may be a filename, database name etc.) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function convert(KDTreeInterface $tree, string $identifier) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $dataChunk = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->handler = fopen($this->path . '/' . $identifier, 'wb'); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->dimensions = $tree->getDimensionCount(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->nodeMemorySize = $this->dimensions * 8 + 3 * 4; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $dataChunk = pack('V', $this->dimensions); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $itemCount = $tree->getItemCount(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $dataChunk = pack('V', $itemCount); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $upperBound = $tree->getMaxBoundary(); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->writeCoordinate($upperBound); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $lowerBound = $tree->getMinBoundary(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->writeCoordinate($lowerBound); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->writeNode($tree->getRoot()); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         fclose($this->handler); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param NodeInterface $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     private function writeNode(NodeInterface $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $position = ftell($this->handler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $coordinate = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $item = $node->getItem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $itemId = $item->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $dataChunk = pack('V', $itemId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $dataChunk = pack('V', 0); // left position currently unknown so it equal 0/null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $rightNode = $node->getRight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $rightPosition = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if ($rightNode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $rightPosition = $position + $this->nodeMemorySize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $dataChunk = pack('V', $rightPosition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         for ($i = 0; $i < $this->dimensions; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $coordinate[] = $item->getNthDimension($i); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->writeCoordinate($coordinate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($rightNode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->writeNode($rightNode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $leftNode = $node->getLeft(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($leftNode == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $leftPosition = ftell($this->handler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         fseek($this->handler, $position + 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $dataChunk = pack('V', $leftPosition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         fseek($this->handler, $leftPosition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->writeNode($leftNode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param array $coordinate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     private function writeCoordinate(array $coordinate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $dataChunk = pack('d'.$this->dimensions, ...$coordinate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         fwrite($this->handler, $dataChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.