dmitry-kulikov/yii2-domain-validator

View on GitHub
src/DomainValidator.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%

Function validateValue has a Cognitive Complexity of 45 (exceeds 5 allowed). Consider refactoring.
Wontfix

    protected function validateValue($value)
    {
        if (!is_string($value)) {
            return $this->getErrorMessage('messageNotString');
        }
Severity: Minor
Found in src/DomainValidator.php - About 6 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 validateValue has 86 lines of code (exceeds 25 allowed). Consider refactoring.
Wontfix

    protected function validateValue($value)
    {
        if (!is_string($value)) {
            return $this->getErrorMessage('messageNotString');
        }
Severity: Major
Found in src/DomainValidator.php - About 3 hrs to fix

    Method getDefaultErrorMessages has 38 lines of code (exceeds 25 allowed). Consider refactoring.
    Wontfix

        protected function getDefaultErrorMessages()
        {
            $messages = [
                'message' => '{attribute} is invalid.',
                'messageDNS' => 'DNS record corresponding to {attribute} not found.',
    Severity: Minor
    Found in src/DomainValidator.php - About 1 hr to fix

      The class DomainValidator has 18 fields. Consider redesigning DomainValidator to keep the number of fields under 15.
      Wontfix

      class DomainValidator extends Validator
      {
          /**
           * @var bool whether to allow underscores in domain name;
           * defaults to false
      Severity: Minor
      Found in src/DomainValidator.php by phpmd

      TooManyFields

      Since: 0.1

      Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.

      Example

      class Person {
         protected $one;
         private $two;
         private $three;
         [... many more fields ...]
      }

      Source https://phpmd.org/rules/codesize.html#toomanyfields

      Function getDefaultErrorMessages has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Wontfix

          protected function getDefaultErrorMessages()
          {
              $messages = [
                  'message' => '{attribute} is invalid.',
                  'messageDNS' => 'DNS record corresponding to {attribute} not found.',
      Severity: Minor
      Found in src/DomainValidator.php - About 45 mins 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

      Avoid too many return statements within this method.
      Wontfix

                      return $this->getErrorMessage('messageInvalidCharacter');
      Severity: Major
      Found in src/DomainValidator.php - About 30 mins to fix

        Avoid too many return statements within this method.
        Wontfix

                        return call_user_func($this->checkDNS, $value);
        Severity: Major
        Found in src/DomainValidator.php - About 30 mins to fix

          Avoid too many return statements within this method.
          Wontfix

                      return $this->getErrorMessage('messageLabelNumberMin', ['labelNumberMin' => $this->labelNumberMin]);
          Severity: Major
          Found in src/DomainValidator.php - About 30 mins to fix

            Avoid too many return statements within this method.
            Wontfix

                            return $this->getErrorMessage('messageLabelTooShort');
            Severity: Major
            Found in src/DomainValidator.php - About 30 mins to fix

              Avoid too many return statements within this method.
              Wontfix

                              return $this->getErrorMessage('messageLabelTooLong');
              Severity: Major
              Found in src/DomainValidator.php - About 30 mins to fix

                Avoid too many return statements within this method.
                Wontfix

                                return $this->getErrorMessage('messageLabelStartEnd');
                Severity: Major
                Found in src/DomainValidator.php - About 30 mins to fix

                  Avoid too many return statements within this method.
                  Wontfix

                          return null;
                  Severity: Major
                  Found in src/DomainValidator.php - About 30 mins to fix

                    Avoid too many return statements within this method.
                    Wontfix

                                    return $this->getErrorMessage('messageDNS');
                    Severity: Major
                    Found in src/DomainValidator.php - About 30 mins to fix

                      The method validateValue() has an NPath complexity of 1347840. The configured NPath complexity threshold is 200.
                      Wontfix

                          protected function validateValue($value)
                          {
                              if (!is_string($value)) {
                                  return $this->getErrorMessage('messageNotString');
                              }
                      Severity: Minor
                      Found in src/DomainValidator.php by phpmd

                      NPathComplexity

                      Since: 0.1

                      The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.

                      Example

                      class Foo {
                          function bar() {
                              // lots of complicated code
                          }
                      }

                      Source https://phpmd.org/rules/codesize.html#npathcomplexity

                      The method validateValue() has 109 lines of code. Current threshold is set to 100. Avoid really long methods.
                      Wontfix

                          protected function validateValue($value)
                          {
                              if (!is_string($value)) {
                                  return $this->getErrorMessage('messageNotString');
                              }
                      Severity: Minor
                      Found in src/DomainValidator.php by phpmd

                      The method validateValue() has a Cyclomatic Complexity of 32. The configured cyclomatic complexity threshold is 10.
                      Wontfix

                          protected function validateValue($value)
                          {
                              if (!is_string($value)) {
                                  return $this->getErrorMessage('messageNotString');
                              }
                      Severity: Minor
                      Found in src/DomainValidator.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

                      Reduce the number of returns of this function 12, down to the maximum allowed 3.
                      Wontfix

                          protected function validateValue($value)
                      Severity: Major
                      Found in src/DomainValidator.php by sonar-php

                      Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.

                      Noncompliant Code Example

                      With the default threshold of 3:

                      function myFunction(){ // Noncompliant as there are 4 return statements
                        if (condition1) {
                          return true;
                        } else {
                          if (condition2) {
                            return false;
                          } else {
                            return true;
                          }
                        }
                        return false;
                      }
                      

                      Refactor this function to reduce its Cognitive Complexity from 46 to the 15 allowed.
                      Wontfix

                          protected function validateValue($value)
                      Severity: Critical
                      Found in src/DomainValidator.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

                      Define a constant instead of duplicating this literal "messageLabelTooShort" 3 times.
                      Wontfix

                                          $errorMessageName = 'messageLabelTooShort';
                      Severity: Critical
                      Found in src/DomainValidator.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 "messageLabelStartEnd" 4 times.
                      Wontfix

                                          $errorMessageName = 'messageLabelStartEnd';
                      Severity: Critical
                      Found in src/DomainValidator.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 "messageInvalidCharacter" 6 times.
                      Wontfix

                                          $errorMessageName = 'messageInvalidCharacter';
                      Severity: Critical
                      Found in src/DomainValidator.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 "kdn/yii2/validators/domain" 3 times.
                      Wontfix

                              Yii::$app->i18n->translations['kdn/yii2/validators/domain'] = [
                      Severity: Critical
                      Found in src/DomainValidator.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 "messageTooLong" 5 times.
                      Wontfix

                                          $errorMessageName = 'messageTooLong';
                      Severity: Critical
                      Found in src/DomainValidator.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 "messageLabelTooLong" 4 times.
                      Wontfix

                                          $errorMessageName = 'messageLabelTooLong';
                      Severity: Critical
                      Found in src/DomainValidator.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 "message" 3 times.
                      Wontfix

                                          $errorMessageName = 'message';
                      Severity: Critical
                      Found in src/DomainValidator.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.

                      Catching undeclared class \yii\base\ErrorException (Did you mean class \ErrorException)
                      Invalid

                              } catch (ErrorException $e) {
                      Severity: Critical
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageLabelTooShort
                      Invalid

                          public $messageLabelTooShort;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageLabelNumberMin
                      Invalid

                          public $messageLabelNumberMin;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageLabelStartEnd
                      Invalid

                          public $messageLabelStartEnd;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Call to method __construct from undeclared class \yii\base\InvalidConfigException
                      Invalid

                                  throw new InvalidConfigException(
                      Severity: Critical
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageNotString
                      Invalid

                          public $messageNotString;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Reference to static property app from undeclared class \Yii
                      Invalid

                              Yii::$app->i18n->translations['kdn/yii2/validators/domain'] = [
                      Severity: Critical
                      Found in src/DomainValidator.php by phan

                      Possibly zero write references to public property \kdn\yii2\validators\DomainValidator->checkDNS
                      Invalid

                          public $checkDNS = false;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Reference to static property app from undeclared class \Yii
                      Invalid

                                  $this->encoding = Yii::$app->charset;
                      Severity: Critical
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public method \kdn\yii2\validators\DomainValidator::init()
                      Invalid

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

                      Possibly zero write references to public property \kdn\yii2\validators\DomainValidator->allowUnderscore
                      Invalid

                          public $allowUnderscore = false;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Class extends undeclared class \yii\validators\Validator
                      Invalid

                      class DomainValidator extends Validator
                      Severity: Critical
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageInvalidCharacter
                      Invalid

                          public $messageInvalidCharacter;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero write references to public property \kdn\yii2\validators\DomainValidator->simpleErrorMessage
                      Invalid

                          public $simpleErrorMessage = false;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Reference to undeclared class \yii\validators\Validator
                      Invalid

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

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageTooShort
                      Invalid

                          public $messageTooShort;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero write references to public property \kdn\yii2\validators\DomainValidator->labelNumberMin
                      Invalid

                          public $labelNumberMin = 2;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageLabelTooLong
                      Invalid

                          public $messageLabelTooLong;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      This branch's code block is the same as the block for the branch on line 221.
                      Open

                                      } elseif (empty($idnaInfo)) {
                                          // too long domain name caused buffer overflow
                                          $errorMessageName = 'messageTooLong';
                                      } else {
                      Severity: Major
                      Found in src/DomainValidator.php by sonar-php

                      Having two cases in a switch statement or two branches in an if chain with the same implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, then in an if chain they should be combined, or for a switch, one should fall through to the other.

                      Noncompliant Code Example

                      switch ($i) {
                        case 1:
                          doSomething();
                          break;
                        case 2:
                          doSomethingDifferent();
                          break;
                        case 3:  // Noncompliant; duplicates case 1's implementation
                          doSomething();
                          break;
                        default:
                          doTheRest();
                      }
                      
                      if ($a >= 0 && $a < 10) {
                        doTheThing();
                      else if ($a >= 10 && $a < 20) {
                        doTheOtherThing();
                      }
                      else if ($a >= 20 && $a < 50) {
                        doTheThing();  // Noncompliant; duplicates first condition
                      }
                      else {
                        doTheRest();
                      }
                      
                      if ($b == 0) {
                        doOneMoreThing();
                      }
                      else {
                        doOneMoreThing(); // Noncompliant; duplicates then-branch
                      }
                      
                      var b = a ? 12 > 4 : 4;  // Noncompliant; always results in the same value
                      

                      Compliant Solution

                      switch ($i) {
                        case 1:
                        case 3:
                          doSomething();
                          break;
                        case 2:
                          doSomethingDifferent();
                          break;
                        default:
                          doTheRest();
                      }
                      
                      if (($a >= 0 && $a < 10) || ($a >= 20 && $a < 50)) {
                        doTheThing();
                      else if ($a >= 10 && $a < 20) {
                        doTheOtherThing();
                      }
                      else {
                        doTheRest();
                      }
                      
                      doOneMoreThing();
                      
                      b = 4;
                      

                      or

                      switch ($i) {
                        case 1:
                          doSomething();
                          break;
                        case 2:
                          doSomethingDifferent();
                          break;
                        case 3:
                          doThirdThing();
                          break;
                        default:
                          doTheRest();
                      }
                      
                      if ($a >= 0 && $a < 10) {
                        doTheThing();
                      else if ($a >= 10 && $a < 20) {
                        doTheOtherThing();
                      }
                      else if ($a >= 20 && $a < 50) {
                        doTheThirdThing();
                      }
                      else {
                        doTheRest();
                      }
                      
                      if ($b == 0) {
                        doOneMoreThing();
                      }
                      else {
                        doTheRest();
                      }
                      
                      int b = a ? 12 > 4 : 8;
                      

                      Exceptions

                      Blocks in an if chain that contain a single line of code are ignored, as are blocks in a switch statement that contain a single line of code with or without a following break.

                      Possibly zero write references to public property \kdn\yii2\validators\DomainValidator->allowURL
                      Invalid

                          public $allowURL = true;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero write references to public property \kdn\yii2\validators\DomainValidator->enableIDN
                      Invalid

                          public $enableIDN = false;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageDNS
                      Invalid

                          public $messageDNS;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to protected method \kdn\yii2\validators\DomainValidator::validateValue()
                      Invalid

                          protected function validateValue($value)
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Possibly zero references to public property \kdn\yii2\validators\DomainValidator->messageTooLong
                      Invalid

                          public $messageTooLong;
                      Severity: Minor
                      Found in src/DomainValidator.php by phan

                      Call to method t from undeclared class \Yii
                      Invalid

                              $this->$name = Yii::t('kdn/yii2/validators/domain', $this->getDefaultErrorMessages()[$name]);
                      Severity: Critical
                      Found in src/DomainValidator.php by phan

                      Unused definition of variable $e as a caught exception
                      Invalid

                              } catch (ErrorException $e) {
                      Severity: Info
                      Found in src/DomainValidator.php by phan

                      Avoid excessively long variable names like $messageLabelNumberMin. Keep variable name length under 20.
                      Wontfix

                          public $messageLabelNumberMin;
                      Severity: Minor
                      Found in src/DomainValidator.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

                      Avoid excessively long variable names like $messageInvalidCharacter. Keep variable name length under 20.
                      Wontfix

                          public $messageInvalidCharacter;
                      Severity: Minor
                      Found in src/DomainValidator.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