Code Duplication    Length = 32-32 lines in 3 locations

classes/ClassGenerator.php 3 locations

@@ 569-600 (lines=32) @@
566
    /**
567
     * @return ClassGenerator
568
     */
569
    public function generateMagicGet(): ClassGenerator
570
    {
571
        $cases = '';
572
573
        /** @var Property $property */
574
        foreach ($this->class->getProperties() as $property) {
575
            $getter = static::camelCase('get_' . $property->getName());
576
577
            try {
578
                $this->class->getMethod($getter);
579
            } catch (InvalidArgumentException $e) {
580
                continue;
581
            }
582
583
            $cases .=
584
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
585
                '        return $this->' . $getter . '();' . PHP_EOL . PHP_EOL;
586
        }
587
588
        $this->class
589
            ->addMethod('__get')
590
            ->addComment(
591
                '@inheritDoc'
592
            )->setBody(
593
                'switch ($name) {' . PHP_EOL .
594
                $cases .
595
                '    default:' . PHP_EOL .
596
                '        return null;' . PHP_EOL .
597
                '}'
598
            )->addParameter('name');
599
        return $this;
600
    }
601
602
    /**
603
     * @return ClassGenerator
@@ 646-677 (lines=32) @@
643
    /**
644
     * @return ClassGenerator
645
     */
646
    public function generateMagicIsset(): ClassGenerator
647
    {
648
        $cases = '';
649
650
        /** @var Property $property */
651
        foreach ($this->class->getProperties() as $property) {
652
            $isset = static::camelCase('isset_' . $property->getName());
653
654
            try {
655
                $this->class->getMethod($isset);
656
            } catch (InvalidArgumentException $e) {
657
                continue;
658
            }
659
660
            $cases .=
661
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
662
                '        return $this->' . $isset . '();' . PHP_EOL . PHP_EOL;
663
        }
664
665
        $this->class
666
            ->addMethod('__isset')
667
            ->addComment(
668
                '@inheritDoc'
669
            )->setBody(
670
                'switch ($name) {' . PHP_EOL .
671
                $cases .
672
                '    default:' . PHP_EOL .
673
                '        return false;' . PHP_EOL .
674
                '}'
675
            )->addParameter('name');
676
        return $this;
677
    }
678
679
    /**
680
     * @return ClassGenerator
@@ 682-713 (lines=32) @@
679
    /**
680
     * @return ClassGenerator
681
     */
682
    public function generateMagicUnset(): ClassGenerator
683
    {
684
        $cases = '';
685
686
        /** @var Property $property */
687
        foreach ($this->class->getProperties() as $property) {
688
            $unset = static::camelCase('unset_' . $property->getName());
689
690
            try {
691
                $this->class->getMethod($unset);
692
            } catch (InvalidArgumentException $e) {
693
                continue;
694
            }
695
696
            $cases .=
697
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
698
                '        return $this->' . $unset . '();' . PHP_EOL . PHP_EOL;
699
        }
700
701
        $this->class
702
            ->addMethod('__unset')
703
            ->addComment(
704
                '@inheritDoc'
705
            )->setBody(
706
                'switch ($name) {' . PHP_EOL .
707
                $cases .
708
                '    default:' . PHP_EOL .
709
                '        return $this;' . PHP_EOL .
710
                '}'
711
            )->addParameter('name');
712
        return $this;
713
    }
714
}