Code Duplication    Length = 47-47 lines in 2 locations

src/Generator.php 2 locations

@@ 104-150 (lines=47) @@
101
     * @param array $structureRow Collection of structure info
102
     * @return string Generated entitiy class code
103
     */
104
    protected function createEntityClass($structureRow)
105
    {
106
        $structureKey = ucfirst($this->transliterated($structureRow['Name']));
107
108
        $class = "\n" . 'class ' . $structureKey . ' extends Entity';
109
        $class .= "\n" . '{';
110
        $class .= "\n\t" . '/** @var string Not transliterated entity name */';
111
        $class .= "\n\t" . 'protected $identifier = "' . $structureRow['Name'] . '";';
112
113
        // Get structure fields
114
        //$fieldMap = array();
115
        $fields = array();
116
        $fieldIDs = array();
117
118
        // TODO: Optimize queries
119
        foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $structureRow['StructureID'] . '" AND `Active` = "1"') as $fieldStructureRow) {
120
            foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) {
121
                $type = str_replace(
122
                    '\samsoncms\api\Field',
123
                    'Field',
124
                    $this->constantNameByValue($fieldRow['Type'])
125
                );
126
                $commentType = Field::$PHP_TYPE[$fieldRow['Type']];
127
                $fieldName = lcfirst($this->transliterated($fieldRow['Name']));
128
129
                $class .= "\n\t" . '/** @var ' . $commentType . ' Field #' . $fieldRow['FieldID'] . '*/';
130
                $class .= "\n\t" . 'protected $' . $fieldName . ';';
131
132
                // Store field metadata
133
                $fields[$fieldName][] = $fieldRow;
134
                $fieldIDs[] = $fieldRow['FieldID'];
135
                //$fieldMap[] = '"'.$fieldName.'" => array("Id" => "'.$fieldRow['FieldID'].'", "Type" => ' . $type . ', "Name" => "' . $fieldRow['Name'] . '")';
136
            }
137
        }
138
139
        //$class .= "\n\t" . '/** @var array Entity additional fields metadata */';
140
        //$class .= "\n\t" .'protected $fieldsData = array('."\n\t\t".implode(','."\n\t\t", $fieldMap)."\n\t".');';
141
        $class .= "\n\t";
142
        $class .= "\n\t" . '/** @var array Collection of additional fields identifiers */';
143
        $class .= "\n\t" . 'protected static $fieldIDs = array(' . implode(',', $fieldIDs) . ');';
144
        $class .= "\n\t" . '/** @var array Collection of navigation identifiers */';
145
        $class .= "\n\t" . 'protected static $navigationIDs = array(' . $structureRow['StructureID'] . ');';
146
        $class .= "\n" . '}';
147
148
        // Replace tabs with spaces
149
        return str_replace("\t", '    ', $class);
150
    }
151
152
    /**
153
     * Create entity PHP class code.
@@ 158-204 (lines=47) @@
155
     * @param array $structureRow Collection of structure info
156
     * @return string Generated entitiy class code
157
     */
158
    protected function createQueryClass($structureRow)
159
    {
160
        $structureKey = ucfirst($this->transliterated($structureRow['Name']));
161
162
        $class = "\n" . 'class ' . $structureKey . ' extends Base';
163
        $class .= "\n" . '{';
164
        $class .= "\n\t" . '/** @var string Not transliterated entity name */';
165
        $class .= "\n\t" . 'protected $identifier = "' . $structureRow['Name'] . '";';
166
167
        // Get structure fields
168
        //$fieldMap = array();
169
        $fields = array();
170
        $fieldIDs = array();
171
172
        // TODO: Optimize queries
173
        foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $structureRow['StructureID'] . '" AND `Active` = "1"') as $fieldStructureRow) {
174
            foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) {
175
                $type = str_replace(
176
                    '\samsoncms\api\Field',
177
                    'Field',
178
                    $this->constantNameByValue($fieldRow['Type'])
179
                );
180
                $commentType = Field::$PHP_TYPE[$fieldRow['Type']];
181
                $fieldName = lcfirst($this->transliterated($fieldRow['Name']));
182
183
                $class .= "\n\t" . '/** @var ' . $commentType . ' Field #' . $fieldRow['FieldID'] . '*/';
184
                $class .= "\n\t" . 'protected $' . $fieldName . ';';
185
186
                // Store field metadata
187
                $fields[$fieldName][] = $fieldRow;
188
                $fieldIDs[] = $fieldRow['FieldID'];
189
                //$fieldMap[] = '"'.$fieldName.'" => array("Id" => "'.$fieldRow['FieldID'].'", "Type" => ' . $type . ', "Name" => "' . $fieldRow['Name'] . '")';
190
            }
191
        }
192
193
        //$class .= "\n\t" . '/** @var array Entity additional fields metadata */';
194
        //$class .= "\n\t" .'protected $fieldsData = array('."\n\t\t".implode(','."\n\t\t", $fieldMap)."\n\t".');';
195
        $class .= "\n\t";
196
        $class .= "\n\t" . '/** @var array Collection of additional fields identifiers */';
197
        $class .= "\n\t" . 'protected static $fieldIDs = array(' . implode(',', $fieldIDs) . ');';
198
        $class .= "\n\t" . '/** @var array Collection of navigation identifiers */';
199
        $class .= "\n\t" . 'protected static $navigationIDs = array(' . $structureRow['StructureID'] . ');';
200
        $class .= "\n" . '}';
201
202
        // Replace tabs with spaces
203
        return str_replace("\t", '    ', $class);
204
    }
205
206
    /** @return string Entity state hash */
207
    public function entityHash()