Passed
Push — master ( afdc50...dc7db1 )
by Gabriel
07:13
created

HasHelpersTrait::getHelperClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Nip\View\Traits;
4
5
/**
6
 * Trait HasHelpersTrait
7
 * @package Nip\View\Traits
8
 *
9
 * @method Nip\Helpers\View\Breadcrumbs Breadcrumbs()
10
 * @method Nip\Helpers\View\Doctype Doctype()
11
 * @method Nip\Helpers\View\Flash Flash()
12
 * @method Nip\Helpers\View\FacebookMeta FacebookMeta()
13
 * @method Nip\Helpers\View\GoogleAnalytics GoogleAnalytics()
14
 * @method Nip\Helpers\View\HTML HTML()
15
 * @method Nip\Helpers\View\Messages Messages()
16
 * @method Nip\Helpers\View\Meta Meta()
17
 * @method Nip\Helpers\View\Paginator Paginator()
18
 * @method Nip\Helpers\View\Scripts Scripts()
19
 * @method Nip\Helpers\View\Stylesheets Stylesheets()
20
 * @method Nip\Helpers\View\TinyMCE TinyMCE()
21
 * @method Nip\Helpers\View\Url Url()
22
 * @method Nip\Helpers\View\GoogleDFP GoogleDFP()
23
 */
24
trait HasHelpersTrait
25
{
26
27
    protected $helpers = [];
28
29
    /**
30
     * @param $name
31
     * @return mixed
32
     */
33
    public function getHelper($name)
34
    {
35
        if (!isset($this->helpers[$name])) {
36
            $this->initHelper($name);
37
        }
38
39
        return $this->helpers[$name];
40
    }
41
42
    /**
43
     * @param $name
44
     */
45
    public function initHelper($name)
46
    {
47
        $this->helpers[$name] = $this->newHelper($name);
48
    }
49
50
    /**
51
     * @param $name
52
     * @return Helpers\View\AbstractHelper
0 ignored issues
show
Bug introduced by
The type Nip\View\Traits\Helpers\View\AbstractHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
     */
54
    public function newHelper($name)
55
    {
56
        $class = $this->getHelperClass($name);
57
        $helper = new $class();
58
        /** @var \Nip\Helpers\View\AbstractHelper $helper */
59
        $helper->setView($this);
0 ignored issues
show
Bug introduced by
$this of type Nip\View\Traits\HasHelpersTrait is incompatible with the type Nip\View\ViewInterface expected by parameter $view of Nip\Helpers\View\AbstractHelper::setView(). ( Ignorable by Annotation )

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

59
        $helper->setView(/** @scrutinizer ignore-type */ $this);
Loading history...
60
61
        return $helper;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $helper returns the type Nip\Helpers\View\AbstractHelper which is incompatible with the documented return type Nip\View\Traits\Helpers\View\AbstractHelper.
Loading history...
62
    }
63
64
    /**
65
     * @param $name
66
     * @return string
67
     */
68
    public function getHelperClass($name)
69
    {
70
        return '\Nip\Helpers\View\\' . $name;
71
    }
72
}
73