Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

@@ 651-726 (lines=76) @@
648
     *
649
     * @return string|string[]|null
650
     */
651
    public function val($value = null)
652
    {
653
        if ($value === null) {
654
            if (
655
                $this->tag === 'input'
656
                &&
657
                (
658
                    $this->getAttribute('type') === 'text'
659
                    ||
660
                    !$this->hasAttribute('type')
661
                )
662
            ) {
663
                return $this->getAttribute('value');
664
            }
665
666
            if (
667
                $this->hasAttribute('checked')
668
                &&
669
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
670
            ) {
671
                return $this->getAttribute('value');
672
            }
673
674
            if ($this->node->nodeName === 'select') {
675
                $valuesFromDom = [];
676
                $options = $this->getElementsByTagName('option');
677
                if ($options instanceof SimpleHtmlDomNode) {
678
                    foreach ($options as $option) {
679
                        if ($this->hasAttribute('checked')) {
680
                            /** @noinspection UnnecessaryCastingInspection */
681
                            $valuesFromDom[] = (string) $option->getAttribute('value');
682
                        }
683
                    }
684
                }
685
686
                if (\count($valuesFromDom) === 0) {
687
                    return null;
688
                }
689
690
                return $valuesFromDom;
691
            }
692
693
            if ($this->node->nodeName === 'textarea') {
694
                return $this->node->nodeValue;
695
            }
696
        } else {
697
            /** @noinspection NestedPositiveIfStatementsInspection */
698
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
699
                if ($value === $this->getAttribute('value')) {
700
                    /** @noinspection UnusedFunctionResultInspection */
701
                    $this->setAttribute('checked', 'checked');
702
                } else {
703
                    /** @noinspection UnusedFunctionResultInspection */
704
                    $this->removeAttribute('checked');
705
                }
706
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
707
                foreach ($this->node->getElementsByTagName('option') as $option) {
708
                    /** @var \DOMElement $option */
709
                    if ($value === $option->getAttribute('value')) {
710
                        /** @noinspection UnusedFunctionResultInspection */
711
                        $option->setAttribute('selected', 'selected');
712
                    } else {
713
                        $option->removeAttribute('selected');
714
                    }
715
                }
716
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
717
                // Set value for input elements
718
                /** @noinspection UnusedFunctionResultInspection */
719
                $this->setAttribute('value', $value);
720
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
721
                $this->node->nodeValue = $value;
722
            }
723
        }
724
725
        return null;
726
    }
727
728
    /**
729
     * @param HtmlDomParser $newDocument

src/voku/helper/SimpleXmlDom.php 1 location

@@ 637-712 (lines=76) @@
634
     *
635
     * @return string|string[]|null
636
     */
637
    public function val($value = null)
638
    {
639
        if ($value === null) {
640
            if (
641
                $this->tag === 'input'
642
                &&
643
                (
644
                    $this->getAttribute('type') === 'text'
645
                    ||
646
                    !$this->hasAttribute('type')
647
                )
648
            ) {
649
                return $this->getAttribute('value');
650
            }
651
652
            if (
653
                $this->hasAttribute('checked')
654
                &&
655
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
656
            ) {
657
                return $this->getAttribute('value');
658
            }
659
660
            if ($this->node->nodeName === 'select') {
661
                $valuesFromDom = [];
662
                $options = $this->getElementsByTagName('option');
663
                if ($options instanceof SimpleXmlDomNode) {
664
                    foreach ($options as $option) {
665
                        if ($this->hasAttribute('checked')) {
666
                            /** @noinspection UnnecessaryCastingInspection */
667
                            $valuesFromDom[] = (string) $option->getAttribute('value');
668
                        }
669
                    }
670
                }
671
672
                if (\count($valuesFromDom) === 0) {
673
                    return null;
674
                }
675
676
                return $valuesFromDom;
677
            }
678
679
            if ($this->node->nodeName === 'textarea') {
680
                return $this->node->nodeValue;
681
            }
682
        } else {
683
            /** @noinspection NestedPositiveIfStatementsInspection */
684
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
685
                if ($value === $this->getAttribute('value')) {
686
                    /** @noinspection UnusedFunctionResultInspection */
687
                    $this->setAttribute('checked', 'checked');
688
                } else {
689
                    /** @noinspection UnusedFunctionResultInspection */
690
                    $this->removeAttribute('checked');
691
                }
692
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
693
                foreach ($this->node->getElementsByTagName('option') as $option) {
694
                    /** @var \DOMElement $option */
695
                    if ($value === $option->getAttribute('value')) {
696
                        /** @noinspection UnusedFunctionResultInspection */
697
                        $option->setAttribute('selected', 'selected');
698
                    } else {
699
                        $option->removeAttribute('selected');
700
                    }
701
                }
702
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
703
                // Set value for input elements
704
                /** @noinspection UnusedFunctionResultInspection */
705
                $this->setAttribute('value', $value);
706
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
707
                $this->node->nodeValue = $value;
708
            }
709
        }
710
711
        return null;
712
    }
713
714
    /**
715
     * Retrieve an external iterator.