| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SQLParser\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SQLParser\Node\NodeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SQLParser\Node\Operator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SQLParser\Node\Reserved; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This class has the ability to create instances implementing NodeInterface based on a descriptive array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author David Négrier <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class StatementFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public static function toObject(array $desc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         if (isset($desc['SELECT'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             $select = new Select(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             $columns = array_map(function ($item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                 return NodeFactory::toObject($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             }, $desc['SELECT']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $columns = NodeFactory::simplify($columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             foreach ($columns as $key => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 if ($column instanceof Reserved) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     if (strtoupper($column->getBaseExpression()) === 'DISTINCT') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                         $select->setDistinct(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                         $options[] = $column->getBaseExpression(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     unset($columns[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $select->setOptions($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $select->setColumns($columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             if (isset($desc['FROM'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $from = NodeFactory::mapArrayToNodeObjectList($desc['FROM']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $select->setFrom($from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 46 |  | View Code Duplication |             if (isset($desc['WHERE'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $where = NodeFactory::mapArrayToNodeObjectList($desc['WHERE']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $where = NodeFactory::simplify($where); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $select->setWhere($where); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |             if (isset($desc['GROUP'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $group = NodeFactory::mapArrayToNodeObjectList($desc['GROUP']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $group = NodeFactory::simplify($group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $select->setGroup($group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 58 |  | View Code Duplication |             if (isset($desc['HAVING'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $having = NodeFactory::mapArrayToNodeObjectList($desc['HAVING']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $having = NodeFactory::simplify($having); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $select->setHaving($having); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |             if (isset($desc['ORDER'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $order = NodeFactory::mapArrayToNodeObjectList($desc['ORDER']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $order = NodeFactory::simplify($order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $select->setOrder($order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (isset($desc['LIMIT'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $descLimit = $desc['LIMIT']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 //$limit = NodeFactory::toObject($descLimit['limit']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 //$limit = NodeFactory::simplify($limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 if (isset($descLimit['rowcount'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     $select->setLimit(NodeFactory::toLimitNode($descLimit['rowcount'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 if (isset($descLimit['offset'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     $select->setOffset(NodeFactory::toLimitNode($descLimit['offset'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 //$offset = NodeFactory::toObject($descLimit['offset']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 //$offset = NodeFactory::simplify($offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 //$select->setOffset($offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return $select; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } elseif (isset($desc['UNION'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $selects = array_map([self::class, 'toObject'], $desc['UNION']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return new Union($selects); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             throw new \BadMethodCallException('Unknown query'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param array $descLimit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /*private static function checkLimitDesc(array $descLimit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (count($descLimit) > 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             throw new \Exception('The limit returned by the SQLParser contains more than 2 items, something might went wrong.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return ['offset' => $descLimit['offset'], 'limit' => $descLimit['rowcount']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     }*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.