| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | //[PHPCOMPRESSOR(remove,start)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Created by Vitaly Iegorov <[email protected]>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * on 22.03.16 at 17:50 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace samsoncms\api\generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use samsoncms\api\generator\metadata\RealMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use samsoncms\api\Record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Real database instances class generator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package samsoncms\api\generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class RealEntity extends Generic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Class definition generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param \samsoncms\api\generator\metadata\GenericMetadata $metadata Entity metadata | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected function createDefinition($metadata) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->generator | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             ->multiComment(array('"' . $metadata->entity . '" database entity class')) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             ->defClass($this->className, '\samson\activerecord\\' . $metadata->entityName); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Class constants generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param RealMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected function createConstants($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ->commentVar('string', 'Entity full class name, use ::class instead') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->defClassConst('ENTITY', $metadata->entityClassName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->commentVar('string', 'Entity manager full class name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->defClassConst('MANAGER', $metadata->entityClassName . 'Query'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // Create all entity fields constants storing each additional field metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         foreach ($metadata->fields as $fieldID => $fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 ->commentVar('string', $fieldName . ' entity field') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 ->defClassConst('F_' . $fieldName, $fieldName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Class static fields generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param RealMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected function createStaticFields($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->defClassVar('$_map', 'public static ', $metadata->arMap) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->defClassVar('$_own_group', 'public static ', $metadata->arGroup) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->defClassVar('$_relations', 'public static ', $metadata->arRelations) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ->defClassVar('$fieldIDs', 'protected static ', $metadata->fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Class fields generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param RealMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 85 |  | View Code Duplication |     protected function createFields($metadata) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($metadata->fields as $fieldID => $fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 ->commentVar($metadata->types[$fieldID], $fieldName . ' entity field') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 ->defClassVar('$' . $fieldName, 'public'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  | //[PHPCOMPRESSOR(remove,end)] | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                        
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.