Code Duplication    Length = 68-69 lines in 2 locations

src/Generator.php 2 locations

@@ 387-455 (lines=69) @@
384
     * @param string $parentClass Parent entity class name
385
     * @return string Generated entity query PHP class code
386
     */
387
    protected function createCollectionClass($navigationID, $navigationName, $entityName, $navigationFields, $parentClass = '\samsoncms\api\renderable\Collection')
388
    {
389
        $this->generator
390
            ->multiComment(array('Class for getting "'.$navigationName.'" instances from database',))
391
            ->defClass($entityName, $parentClass)
392
        ;
393
394
        // Iterate additional fields
395
        $localizedFieldIDs = array();
396
        $notLocalizedFieldIDs = array();
397
        $allFieldIDs = array();
398
        $allFieldNames = array();
399
        $allFieldValueColumns = array();
400
        $realNames = array();
401
        foreach ($navigationFields as $fieldID => $fieldRow) {
402
            $fieldName = $this->fieldName($fieldRow['Name']);
403
404
            // TODO: Add different method generation depending on their field type
405
            $this->generator->text($this->generateFieldConditionMethod(
406
                $fieldName,
407
                $fieldRow[Field::F_PRIMARY],
408
                $fieldRow[Field::F_TYPE]
409
            ));
410
411
            // Store field metadata
412
            $realNames[$fieldRow['Name']] = $fieldName;
413
            $allFieldIDs[$fieldID] = $fieldName;
414
            $allFieldNames[$fieldName] = $fieldID;
415
            $allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]);
416
            if ($fieldRow[Field::F_LOCALIZED] == 1) {
417
                $localizedFieldIDs[$fieldID] = $fieldName;
418
            } else {
419
                $notLocalizedFieldIDs[$fieldID] = $fieldName;
420
            }
421
        }
422
423
        // TODO: Add generator method generation logic
424
        $class = "\n\t".'/**';
425
        $class .= "\n\t".' * @param ViewInterface $renderer Rendering instance';
426
        $class .= "\n\t".' * @param QueryInterface $query Querying instance';
427
        $class .= "\n\t".' * @param string $locale Localization identifier';
428
        $class .= "\n\t".' */';
429
        $class .= "\n\t".'public function __construct(ViewInterface $renderer, QueryInterface $query = null, $locale = null)';
430
        $class .= "\n\t".'{';
431
        $class .= "\n\t\t".'parent::__construct($renderer, $query === null ? new dbQuery() : $query, $locale);';
432
        $class .= "\n\t".'}'."\n";
433
434
        return $this->generator
435
            ->commentVar('array', 'Collection of real additional field names')
436
            ->defClassVar('$fieldRealNames', 'public static', $realNames)
437
            ->commentVar('array', 'Collection of navigation identifiers')
438
            ->defClassVar('$navigationIDs', 'protected static', array($navigationID))
439
            ->commentVar('string', 'Not transliterated entity name')
440
            ->defClassVar('$identifier', 'protected static', $this->fullEntityName($navigationName))
441
            ->commentVar('array', 'Collection of localized additional fields identifiers')
442
            ->defClassVar('$localizedFieldIDs', 'protected static', $localizedFieldIDs)
443
            ->commentVar('array', 'Collection of NOT localized additional fields identifiers')
444
            ->defClassVar('$notLocalizedFieldIDs', 'protected static', $notLocalizedFieldIDs)
445
            ->commentVar('array', 'Collection of localized additional fields identifiers')
446
            ->defClassVar('$fieldIDs', 'protected static', $allFieldIDs)
447
            ->commentVar('array', 'Collection of additional fields value column names')
448
            ->defClassVar('$fieldValueColumns', 'protected static', $allFieldValueColumns)
449
            ->commentVar('array', 'Collection of additional field names')
450
            ->defClassVar('$fieldNames', 'public static', $allFieldNames)
451
            ->text($class)
452
            ->endClass()
453
            ->flush()
454
            ;
455
    }
456
457
    /**
458
     * Create entity query PHP class code.
@@ 467-534 (lines=68) @@
464
     * @param string $parentClass Parent entity class name
465
     * @return string Generated entity query PHP class code
466
     */
467
    protected function createQueryClass($navigationID, $navigationName, $entityName, $navigationFields, $parentClass = '\samsoncms\api\query\Entity')
468
    {
469
        $this->generator
470
            ->multiComment(array('Class for getting "'.$navigationName.'" instances from database'))
471
            ->defClass($entityName, $parentClass)
472
        ;
473
474
        // Iterate additional fields
475
        $localizedFieldIDs = array();
476
        $notLocalizedFieldIDs = array();
477
        $allFieldIDs = array();
478
        $allFieldNames = array();
479
        $allFieldValueColumns = array();
480
        $realNames = array();
481
        foreach ($navigationFields as $fieldID => $fieldRow) {
482
            $fieldName = $this->fieldName($fieldRow['Name']);
483
484
            // TODO: Add different method generation depending on their field type
485
            $this->generator->text($this->generateFieldConditionMethod(
486
                $fieldName,
487
                $fieldRow[Field::F_PRIMARY],
488
                $fieldRow[Field::F_TYPE]
489
            ));
490
491
            // Store field metadata
492
            $realNames[$fieldRow['Name']] = $fieldName;
493
            $allFieldIDs[$fieldID] = $fieldName;
494
            $allFieldNames[$fieldName] = $fieldID;
495
            $allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]);
496
            if ($fieldRow[Field::F_LOCALIZED] == 1) {
497
                $localizedFieldIDs[$fieldID] = $fieldName;
498
            } else {
499
                $notLocalizedFieldIDs[$fieldID] = $fieldName;
500
            }
501
        }
502
503
        // TODO: Add generator method generation logic
504
        $class = "\n\t".'/**';
505
        $class .= "\n\t".' * @param QueryInterface $query Rendering instance';
506
        $class .= "\n\t".' * @param string $locale Localization identifier';
507
        $class .= "\n\t".' */';
508
        $class .= "\n\t".'public function __construct(QueryInterface $query = null, $locale = null)';
509
        $class .= "\n\t".'{';
510
        $class .= "\n\t\t".'parent::__construct($query === null ? new dbQuery() : $query, $locale);';
511
        $class .= "\n\t".'}'."\n";
512
513
        return $this->generator
514
            ->commentVar('array', 'Collection of real additional field names')
515
            ->defClassVar('$fieldRealNames', 'public static', $realNames)
516
            ->commentVar('array', 'Collection of navigation identifiers')
517
            ->defClassVar('$navigationIDs', 'protected static', array($navigationID))
518
            ->commentVar('string', 'Not transliterated entity name')
519
            ->defClassVar('$identifier', 'protected static', $this->fullEntityName($navigationName))
520
            ->commentVar('array', 'Collection of localized additional fields identifiers')
521
            ->defClassVar('$localizedFieldIDs', 'protected static', $localizedFieldIDs)
522
            ->commentVar('array', 'Collection of NOT localized additional fields identifiers')
523
            ->defClassVar('$notLocalizedFieldIDs', 'protected static', $notLocalizedFieldIDs)
524
            ->commentVar('array', 'Collection of localized additional fields identifiers')
525
            ->defClassVar('$fieldIDs', 'protected static', $allFieldIDs)
526
            ->commentVar('array', 'Collection of additional fields value column names')
527
            ->defClassVar('$fieldValueColumns', 'protected static', $allFieldValueColumns)
528
            ->commentVar('array', 'Collection of additional field names')
529
            ->defClassVar('$fieldNames', 'public static', $allFieldNames)
530
            ->text($class)
531
            ->endClass()
532
            ->flush()
533
        ;
534
    }
535
536
    /** @return string Entity state hash */
537
    public function entityHash()