Code Duplication    Length = 78-78 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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