Passed
Push — master ( 90a125...a845e6 )
by Caen
03:36 queued 13s
created

MarkdownDocument::toArray()   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\Contracts\MarkdownDocumentContract;
6
use Hyde\Framework\Facades\Markdown;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Modules\Markdown\MarkdownFileParser;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest
14
 */
15
class MarkdownDocument implements MarkdownDocumentContract, Arrayable
16
{
17
    public array $matter;
18
    public string $body;
19
20
    public function __construct(array $matter = [], string $body = '')
21
    {
22
        $this->matter = $matter;
23
        $this->body = $body;
24
    }
25
26
    public function __toString(): string
27
    {
28
        return $this->body;
29
    }
30
31
    public function __get(string $key): mixed
32
    {
33
        return $this->matter($key);
34
    }
35
36
    public function matter(string $key = null, mixed $default = null): mixed
37
    {
38
        if ($key) {
39
            return Arr::get($this->matter, $key, $default);
40
        }
41
42
        return $this->matter;
43
    }
44
45
    public function body(): string
46
    {
47
        return $this->body;
48
    }
49
50
    public function render(): string
51
    {
52
        return Markdown::parse($this->body);
53
    }
54
55
    /**
56
     * Return the Markdown document body explored by line into an array.
57
     */
58
    public function toArray(): array
59
    {
60
        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...
61
    }
62
63
    /**
64
     * @deprecated v0.56.0 - Will be renamed to parse()
65
     */
66
    public static function parseFile(string $localFilepath): static
67
    {
68
        return (new MarkdownFileParser(Hyde::path($localFilepath)))->get();
69
    }
70
}
71