Code Duplication    Length = 76-76 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

@@ 602-677 (lines=76) @@
599
     *
600
     * @return string|string[]|null
601
     */
602
    public function val($value = null)
603
    {
604
        if ($value === null) {
605
            if (
606
                $this->tag === 'input'
607
                &&
608
                (
609
                    $this->getAttribute('type') === 'text'
610
                    ||
611
                    !$this->hasAttribute('type')
612
                )
613
            ) {
614
                return $this->getAttribute('value');
615
            }
616
617
            if (
618
                $this->hasAttribute('checked')
619
                &&
620
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
621
            ) {
622
                return $this->getAttribute('value');
623
            }
624
625
            if ($this->node->nodeName === 'select') {
626
                $valuesFromDom = [];
627
                $options = $this->getElementsByTagName('option');
628
                if ($options instanceof SimpleXmlDomNode) {
629
                    foreach ($options as $option) {
630
                        if ($this->hasAttribute('checked')) {
631
                            /** @noinspection UnnecessaryCastingInspection */
632
                            $valuesFromDom[] = (string) $option->getAttribute('value');
633
                        }
634
                    }
635
                }
636
637
                if (\count($valuesFromDom) === 0) {
638
                    return null;
639
                }
640
641
                return $valuesFromDom;
642
            }
643
644
            if ($this->node->nodeName === 'textarea') {
645
                return $this->node->nodeValue;
646
            }
647
        } else {
648
            /** @noinspection NestedPositiveIfStatementsInspection */
649
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
650
                if ($value === $this->getAttribute('value')) {
651
                    /** @noinspection UnusedFunctionResultInspection */
652
                    $this->setAttribute('checked', 'checked');
653
                } else {
654
                    /** @noinspection UnusedFunctionResultInspection */
655
                    $this->removeAttribute('checked');
656
                }
657
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
658
                foreach ($this->node->getElementsByTagName('option') as $option) {
659
                    /** @var \DOMElement $option */
660
                    if ($value === $option->getAttribute('value')) {
661
                        /** @noinspection UnusedFunctionResultInspection */
662
                        $option->setAttribute('selected', 'selected');
663
                    } else {
664
                        $option->removeAttribute('selected');
665
                    }
666
                }
667
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
668
                // Set value for input elements
669
                /** @noinspection UnusedFunctionResultInspection */
670
                $this->setAttribute('value', $value);
671
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
672
                $this->node->nodeValue = $value;
673
            }
674
        }
675
676
        return null;
677
    }
678
679
    /**
680
     * Retrieve an external iterator.