Test Failed
Push — master ( ed648f...76cdf4 )
by Gabriel
10:26
created

RenderConditionsExtension::loadIfExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\View\Extensions\RenderConditions;
6
7
use League\Plates\Engine;
8
use Nip\View\Extensions\AbstractExtension;
9
use Nip\View\View;
10
use function is_callable;
11
12
class RenderConditionsExtension extends AbstractExtension
13
{
14
    public function register(View|Engine $engine)
15
    {
16
        parent::register($engine);
17
        $engine->addMethod('loadIfExists', [$this, 'loadIfExists']);
0 ignored issues
show
introduced by
The method addMethod() does not exist on League\Plates\Engine. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

17
        $engine->/** @scrutinizer ignore-call */ 
18
                 addMethod('loadIfExists', [$this, 'loadIfExists']);
Loading history...
18
        $engine->addMethod('loadIf', [$this, 'loadIf']);
19
        $engine->addMethod('loadWithFallback', [$this, 'loadWithFallback']);
20
    }
21
22
    /**
23
     * @param array $variables
24
     * @param bool $return
25
     *
26
     * @return bool|string|void|null
27
     */
28
    public function loadIfExists($view, $variables = [], $return = false)
29
    {
30
        return $this->loadIf($this->existPath($view), $view, $variables, $return);
0 ignored issues
show
Unused Code introduced by
The call to Nip\View\Extensions\Rend...ionsExtension::loadIf() has too many arguments starting with $return. ( Ignorable by Annotation )

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

30
        return $this->/** @scrutinizer ignore-call */ loadIf($this->existPath($view), $view, $variables, $return);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
    }
32
33
    /**
34
     * @param bool|callable $condition
35
     * @param array $variables
36
     *
37
     * @return bool|string|void|null
38
     */
39
    public function loadIf($condition, $view, $variables = [])
40
    {
41
        $condition = is_callable($condition) ? $condition() : $condition;
42
        if (false == $condition) {
43
            return;
44
        }
45
46
        return $this->template->fetch($view, $variables);
47
    }
48
49
    protected function existPath($view): bool
50
    {
51
        return $this->engine->existPath($view);
52
    }
53
54
    /**
55
     * @param array $variables
56
     * @param bool $return
57
     *
58
     * @return bool|string|void|null
59
     */
60
    public function loadWithFallback($view, $fallback, $variables = [], $return = false)
61
    {
62
        $view = $this->existPath($view) ? $view : $fallback;
63
64
        return $this->template->fetch($view, $variables, $return);
0 ignored issues
show
Unused Code introduced by
The call to League\Plates\Template\Template::fetch() has too many arguments starting with $return. ( Ignorable by Annotation )

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

64
        return $this->template->/** @scrutinizer ignore-call */ fetch($view, $variables, $return);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
65
    }
66
}
67