Completed
Push — master ( 39620e...85b6d0 )
by Klochok
05:09
created

AjaxModalWithTemplatedButton::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace hipanel\widgets;
4
5
use yii\helpers\ArrayHelper;
6
use ReflectionClass;
7
use yii\base\Widget;
8
use yii\web\View;
9
10
/**
11
 * Class AjaxModalWithTemplatedButton render AjaxModal markup to the end of body and render toggle button in any
12
 * place of html with additional toggle button template.
13
 */
14
class AjaxModalWithTemplatedButton extends Widget
15
{
16
    /**
17
     * @var array
18
     */
19
    public $ajaxModalOptions;
20
21
    /**
22
     * @var string the template used to render a toggle button.
23
     */
24
    public $toggleButtonTemplate = "{toggleButton}";
25
26
    /**
27
     * @var array
28
     */
29
    private $toggleButtonOptions;
30
31
    public function init()
32
    {
33
        $this->toggleButtonOptions = ArrayHelper::remove($this->ajaxModalOptions, 'toggleButton');
0 ignored issues
show
Documentation Bug introduced by
It seems like \yii\helpers\ArrayHelper...ptions, 'toggleButton') of type * is incompatible with the declared type array of property $toggleButtonOptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        $this->ajaxModalOptions['toggleButton'] = false;
35
        $this->view->on(View::EVENT_END_BODY, function ($event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
            echo AjaxModal::widget($this->ajaxModalOptions);
37
        });
38
    }
39
40
    public function run()
41
    {
42
        $id = $this->ajaxModalOptions['id'];
43
        $toggleButton = function () use ($id) {
44
            $this->options['id'] = $id;
0 ignored issues
show
Bug introduced by
The property options does not seem to exist. Did you mean ajaxModalOptions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
45
            $this->initOptions();
0 ignored issues
show
Documentation Bug introduced by
The method initOptions does not exist on object<hipanel\widgets\A...dalWithTemplatedButton>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
46
47
            return $this->renderToggleButton();
0 ignored issues
show
Documentation Bug introduced by
The method renderToggleButton does not exist on object<hipanel\widgets\A...dalWithTemplatedButton>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
        };
49
        $obj = (new ReflectionClass(AjaxModal::class))->newInstanceWithoutConstructor();
50
        $obj->toggleButton = $this->toggleButtonOptions;
51
52
        $button = $toggleButton->call($obj);
53
        if ($button) {
54
            $button = strtr($this->toggleButtonTemplate, [
55
                '{toggleButton}' => $button,
56
            ]);
57
        }
58
59
        return $button;
60
    }
61
}
62