TocRule::createToc()   C
last analyzed

Complexity

Conditions 8
Paths 52

Size

Total Lines 63
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 48
cts 48
cp 1
rs 6.8825
c 0
b 0
f 0
cc 8
eloc 41
nc 52
nop 0
crap 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaloa\Renderer\Xml\Rule;
4
5
use Kaloa\Renderer\Xml\Rule\AbstractRule;
6
7
/**
8
 *
9
 */
10
final class TocRule extends AbstractRule
11
{
12
    private $config;
13
14
    /**
15
     *
16
     * @param array $config
17
     */
18 1
    public function __construct(array $config = array())
19
    {
20
        $configDefault = array(
21
            'idFormat' => 'h:%s'
22 1
        );
23
24 1
        $this->config = array_merge($configDefault, $config);
25 1
    }
26
27
    /**
28
     *
29
     */
30 1
    public function preRender()
31
    {
32 1
        if (count($this->runXpathQuery('//k:toc')) > 0) {
33 1
            $this->createToc();
34 1
        }
35 1
    }
36
37
    /**
38
     *
39
     */
40 1
    private function createToc()
41
    {
42 1
        $curDepth = 2;
43 1
        $i = 0;
44 1
        $toc = '';
45
46 1
        $toc .= "<ul>\n";
47 1
        foreach ($this->runXpathQuery('//h2|//h3|//h4|//h5|//h6') as $node) {
48 1
            $thisDepth = (int) substr($node->nodeName, 1);
49
50 1
            $identifier = (string) $node->getAttribute('id');
51
52 1
            if ('' === $identifier) {
53
                // No id set, generate a reasonable one
54 1
                $tmp = $node->nodeValue;
55 1
                $tmp = mb_strtolower($tmp);
56 1
                $tmp = str_replace(' ', '-', $tmp);
57 1
                $tmp = preg_replace('/[^\p{L}0-9-]/u', '', $tmp);
58 1
                $identifier = mb_substr($tmp, 0, 30);
59 1
                $identifier = rtrim($identifier, '-');
60 1
            }
61
62 1
            $identifierFormatted = sprintf(
63 1
                $this->config['idFormat'],
64
                $identifier
65 1
            );
66
67 1
            $node->setAttribute('id', $identifierFormatted);
68
69 1
            if ($thisDepth > $curDepth) {
70 1
                $toc .= "\n" . str_repeat('  ', $thisDepth - 1) . "<ul>\n";
71 1
            } elseif ($i > 0) {
72 1
                $toc .= "</li>\n";
73 1
            }
74
75 1
            while ($thisDepth < $curDepth) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 1
                $toc .= str_repeat('  ', $curDepth - 1) . "</ul>\n</li>\n";
77 1
                $curDepth--;
78 1
            }
79
80 1
            $toc .= str_repeat('  ', $thisDepth) . '<li>';
81 1
            $toc .= '<a href="#' . $identifierFormatted . '">';
82 1
            $toc .= $this->getInnerXml($node);
83 1
            $toc .= '</a>';
84
85 1
            $curDepth = $thisDepth;
86 1
            $i++;
87 1
        }
88
89 1
        $toc .= "</li>\n</ul>\n";
90 1
        while ($curDepth > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91 1
            $toc .= str_repeat('  ', $curDepth - 1) . "</li>\n</ul>\n";
92 1
            $curDepth--;
93 1
        }
94
95 1
        $tocFragment = $this->getDocument()->createDocumentFragment();
96 1
        $tocFragment->appendXML($toc);
97
98 1
        foreach ($this->runXpathQuery('//k:toc') as $node) {
99 1
            $parent = $node->parentNode;
100 1
            $parent->replaceChild($tocFragment->cloneNode(true), $node);
101 1
        }
102 1
    }
103
}
104