Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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