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

@@ 718-795 (lines=78) @@
715
     *
716
     * @return string|string[]|null
717
     */
718
    public function val($value = null)
719
    {
720
        if ($value === null) {
721
            if (
722
                $this->tag === 'input'
723
                &&
724
                (
725
                    $this->getAttribute('type') === 'hidden'
726
                    ||
727
                    $this->getAttribute('type') === 'text'
728
                    ||
729
                    !$this->hasAttribute('type')
730
                )
731
            ) {
732
                return $this->getAttribute('value');
733
            }
734
735
            if (
736
                $this->hasAttribute('checked')
737
                &&
738
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
739
            ) {
740
                return $this->getAttribute('value');
741
            }
742
743
            if ($this->node->nodeName === 'select') {
744
                $valuesFromDom = [];
745
                $options = $this->getElementsByTagName('option');
746
                if ($options instanceof SimpleHtmlDomNode) {
747
                    foreach ($options as $option) {
748
                        if ($this->hasAttribute('checked')) {
749
                            $valuesFromDom[] = (string) $option->getAttribute('value');
750
                        }
751
                    }
752
                }
753
754
                if (\count($valuesFromDom) === 0) {
755
                    return null;
756
                }
757
758
                return $valuesFromDom;
759
            }
760
761
            if ($this->node->nodeName === 'textarea') {
762
                return $this->node->nodeValue;
763
            }
764
        } else {
765
            /** @noinspection NestedPositiveIfStatementsInspection */
766
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
767
                if ($value === $this->getAttribute('value')) {
768
                    /** @noinspection UnusedFunctionResultInspection */
769
                    $this->setAttribute('checked', 'checked');
770
                } else {
771
                    /** @noinspection UnusedFunctionResultInspection */
772
                    $this->removeAttribute('checked');
773
                }
774
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
775
                foreach ($this->node->getElementsByTagName('option') as $option) {
776
                    /** @var \DOMElement $option */
777
                    if ($value === $option->getAttribute('value')) {
778
                        /** @noinspection UnusedFunctionResultInspection */
779
                        $option->setAttribute('selected', 'selected');
780
                    } else {
781
                        $option->removeAttribute('selected');
782
                    }
783
                }
784
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
785
                // Set value for input elements
786
                /** @noinspection UnusedFunctionResultInspection */
787
                $this->setAttribute('value', $value);
788
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
789
                $this->node->nodeValue = $value;
790
            }
791
        }
792
793
        return null;
794
    }
795
796
    /**
797
     * @param HtmlDomParser $newDocument
798
     * @param bool          $removeExtraHeadTag