Passed
Push — master ( ef5451...9c1fbd )
by Caen
03:43 queued 12s
created

Markdown::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Modules\Markdown\MarkdownConverter;
6
use Hyde\Framework\Services\MarkdownService;
7
use Illuminate\Contracts\Support\Arrayable;
8
9
/**
10
 * A simple object representation of a Markdown file, with helpful methods to interact with it.
11
 *
12
 * @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest
13
 */
14
class Markdown implements Arrayable
15
{
16
    public string $body;
17
18
    public function __construct(string $body = '')
19
    {
20
        $this->body = $body;
21
    }
22
23
    public static function fromFile(string $localFilepath): static
24
    {
25
        return MarkdownDocument::parseFile($localFilepath)->markdown();
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Models\Ma...wnDocument::parseFile() has been deprecated: v0.56.0 - Use static::parse() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
        return /** @scrutinizer ignore-deprecated */ MarkdownDocument::parseFile($localFilepath)->markdown();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
26
    }
27
28
    public function compile(): string
29
    {
30
        return static::render($this->body);
31
    }
32
33
    public function __toString(): string
34
    {
35
        return $this->body;
36
    }
37
38
    /**
39
     * Return the Markdown document body explored by line into an array.
40
     *
41
     * @return string[]
42
     */
43
    public function toArray(): array
44
    {
45
        return explode("\n", $this->body);
0 ignored issues
show
Bug Best Practice introduced by
The expression return explode(' ', $this->body) returns the type string[] which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of Illuminate\Contracts\Support\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
46
    }
47
48
    public function body(): string
49
    {
50
        return $this->body;
51
    }
52
53
    /**
54
     * Render a Markdown string into HTML.
55
     *
56
     * If a source model is provided, the Markdown will be converted using the dynamic MarkdownService,
57
     * otherwise, the pre-configured singleton from the service container will be used instead.
58
     *
59
     * @return string $html
60
     */
61
    public static function render(string $markdown, ?string $sourceModel = null): string
62
    {
63
        return $sourceModel !== null
64
            ? (new MarkdownService($markdown, $sourceModel))->parse()
65
            : app(MarkdownConverter::class)->convert($markdown);
66
    }
67
}
68