HookHelper::attachCallable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Helper;
13
14
use Closure;
15
use Jitamin\Foundation\Base;
16
17
/**
18
 * Template Hook helpers.
19
 */
20
class HookHelper extends Base
21
{
22
    /**
23
     * Add assets JS or CSS.
24
     *
25
     * @param string $type
26
     * @param string $hook
27
     *
28
     * @return string
29
     */
30
    public function asset($type, $hook)
31
    {
32
        $buffer = '';
33
34
        foreach ($this->hook->getListeners($hook) as $params) {
0 ignored issues
show
Documentation introduced by
The property hook does not exist on object<Jitamin\Helper\HookHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
            $buffer .= $this->helper->asset->$type($params['template']);
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Helper\HookHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
        }
37
38
        return $buffer;
39
    }
40
41
    /**
42
     * Render all attached hooks.
43
     *
44
     * @param string $hook
45
     * @param array  $variables
46
     *
47
     * @return string
48
     */
49
    public function render($hook, array $variables = [])
50
    {
51
        $buffer = '';
52
53
        foreach ($this->hook->getListeners($hook) as $params) {
0 ignored issues
show
Documentation introduced by
The property hook does not exist on object<Jitamin\Helper\HookHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
            if (!empty($params['variables'])) {
55
                $variables = array_merge($variables, $params['variables']);
56
            } elseif (!empty($params['callable'])) {
57
                $result = call_user_func_array($params['callable'], $variables);
58
59
                if (is_array($result)) {
60
                    $variables = array_merge($variables, $result);
61
                }
62
            }
63
64
            $buffer .= $this->template->render($params['template'], $variables);
0 ignored issues
show
Documentation introduced by
The property template does not exist on object<Jitamin\Helper\HookHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
        }
66
67
        return $buffer;
68
    }
69
70
    /**
71
     * Attach a template to a hook.
72
     *
73
     * @param string $hook
74
     * @param string $template
75
     * @param array  $variables
76
     *
77
     * @return $this
78
     */
79
    public function attach($hook, $template, array $variables = [])
80
    {
81
        $this->hook->on($hook, [
0 ignored issues
show
Documentation introduced by
The property hook does not exist on object<Jitamin\Helper\HookHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
            'template'  => $template,
83
            'variables' => $variables,
84
        ]);
85
86
        return $this;
87
    }
88
89
    /**
90
     * Attach a template to a hook with a callable.
91
     *
92
     * Arguments passed to the callback are the one passed to the hook
93
     *
94
     * @param string  $hook
95
     * @param string  $template
96
     * @param Closure $callable
97
     *
98
     * @return $this
99
     */
100
    public function attachCallable($hook, $template, Closure $callable)
101
    {
102
        $this->hook->on($hook, [
0 ignored issues
show
Documentation introduced by
The property hook does not exist on object<Jitamin\Helper\HookHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
            'template' => $template,
104
            'callable' => $callable,
105
        ]);
106
107
        return $this;
108
    }
109
}
110