Code Duplication    Length = 78-78 lines in 2 locations

src/voku/helper/SimpleXmlDom.php 1 location

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

src/voku/helper/SimpleHtmlDom.php 1 location

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