dmitry-kulikov/yii2-json-editor

View on GitHub
src/JsonEditor.php

Summary

Maintainability
A
3 hrs
Test Coverage
A
100%

Function registerClientScript has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

    public function registerClientScript()
    {
        $this->initClientOptions();
        $view = $this->getView();

Severity: Minor
Found in src/JsonEditor.php - About 2 hrs to fix

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();

Severity: Minor
Found in src/JsonEditor.php - About 1 hr to fix

    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();
    
    
    Severity: Minor
    Found in src/JsonEditor.php by phpmd

    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()
    Severity: Critical
    Found in src/JsonEditor.php by sonar-php

    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
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

                echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Checking instanceof against undeclared class \yii\web\JsExpression
    Wontfix

                if (!$value instanceof JsExpression && in_array($key, $this->jsExpressionClientOptions)) {
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Possibly zero write references to public property \kdn\yii2\JsonEditor->collapseAll
    Wontfix

        public $collapseAll = [];
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method getValue from undeclared class \yii\helpers\ArrayHelper
    Wontfix

                $this->$parameterName = ArrayHelper::getValue($this->clientOptions, $parameterName, $this->$parameterName);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Call to undeclared method \kdn\yii2\JsonEditor::hasModel
    Wontfix

            if ($this->hasModel()) {
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

                $this->value = $this->options['value'];
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->attribute
    Wontfix

                $this->value = Html::getAttributeValue($this->model, $this->attribute);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

                $this->options['value'] = $this->value; // model may contain decoded JSON, override value for rendering
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

                $this->value = $this->defaultValue;
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method variablize from undeclared class \yii\helpers\Inflector
    Wontfix

            $editorName = Inflector::variablize($hiddenInputId) . 'JsonEditor_' . hash('crc32', $hiddenInputId);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Call to method htmlEncode from undeclared class \yii\helpers\Json
    Wontfix

            $htmlEncodedValue = Json::htmlEncode($this->decodedValue); // Json::htmlEncode is needed to prevent XSS
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Possibly zero write references to protected property \kdn\yii2\JsonEditor->jsExpressionClientOptions
    Wontfix

        protected $jsExpressionClientOptions = [
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Static call to undeclared method \kdn\yii2\assets\JsonEditorMinimalistAsset::register
    Wontfix

                JsonEditorMinimalistAsset::register($view);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Static call to undeclared method \kdn\yii2\assets\JsonEditorFullAsset::register
    Wontfix

                JsonEditorFullAsset::register($view);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Possibly zero write references to private property \kdn\yii2\JsonEditor->modes
    Wontfix

        private $modes = [];
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to undeclared method \kdn\yii2\JsonEditor::hasModel
    Wontfix

            if (!$this->issetValue() && $this->hasModel()) {
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

            $hiddenInputId = $this->options['id'];
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method __construct from undeclared class \yii\web\JsExpression
    Wontfix

                $this->clientOptions['onModeChange'] = new JsExpression($jsOnModeChange);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Possibly zero write references to public property \kdn\yii2\JsonEditor->defaultValue
    Wontfix

        public $defaultValue = '{}';
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

                $this->containerOptions['id'] = $this->options['id'] . '-json-editor';
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

            $this->decodedValue = Json::decode($this->value, false);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

            return $this->value !== null && $this->value !== '';
    Severity: Minor
    Found in src/JsonEditor.php by phan

    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);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method tag from undeclared class \yii\helpers\Html
    Wontfix

            echo Html::tag('div', '', $this->containerOptions);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Call to method __construct from undeclared class \yii\web\JsExpression
    Wontfix

            $this->clientOptions['onChange'] = new JsExpression("function() {{$jsUpdateHiddenField}$userFunction}");
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Call to method encode from undeclared class \yii\helpers\Json
    Wontfix

                $this->value = Json::encode($this->decodedValue);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

                $this->options['value'] = $this->value; // model may contain decoded JSON, override value for rendering
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->name
    Wontfix

                echo Html::hiddenInput($this->name, $this->value, $this->options);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method htmlEncode from undeclared class \yii\helpers\Json
    Wontfix

                        $jsOnModeChange .= "if (" . Json::htmlEncode($this->$property) . ".indexOf(newMode) !== -1) " .
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

                $this->value = $this->options['value'];
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method hiddenInput from undeclared class \yii\helpers\Html
    Wontfix

                echo Html::hiddenInput($this->name, $this->value, $this->options);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

                echo Html::hiddenInput($this->name, $this->value, $this->options);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

                echo Html::hiddenInput($this->name, $this->value, $this->options);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method __construct from undeclared class \yii\web\JsExpression
    Wontfix

                    $options[$key] = new JsExpression($value);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

            $this->options['data-json-editor-name'] = $editorName;
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method htmlEncode from undeclared class \yii\helpers\Json
    Wontfix

                Json::htmlEncode($this->clientOptions) . ");\n" .
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared class \yii\widgets\InputWidget
    Wontfix

            parent::init();
    Severity: Critical
    Found in src/JsonEditor.php by phan

    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);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

                $this->value = Json::encode($this->decodedValue);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->value
    Wontfix

                $this->value = Html::getAttributeValue($this->model, $this->attribute);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to undeclared method \kdn\yii2\JsonEditor::getView
    Wontfix

            $view = $this->getView();
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Possibly zero references to public method \kdn\yii2\JsonEditor::init()
    Wontfix

        public function init()
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method getAttributeValue from undeclared class \yii\helpers\Html
    Wontfix

                $this->value = Html::getAttributeValue($this->model, $this->attribute);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Possibly zero references to public method \kdn\yii2\JsonEditor::run()
    Wontfix

        public function run()
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Possibly zero write references to public property \kdn\yii2\JsonEditor->expandAll
    Wontfix

        public $expandAll = [];
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->options
    Wontfix

            if (!$this->issetValue() && isset($this->options['value'])) {
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Call to method decode from undeclared class \yii\helpers\Json
    Wontfix

            $this->decodedValue = Json::decode($this->value, false);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Call to method activeHiddenInput from undeclared class \yii\helpers\Html
    Wontfix

                echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
    Severity: Critical
    Found in src/JsonEditor.php by phan

    Reference to undeclared property \kdn\yii2\JsonEditor->attribute
    Wontfix

                echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
    Severity: Minor
    Found in src/JsonEditor.php by phan

    Define a constant instead of duplicating this literal "onModeChange" 4 times.
    Open

            'onModeChange',
    Severity: Critical
    Found in src/JsonEditor.php by sonar-php

    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'])) {
    Severity: Critical
    Found in src/JsonEditor.php by sonar-php

    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',
    Severity: Critical
    Found in src/JsonEditor.php by sonar-php

    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',
    Severity: Minor
    Found in src/JsonEditor.php by phpmd

    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++ ) {
            }
        }
    }

    Source https://phpmd.org/rules/naming.html#longvariable

    There are no issues that match your filters.

    Category
    Status