Completed
Branch master (a1edd4)
by
unknown
54:09
created

InputWidget   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 11 3
A prepareTemplate() 0 6 1
A renderTemplate() 0 4 1
1
<?php
2
/**
3
 * This file is part of the fangface/yii2-concord package
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 *
8
 * @package fangface/yii2-concord
9
 * @author Fangface <[email protected]>
10
 * @copyright Copyright (c) 2014 Fangface <[email protected]>
11
 * @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License
12
 *
13
 */
14
15
namespace fangface\widgets;
16
17
use fangface\base\traits\InputAddonTrait;
18
use fangface\forms\ActiveForm;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, fangface\widgets\ActiveForm.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use fangface\forms\ActiveField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, fangface\widgets\ActiveField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
use fangface\widgets\WidgetTrait;
21
use yii\widgets\ActiveField as YiiActiveField;
22
use yii\widgets\InputWidget as YiiInputWidget;
23
24
25
/**
26
 * Form Builder
27
 */
28
class InputWidget extends YiiInputWidget
29
{
30
    use WidgetTrait;
31
    use InputAddonTrait;
32
33
    /**
34
     * @var ActiveForm
35
     */
36
    public $form;
37
    /**
38
     * @var boolean is the input disabled, can be used to prevent registered assets for example
39
     */
40
    public $disabled = false;
41
    /**
42
     * @var array the HTML attributes for the input tag.
43
     */
44
    public $options = ['class' => 'form-control'];
45
46
47
    /**
48
     * {@inheritDoc}
49
     * @see \yii\base\Widget::run()
50
     */
51
    public function run()
52
    {
53
        if (isset($this->options['disabled'])) {
54
            $this->disabled = true;
55
        }
56
        $this->applyPluginSettings();
57
        parent::run();
58
        if (method_exists($this, 'renderWidget')) {
59
            $this->renderWidget();
0 ignored issues
show
Documentation Bug introduced by
The method renderWidget does not exist on object<fangface\widgets\InputWidget>? 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...
60
        }
61
    }
62
63
    /**
64
     * Prepare the template
65
     */
66
    public function prepareTemplate()
67
    {
68
        $this->template = strtr($this->template, [
69
            '{input}' => $this->generateAddon(),
70
        ]);
71
    }
72
73
    /**
74
     * Renders all sections using the current template [[template]].
75
     * @return string the full rendering result
76
     */
77
    protected function renderTemplate()
78
    {
79
        return $this->findSectionsToRender($this->template);
80
    }
81
82
}