Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

@@ 626-701 (lines=76) @@
623
     *
624
     * @return string|string[]|null
625
     */
626
    public function val($value = null)
627
    {
628
        if ($value === null) {
629
            if (
630
                $this->tag === 'input'
631
                &&
632
                (
633
                    $this->getAttribute('type') === 'text'
634
                    ||
635
                    !$this->hasAttribute('type')
636
                )
637
            ) {
638
                return $this->getAttribute('value');
639
            }
640
641
            if (
642
                $this->hasAttribute('checked')
643
                &&
644
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
645
            ) {
646
                return $this->getAttribute('value');
647
            }
648
649
            if ($this->node->nodeName === 'select') {
650
                $valuesFromDom = [];
651
                $options = $this->getElementsByTagName('option');
652
                if ($options instanceof SimpleXmlDomNode) {
653
                    foreach ($options as $option) {
654
                        if ($this->hasAttribute('checked')) {
655
                            /** @noinspection UnnecessaryCastingInspection */
656
                            $valuesFromDom[] = (string) $option->getAttribute('value');
657
                        }
658
                    }
659
                }
660
661
                if (\count($valuesFromDom) === 0) {
662
                    return null;
663
                }
664
665
                return $valuesFromDom;
666
            }
667
668
            if ($this->node->nodeName === 'textarea') {
669
                return $this->node->nodeValue;
670
            }
671
        } else {
672
            /** @noinspection NestedPositiveIfStatementsInspection */
673
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
674
                if ($value === $this->getAttribute('value')) {
675
                    /** @noinspection UnusedFunctionResultInspection */
676
                    $this->setAttribute('checked', 'checked');
677
                } else {
678
                    /** @noinspection UnusedFunctionResultInspection */
679
                    $this->removeAttribute('checked');
680
                }
681
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
682
                foreach ($this->node->getElementsByTagName('option') as $option) {
683
                    /** @var \DOMElement $option */
684
                    if ($value === $option->getAttribute('value')) {
685
                        /** @noinspection UnusedFunctionResultInspection */
686
                        $option->setAttribute('selected', 'selected');
687
                    } else {
688
                        $option->removeAttribute('selected');
689
                    }
690
                }
691
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
692
                // Set value for input elements
693
                /** @noinspection UnusedFunctionResultInspection */
694
                $this->setAttribute('value', $value);
695
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
696
                $this->node->nodeValue = $value;
697
            }
698
        }
699
700
        return null;
701
    }
702
703
    /**
704
     * Retrieve an external iterator.