Passed
Branch master (ef5451)
by Caen
03:29
created

Markdown::render()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Facades\Markdown as MarkdownFacade;
6
use Illuminate\Contracts\Support\Arrayable;
7
8
/**
9
 * A simple object representation of a Markdown file, with helpful methods to interact with it.
10
 *
11
 * @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest
12
 */
13
class Markdown implements Arrayable
14
{
15
    public string $body;
16
17
    public function __construct(string $body = '')
18
    {
19
        $this->body = $body;
20
    }
21
22
    public static function fromFile(string $localFilepath): static
23
    {
24
        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

24
        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...
25
    }
26
27
    public function render(): string
28
    {
29
        return MarkdownFacade::render($this->body);
30
    }
31
32
    public function __toString(): string
33
    {
34
        return $this->body;
35
    }
36
37
    /**
38
     * Return the Markdown document body explored by line into an array.
39
     *
40
     * @return string[]
41
     */
42
    public function toArray(): array
43
    {
44
        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...
45
    }
46
47
    public function body(): string
48
    {
49
        return $this->body;
50
    }
51
}
52