Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

@@ 649-724 (lines=76) @@
646
     *
647
     * @return string|string[]|null
648
     */
649
    public function val($value = null)
650
    {
651
        if ($value === null) {
652
            if (
653
                $this->tag === 'input'
654
                &&
655
                (
656
                    $this->getAttribute('type') === 'text'
657
                    ||
658
                    !$this->hasAttribute('type')
659
                )
660
            ) {
661
                return $this->getAttribute('value');
662
            }
663
664
            if (
665
                $this->hasAttribute('checked')
666
                &&
667
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
668
            ) {
669
                return $this->getAttribute('value');
670
            }
671
672
            if ($this->node->nodeName === 'select') {
673
                $valuesFromDom = [];
674
                $options = $this->getElementsByTagName('option');
675
                if ($options instanceof SimpleXmlDomNode) {
676
                    foreach ($options as $option) {
677
                        if ($this->hasAttribute('checked')) {
678
                            /** @noinspection UnnecessaryCastingInspection */
679
                            $valuesFromDom[] = (string) $option->getAttribute('value');
680
                        }
681
                    }
682
                }
683
684
                if (\count($valuesFromDom) === 0) {
685
                    return null;
686
                }
687
688
                return $valuesFromDom;
689
            }
690
691
            if ($this->node->nodeName === 'textarea') {
692
                return $this->node->nodeValue;
693
            }
694
        } else {
695
            /** @noinspection NestedPositiveIfStatementsInspection */
696
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
697
                if ($value === $this->getAttribute('value')) {
698
                    /** @noinspection UnusedFunctionResultInspection */
699
                    $this->setAttribute('checked', 'checked');
700
                } else {
701
                    /** @noinspection UnusedFunctionResultInspection */
702
                    $this->removeAttribute('checked');
703
                }
704
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
705
                foreach ($this->node->getElementsByTagName('option') as $option) {
706
                    /** @var \DOMElement $option */
707
                    if ($value === $option->getAttribute('value')) {
708
                        /** @noinspection UnusedFunctionResultInspection */
709
                        $option->setAttribute('selected', 'selected');
710
                    } else {
711
                        $option->removeAttribute('selected');
712
                    }
713
                }
714
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
715
                // Set value for input elements
716
                /** @noinspection UnusedFunctionResultInspection */
717
                $this->setAttribute('value', $value);
718
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
719
                $this->node->nodeValue = $value;
720
            }
721
        }
722
723
        return null;
724
    }
725
726
    /**
727
     * Retrieve an external iterator.