FrontMatterLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This is part of the webuni/front-matter package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\FrontMatter\Twig;
14
15
use Twig\Loader\LoaderInterface;
16
use Twig\Source;
17
use Webuni\FrontMatter\FrontMatterInterface;
18
19
class FrontMatterLoader implements LoaderInterface
20
{
21
    /** @var LoaderInterface */
22
    private $loader;
23
24
    /** @var FrontMatterInterface */
25
    private $parser;
26
27
    /** @var DataToTwigConvertor */
28
    private $convertor;
29
30
    public function __construct(FrontMatterInterface $parser, LoaderInterface $loader, DataToTwigConvertor $convertor = null)
31
    {
32
        $this->loader = $loader;
33
        $this->parser = $parser;
34
        $this->convertor = $convertor ?? DataToTwigConvertor::nothing();
0 ignored issues
show
Documentation Bug introduced by
It seems like $convertor ?? \Webuni\Fr...wigConvertor::nothing() can also be of type object<self>. However, the property $convertor is declared as type object<Webuni\FrontMatte...ig\DataToTwigConvertor>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35
    }
36
37
    public function getCacheKey(string $name): string
38
    {
39
        return $this->loader->getCacheKey($name);
40
    }
41
42
    public function isFresh(string $name, int $time): bool
43
    {
44
        return $this->loader->isFresh($name, $time);
45
    }
46
47
    public function exists(string $name): bool
48
    {
49
        return $this->loader->exists($name);
50
    }
51
52
    public function getSourceContext(string $name): Source
53
    {
54
        $source = $this->loader->getSourceContext($name);
55
        $code = $source->getCode();
56
        $document = $this->parser->parse($code);
57
        $content = $document->getContent();
58
59
        if ($code === $content) {
60
            return $source;
61
        }
62
63
        $content = $this->convertor->convert($document->getData()).$content;
64
65
        $lines = substr_count($code, "\n", 0, - strlen($content));
66
        $content = "{% line $lines %}\n$content";
67
68
        return new Source($content, $source->getName(), $source->getPath());
69
    }
70
}
71