LinkBehavior::getLinkText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @author Brett O'Donnell <[email protected]>
4
 * @copyright 2016 Mr PHP
5
 * @link https://github.com/cornernote/yii2-link-behavior
6
 * @license BSD-3-Clause https://raw.github.com/cornernote/yii2-link-behavior/master/LICENSE.md
7
 */
8
9
namespace cornernote\linkbehavior;
10
11
use Yii;
12
use yii\base\Behavior;
13
use yii\base\Event;
14
use yii\db\ActiveRecord;
15
use yii\db\BaseActiveRecord;
16
use yii\helpers\ArrayHelper;
17
use yii\helpers\Html;
18
use yii\helpers\Inflector;
19
use yii\helpers\StringHelper;
20
21
/**
22
 * LinkBehavior
23
 *
24
 * @usage:
25
 * ```
26
 * public function behaviors() {
27
 *     return [
28
 *         [
29
 *             'class' => 'cornernote\linkbehavior\LinkBehavior',
30
 *         ],
31
 *     ];
32
 * }
33
 * ```
34
 *
35
 * @property string|array $url
36
 * @property string $link
37
 * @property string $linkText
38
 * @property string $controllerName
39
 * @property string $primaryKeyString
40
 *
41
 * @property ActiveRecord|LinkBehavior $owner
42
 */
43
class LinkBehavior extends Behavior
44
{
45
46
    /**
47
     * @var string The name of default action for the model, usually view
48
     */
49
    public $defaultAction = 'view';
50
51
    /**
52
     * @var string The name of the controller to be used in links
53
     */
54
    public $moduleName;
55
56
    /**
57
     * @var string The name of the controller to be used in links
58
     */
59
    private $_controllerName;
60
61
    /**
62
     * Gets the name of the controller to be used in links
63
     *
64
     * @return string
65
     */
66
    public function getControllerName()
67
    {
68
        if ($this->_controllerName)
69
            return $this->_controllerName;
70
        return $this->_controllerName = Inflector::slug(Inflector::camel2words(StringHelper::basename($this->owner->className())));
71
    }
72
73
    /**
74
     * Sets the name of the controller to be used in links
75
     *
76
     * @param $controllerName
77
     */
78
    public function setControllerName($controllerName)
79
    {
80
        $this->_controllerName = $controllerName;
81
    }
82
83
    /**
84
     * The name of this model to be used in titles
85
     *
86
     * @return string
87
     */
88
    public function getLinkText()
89
    {
90
        return $this->owner->className() . '-' . $this->owner->getPrimaryKeyString();
0 ignored issues
show
Bug introduced by
The method getPrimaryKeyString does only exist in cornernote\linkbehavior\LinkBehavior, but not in yii\db\ActiveRecord.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
91
    }
92
93
    /**
94
     * Returns a URL Array to the model
95
     *
96
     * @param string $action
97
     * @param array $params
98
     * @return array
99
     */
100
    public function getUrl($action = null, $params = [])
101
    {
102
        if (!$action)
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
103
            $action = $this->defaultAction;
104
        return ArrayHelper::merge([
105
            '/' . ($this->owner->moduleName ? $this->owner->moduleName . '/' : '') . $this->owner->getControllerName() . '/' . $action,
0 ignored issues
show
Bug introduced by
The method getControllerName does only exist in cornernote\linkbehavior\LinkBehavior, but not in yii\db\ActiveRecord.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
            'id' => $this->owner->getPrimaryKeyString(),
0 ignored issues
show
Bug introduced by
The method getPrimaryKeyString does only exist in cornernote\linkbehavior\LinkBehavior, but not in yii\db\ActiveRecord.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
107
        ], $params);
108
    }
109
110
    /**
111
     * Returns a Link to the model
112
     *
113
     * @param string $title
114
     * @param string $action
115
     * @param array $params
116
     * @param array $options
117
     * @return string
118
     */
119
    public function getLink($title = null, $action = null, $params = [], $options = [])
120
    {
121
        if ($title === null)
122
            $title = $this->owner->linkText;
123
        return Html::a($title, $this->owner->getUrl($action, $params), $options);
0 ignored issues
show
Bug introduced by
The method getUrl does only exist in cornernote\linkbehavior\LinkBehavior, but not in yii\db\ActiveRecord.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
    }
125
126
    /**
127
     * Returns Primary Key as a string
128
     *
129
     * @return string
130
     */
131
    public function getPrimaryKeyString()
132
    {
133
        if (is_array($this->owner->primaryKey))
134
            return implode('-', $this->owner->primaryKey);
135
        return $this->owner->primaryKey;
136
    }
137
138
}
139