Function registerClientScript
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
public function registerClientScript()
{
$this->initClientOptions();
$view = $this->getView();
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method registerClientScript
has 46 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function registerClientScript()
{
$this->initClientOptions();
$view = $this->getView();
The method registerClientScript() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10. Open
public function registerClientScript()
{
$this->initClientOptions();
$view = $this->getView();
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed. Open
public function registerClientScript()
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.
See
Class extends undeclared class \yii\widgets\InputWidget
Wontfix
class JsonEditor extends InputWidget
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
- Exclude checks
Checking instanceof against undeclared class \yii\web\JsExpression
Wontfix
if (!$value instanceof JsExpression && in_array($key, $this->jsExpressionClientOptions)) {
- Exclude checks
Possibly zero write references to public property \kdn\yii2\JsonEditor->collapseAll
Wontfix
public $collapseAll = [];
- Exclude checks
Call to method getValue
from undeclared class \yii\helpers\ArrayHelper
Wontfix
$this->$parameterName = ArrayHelper::getValue($this->clientOptions, $parameterName, $this->$parameterName);
- Exclude checks
Call to undeclared method \kdn\yii2\JsonEditor::hasModel
Wontfix
if ($this->hasModel()) {
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
$this->value = $this->options['value'];
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->attribute
Wontfix
$this->value = Html::getAttributeValue($this->model, $this->attribute);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
$this->options['value'] = $this->value; // model may contain decoded JSON, override value for rendering
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
$this->value = $this->defaultValue;
- Exclude checks
Call to method variablize
from undeclared class \yii\helpers\Inflector
Wontfix
$editorName = Inflector::variablize($hiddenInputId) . 'JsonEditor_' . hash('crc32', $hiddenInputId);
- Exclude checks
Call to method htmlEncode
from undeclared class \yii\helpers\Json
Wontfix
$htmlEncodedValue = Json::htmlEncode($this->decodedValue); // Json::htmlEncode is needed to prevent XSS
- Exclude checks
Possibly zero write references to protected property \kdn\yii2\JsonEditor->jsExpressionClientOptions
Wontfix
protected $jsExpressionClientOptions = [
- Exclude checks
Static call to undeclared method \kdn\yii2\assets\JsonEditorMinimalistAsset::register
Wontfix
JsonEditorMinimalistAsset::register($view);
- Exclude checks
Static call to undeclared method \kdn\yii2\assets\JsonEditorFullAsset::register
Wontfix
JsonEditorFullAsset::register($view);
- Exclude checks
Possibly zero write references to private property \kdn\yii2\JsonEditor->modes
Wontfix
private $modes = [];
- Exclude checks
Call to undeclared method \kdn\yii2\JsonEditor::hasModel
Wontfix
if (!$this->issetValue() && $this->hasModel()) {
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
$hiddenInputId = $this->options['id'];
- Exclude checks
Call to method __construct
from undeclared class \yii\web\JsExpression
Wontfix
$this->clientOptions['onModeChange'] = new JsExpression($jsOnModeChange);
- Exclude checks
Possibly zero write references to public property \kdn\yii2\JsonEditor->defaultValue
Wontfix
public $defaultValue = '{}';
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
$this->containerOptions['id'] = $this->options['id'] . '-json-editor';
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
$this->decodedValue = Json::decode($this->value, false);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
return $this->value !== null && $this->value !== '';
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->model
(Did you mean expr->mode or expr->modes) Wontfix
echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
- Exclude checks
Call to method tag
from undeclared class \yii\helpers\Html
Wontfix
echo Html::tag('div', '', $this->containerOptions);
- Exclude checks
Call to method __construct
from undeclared class \yii\web\JsExpression
Wontfix
$this->clientOptions['onChange'] = new JsExpression("function() {{$jsUpdateHiddenField}$userFunction}");
- Exclude checks
Call to method encode
from undeclared class \yii\helpers\Json
Wontfix
$this->value = Json::encode($this->decodedValue);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
$this->options['value'] = $this->value; // model may contain decoded JSON, override value for rendering
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->name
Wontfix
echo Html::hiddenInput($this->name, $this->value, $this->options);
- Exclude checks
Call to method htmlEncode
from undeclared class \yii\helpers\Json
Wontfix
$jsOnModeChange .= "if (" . Json::htmlEncode($this->$property) . ".indexOf(newMode) !== -1) " .
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
$this->value = $this->options['value'];
- Exclude checks
Call to method hiddenInput
from undeclared class \yii\helpers\Html
Wontfix
echo Html::hiddenInput($this->name, $this->value, $this->options);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
echo Html::hiddenInput($this->name, $this->value, $this->options);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
echo Html::hiddenInput($this->name, $this->value, $this->options);
- Exclude checks
Call to method __construct
from undeclared class \yii\web\JsExpression
Wontfix
$options[$key] = new JsExpression($value);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
$this->options['data-json-editor-name'] = $editorName;
- Exclude checks
Call to method htmlEncode
from undeclared class \yii\helpers\Json
Wontfix
Json::htmlEncode($this->clientOptions) . ");\n" .
- Exclude checks
Reference to undeclared class \yii\widgets\InputWidget
Wontfix
parent::init();
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->model
(Did you mean expr->mode or expr->modes) Wontfix
$this->value = Html::getAttributeValue($this->model, $this->attribute);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
$this->value = Json::encode($this->decodedValue);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->value
Wontfix
$this->value = Html::getAttributeValue($this->model, $this->attribute);
- Exclude checks
Call to undeclared method \kdn\yii2\JsonEditor::getView
Wontfix
$view = $this->getView();
- Exclude checks
Possibly zero references to public method \kdn\yii2\JsonEditor::init()
Wontfix
public function init()
- Exclude checks
Call to method getAttributeValue
from undeclared class \yii\helpers\Html
Wontfix
$this->value = Html::getAttributeValue($this->model, $this->attribute);
- Exclude checks
Possibly zero references to public method \kdn\yii2\JsonEditor::run()
Wontfix
public function run()
- Exclude checks
Possibly zero write references to public property \kdn\yii2\JsonEditor->expandAll
Wontfix
public $expandAll = [];
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->options
Wontfix
if (!$this->issetValue() && isset($this->options['value'])) {
- Exclude checks
Call to method decode
from undeclared class \yii\helpers\Json
Wontfix
$this->decodedValue = Json::decode($this->value, false);
- Exclude checks
Call to method activeHiddenInput
from undeclared class \yii\helpers\Html
Wontfix
echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
- Exclude checks
Reference to undeclared property \kdn\yii2\JsonEditor->attribute
Wontfix
echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
- Exclude checks
Define a constant instead of duplicating this literal "onModeChange" 4 times. Open
'onModeChange',
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Define a constant instead of duplicating this literal "value" 3 times. Wontfix
if (!$this->issetValue() && isset($this->options['value'])) {
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Define a constant instead of duplicating this literal "onChange" 4 times. Open
'onChange',
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Avoid excessively long variable names like $jsExpressionClientOptions. Keep variable name length under 20. Open
protected $jsExpressionClientOptions = [
'ace',
'ajv',
'autocomplete',
'createQuery',
- Read upRead up
- Exclude checks
LongVariable
Since: 0.2
Detects when a field, formal or local variable is declared with a long name.
Example
class Something {
protected $reallyLongIntName = -3; // VIOLATION - Field
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
$otherReallyLongName = -5; // VIOLATION - Local
for ($interestingIntIndex = 0; // VIOLATION - For
$interestingIntIndex < 10;
$interestingIntIndex++ ) {
}
}
}