Code Duplication    Length = 76-77 lines in 2 locations

src/Generator.php 2 locations

@@ 409-485 (lines=77) @@
406
     * @param array $navigationFields Collection of entity additional fields
407
     * @return string Generated entity query PHP class code
408
     */
409
    protected function createCollectionClass($navigationID, $navigationName, $entityName, $navigationFields)
410
    {
411
        $this->generator->multiComment(array(
412
            'Class for getting "'.$navigationName.'" instances from database',
413
            '@method '.$this->entityName($navigationName).'[] find($page = null, $count = null) Get entities collection',
414
            '@method '.$this->entityName($navigationName).' first() Get entity',
415
            '@method '.$entityName.' where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)',
416
            '@method '.$entityName.' primary($value) Query for chaining',
417
            '@method '.$entityName.' identifier($value) Query for chaining',
418
            '@method '.$entityName.' created($value) Query for chaining',
419
            '@method '.$entityName.' modified($value) Query for chaining',
420
            '@method '.$entityName.' published($value) Query for chaining'
421
        ))->defClass($entityName, '\samsoncms\api\renderable\Collection')
422
        ;
423
424
        // Iterate additional fields
425
        $localizedFieldIDs = array();
426
        $notLocalizedFieldIDs = array();
427
        $allFieldIDs = array();
428
        $allFieldNames = array();
429
        $allFieldValueColumns = array();
430
        $realNames = array();
431
        foreach ($navigationFields as $fieldID => $fieldRow) {
432
            $fieldName = $this->fieldName($fieldRow['Name']);
433
434
            // TODO: Add different method generation depending on their field type
435
            $this->generator->text($this->generateFieldConditionMethod(
436
                $fieldName,
437
                $fieldRow[Field::F_PRIMARY],
438
                $fieldRow[Field::F_TYPE]
439
            ));
440
441
            // Store field metadata
442
            $realNames[$fieldRow['Name']] = $fieldName;
443
            $allFieldIDs[$fieldID] = $fieldName;
444
            $allFieldNames[$fieldName] = $fieldID;
445
            $allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]);
446
            if ($fieldRow[Field::F_LOCALIZED] == 1) {
447
                $localizedFieldIDs[$fieldID] = $fieldName;
448
            } else {
449
                $notLocalizedFieldIDs[$fieldID] = $fieldName;
450
            }
451
        }
452
453
        // TODO: Add generator method generation logic
454
        $class = "\n\t".'/**';
455
        $class .= "\n\t".' * @param ViewInterface $renderer Rendering instance';
456
        $class .= "\n\t".' * @param QueryInterface $query Querying instance';
457
        $class .= "\n\t".' * @param string $locale Localization identifier';
458
        $class .= "\n\t".' */';
459
        $class .= "\n\t".'public function __construct(ViewInterface $renderer, QueryInterface $query = null, $locale = null)';
460
        $class .= "\n\t".'{';
461
        $class .= "\n\t\t".'parent::__construct($query === null ? new dbQuery() : $query, $renderer, $locale);';
462
        $class .= "\n\t".'}'."\n";
463
464
        return $this->generator
465
            ->commentVar('array', 'Collection of real additional field names')
466
            ->defClassVar('$fieldRealNames', 'public static', $realNames)
467
            ->commentVar('array', 'Collection of navigation identifiers')
468
            ->defClassVar('$navigationIDs', 'protected static', array($navigationID))
469
            ->commentVar('string', 'Not transliterated entity name')
470
            ->defClassVar('$identifier', 'protected static', $this->fullEntityName($navigationName))
471
            ->commentVar('array', 'Collection of localized additional fields identifiers')
472
            ->defClassVar('$localizedFieldIDs', 'protected static', $localizedFieldIDs)
473
            ->commentVar('array', 'Collection of NOT localized additional fields identifiers')
474
            ->defClassVar('$notLocalizedFieldIDs', 'protected static', $notLocalizedFieldIDs)
475
            ->commentVar('array', 'Collection of localized additional fields identifiers')
476
            ->defClassVar('$fieldIDs', 'protected static', $allFieldIDs)
477
            ->commentVar('array', 'Collection of additional fields value column names')
478
            ->defClassVar('$fieldValueColumns', 'protected static', $allFieldValueColumns)
479
            ->commentVar('array', 'Collection of additional field names')
480
            ->defClassVar('$fieldNames', 'public static', $allFieldNames)
481
            ->text($class)
482
            ->endClass()
483
            ->flush()
484
            ;
485
    }
486
487
    /**
488
     * Create entity query PHP class code.
@@ 496-571 (lines=76) @@
493
     * @param array $navigationFields Collection of entity additional fields
494
     * @return string Generated entity query PHP class code
495
     */
496
    protected function createQueryClass($navigationID, $navigationName, $entityName, $navigationFields)
497
    {
498
        $this->generator->multiComment(array(
499
            'Class for getting "'.$navigationName.'" instances from database',
500
            '@method '.$this->entityName($navigationName).'[] find($page = null, $count = null) Get entities collection',
501
            '@method '.$this->entityName($navigationName).' first() Get entity',
502
            '@method '.$entityName.' where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)',
503
            '@method '.$entityName.' primary($value) Query for chaining',
504
            '@method '.$entityName.' identifier($value) Query for chaining',
505
            '@method '.$entityName.' created($value) Query for chaining',
506
            '@method '.$entityName.' modified($value) Query for chaining',
507
            '@method '.$entityName.' published($value) Query for chaining'
508
        ))->defClass($entityName, '\samsoncms\api\query\Entity')
509
        ;
510
511
        // Iterate additional fields
512
        $localizedFieldIDs = array();
513
        $notLocalizedFieldIDs = array();
514
        $allFieldIDs = array();
515
        $allFieldNames = array();
516
        $allFieldValueColumns = array();
517
        $realNames = array();
518
        foreach ($navigationFields as $fieldID => $fieldRow) {
519
            $fieldName = $this->fieldName($fieldRow['Name']);
520
521
            // TODO: Add different method generation depending on their field type
522
            $this->generator->text($this->generateFieldConditionMethod(
523
                $fieldName,
524
                $fieldRow[Field::F_PRIMARY],
525
                $fieldRow[Field::F_TYPE]
526
            ));
527
528
            // Store field metadata
529
            $realNames[$fieldRow['Name']] = $fieldName;
530
            $allFieldIDs[$fieldID] = $fieldName;
531
            $allFieldNames[$fieldName] = $fieldID;
532
            $allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]);
533
            if ($fieldRow[Field::F_LOCALIZED] == 1) {
534
                $localizedFieldIDs[$fieldID] = $fieldName;
535
            } else {
536
                $notLocalizedFieldIDs[$fieldID] = $fieldName;
537
            }
538
        }
539
540
        // TODO: Add generator method generation logic
541
        $class = "\n\t".'/**';
542
        $class .= "\n\t".' * @param QueryInterface $query Rendering instance';
543
        $class .= "\n\t".' * @param string $locale Localization identifier';
544
        $class .= "\n\t".' */';
545
        $class .= "\n\t".'public function __construct(QueryInterface $query = null, $locale = null)';
546
        $class .= "\n\t".'{';
547
        $class .= "\n\t\t".'parent::__construct($query === null ? new dbQuery() : $query, $locale);';
548
        $class .= "\n\t".'}'."\n";
549
550
        return $this->generator
551
            ->commentVar('array', 'Collection of real additional field names')
552
            ->defClassVar('$fieldRealNames', 'public static', $realNames)
553
            ->commentVar('array', 'Collection of navigation identifiers')
554
            ->defClassVar('$navigationIDs', 'protected static', array($navigationID))
555
            ->commentVar('string', 'Not transliterated entity name')
556
            ->defClassVar('$identifier', 'protected static', $this->fullEntityName($navigationName))
557
            ->commentVar('array', 'Collection of localized additional fields identifiers')
558
            ->defClassVar('$localizedFieldIDs', 'protected static', $localizedFieldIDs)
559
            ->commentVar('array', 'Collection of NOT localized additional fields identifiers')
560
            ->defClassVar('$notLocalizedFieldIDs', 'protected static', $notLocalizedFieldIDs)
561
            ->commentVar('array', 'Collection of localized additional fields identifiers')
562
            ->defClassVar('$fieldIDs', 'protected static', $allFieldIDs)
563
            ->commentVar('array', 'Collection of additional fields value column names')
564
            ->defClassVar('$fieldValueColumns', 'protected static', $allFieldValueColumns)
565
            ->commentVar('array', 'Collection of additional field names')
566
            ->defClassVar('$fieldNames', 'public static', $allFieldNames)
567
            ->text($class)
568
            ->endClass()
569
            ->flush()
570
        ;
571
    }
572
573
    /** @return string Entity state hash */
574
    public function entityHash()