Karaka-Management / phpOMS

PHP Framework
Other
2 stars 0 forks source link

Consider to replace recursive Markdown functions with while loop #360

Closed spl1nes closed 4 months ago

spl1nes commented 7 months ago

File: phpOMS/Utils/Parser/Markdown/Markdown.php

Consider to replace the recursion of the following two function with a while loop:

protected function elementApplyRecursive(string|\Closure $closure, array $element) : array
    {
        $element = \is_string($closure) ? $this->{$closure}($element) : $closure($element);

        if (isset($element['elements'])) {
            foreach ($element['elements'] as &$e) {
                $e = $this->elementApplyRecursive($closure, $e);
            }
        } elseif (isset($element['element'])) {
            $element['element'] = $this->elementApplyRecursive($closure, $element['element']);
        }

        return $element;
    }

    protected function elementApplyRecursiveDepthFirst(string|\Closure $closure, array $element) : array
    {
        if (isset($element['elements'])) {
            foreach ($element['elements'] as &$e) {
                $e = $this->elementApplyRecursiveDepthFirst($closure, $e);
            }
        } elseif (isset($element['element'])) {
            // @bug is this loop even correct? See function `elementApplyRecursive()` where we don't loop
            foreach ($element['element'] as &$e) {
                $e = $this->elementApplyRecursiveDepthFirst($closure, $e);
            }
        }

        return \is_string($closure) ? $this->{$closure}($element) : $closure($element);
    }
spl1nes commented 4 months ago

Couldn't remove the second function with a simple iterative version due to the double recursion.

However, the first function was not used at all and therefor removed.