Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

@@ 681-756 (lines=76) @@
678
     *
679
     * @return string|string[]|null
680
     */
681
    public function val($value = null)
682
    {
683
        if ($value === null) {
684
            if (
685
                $this->tag === 'input'
686
                &&
687
                (
688
                    $this->getAttribute('type') === 'text'
689
                    ||
690
                    !$this->hasAttribute('type')
691
                )
692
            ) {
693
                return $this->getAttribute('value');
694
            }
695
696
            if (
697
                $this->hasAttribute('checked')
698
                &&
699
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
700
            ) {
701
                return $this->getAttribute('value');
702
            }
703
704
            if ($this->node->nodeName === 'select') {
705
                $valuesFromDom = [];
706
                $options = $this->getElementsByTagName('option');
707
                if ($options instanceof SimpleHtmlDomNode) {
708
                    foreach ($options as $option) {
709
                        if ($this->hasAttribute('checked')) {
710
                            /** @noinspection UnnecessaryCastingInspection */
711
                            $valuesFromDom[] = (string) $option->getAttribute('value');
712
                        }
713
                    }
714
                }
715
716
                if (\count($valuesFromDom) === 0) {
717
                    return null;
718
                }
719
720
                return $valuesFromDom;
721
            }
722
723
            if ($this->node->nodeName === 'textarea') {
724
                return $this->node->nodeValue;
725
            }
726
        } else {
727
            /** @noinspection NestedPositiveIfStatementsInspection */
728
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
729
                if ($value === $this->getAttribute('value')) {
730
                    /** @noinspection UnusedFunctionResultInspection */
731
                    $this->setAttribute('checked', 'checked');
732
                } else {
733
                    /** @noinspection UnusedFunctionResultInspection */
734
                    $this->removeAttribute('checked');
735
                }
736
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
737
                foreach ($this->node->getElementsByTagName('option') as $option) {
738
                    /** @var \DOMElement $option */
739
                    if ($value === $option->getAttribute('value')) {
740
                        /** @noinspection UnusedFunctionResultInspection */
741
                        $option->setAttribute('selected', 'selected');
742
                    } else {
743
                        $option->removeAttribute('selected');
744
                    }
745
                }
746
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
747
                // Set value for input elements
748
                /** @noinspection UnusedFunctionResultInspection */
749
                $this->setAttribute('value', $value);
750
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
751
                $this->node->nodeValue = $value;
752
            }
753
        }
754
755
        return null;
756
    }
757
758
    /**
759
     * @param HtmlDomParser $newDocument

src/voku/helper/SimpleXmlDom.php 1 location

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