Passed
Push — master ( ea6259...fd9feb )
by Caen
03:52 queued 11s
created

GeneratesSidebarTableOfContents   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 26 1
A __construct() 0 3 1
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Contracts\ActionContract;
6
use League\CommonMark\Environment\Environment;
7
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
8
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
9
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
10
use League\CommonMark\MarkdownConverter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Hyde\Framework\Actions\MarkdownConverter. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
12
/**
13
 * Generates a table of contents for the Markdown document.
14
 *
15
 * @see \Hyde\Framework\Testing\Feature\Actions\GeneratesSidebarTableOfContentsTest
16
 */
17
class GeneratesSidebarTableOfContents implements ActionContract
18
{
19
    protected string $markdown;
20
21
    public function __construct(string $markdown)
22
    {
23
        $this->markdown = $markdown;
24
    }
25
26
    public function execute(): string
27
    {
28
        $config = [
29
            'table_of_contents' => [
30
                'html_class' => 'table-of-contents',
31
                'position' => 'top',
32
                'style' => 'bullet',
33
                'min_heading_level' => config('docs.table_of_contents.min_heading_level', 2),
34
                'max_heading_level' => config('docs.table_of_contents.max_heading_level', 4),
35
                'normalize' => 'relative',
36
            ],
37
            'heading_permalink' => [
38
                'fragment_prefix' => '',
39
            ],
40
        ];
41
42
        $environment = new Environment($config);
43
        $environment->addExtension(new CommonMarkCoreExtension());
44
        $environment->addExtension(new HeadingPermalinkExtension());
45
        $environment->addExtension(new TableOfContentsExtension());
46
47
        $converter = new MarkdownConverter($environment);
48
        $html = $converter->convert("[[END_TOC]]\n".$this->markdown)->getContent();
49
50
        // Return everything before the [[END_TOC]] marker.
51
        return substr($html, 0, strpos($html, '<p>[[END_TOC]]'));
52
    }
53
}
54