SearchWidget::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace widgets\search;
9
10
use common\components\BaseWidget;
11
use common\models\Option;
12
use Yii;
13
14
/**
15
 * Class Search
16
 *
17
 * @author Agiel K. Saputra <[email protected]>
18
 * @since 0.1.1
19
 */
20
class SearchWidget extends BaseWidget
21
{
22
    /**
23
     * @var string Path of the search form. If there is a file search-form.php in directory 'layouts' of active theme,
24
     * the widget uses it, but if not, the widget uses search-form.php that exist in the directory 'views'.
25
     */
26
    private $_searchForm;
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function init()
32
    {
33
        $theme = Option::get('theme');
34
        $searchForm = Yii::getAlias('@themes/' . $theme . '/layouts/search-form.php');
35
36
        if (is_file($searchForm)) {
37
            $this->_searchForm = $searchForm;
0 ignored issues
show
Documentation Bug introduced by
It seems like $searchForm can also be of type boolean. However, the property $_searchForm is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
        } else {
39
            $this->_searchForm = __DIR__ . '/views/search-form.php';
40
        }
41
42
        parent::init();
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function run()
49
    {
50
        echo $this->beforeWidget;
51
52
        if ($this->title) {
53
            echo $this->beforeTitle . $this->title . $this->afterTitle;
54
        }
55
56
        echo Yii::$app->view->renderFile($this->_searchForm);
57
        echo $this->afterWidget;
58
    }
59
}
60