Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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