yiisoft/yii2

View on GitHub
framework/validators/DateValidator.php

Summary

Maintainability
B
6 hrs
Test Coverage

The class DateValidator has an overall complexity of 68 which is very high. The configured complexity threshold is 50.
Open

class DateValidator extends Validator
{
    /**
     * Constant for specifying the validation [[type]] as a date value, used for validation with intl short format.
     * @since 2.0.8

Function init has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Open

    public function init()
    {
        parent::init();
        if ($this->message === null) {
            $this->message = Yii::t('yii', 'The format of {attribute} is invalid.');
Severity: Minor
Found in framework/validators/DateValidator.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

Function validateAttribute has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Open

    public function validateAttribute($model, $attribute)
    {
        $value = $model->$attribute;
        if ($this->isEmpty($value)) {
            if ($this->timestampAttribute !== null) {
Severity: Minor
Found in framework/validators/DateValidator.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

The class DateValidator has 16 fields. Consider redesigning DateValidator to keep the number of fields under 15.
Open

class DateValidator extends Validator
{
    /**
     * Constant for specifying the validation [[type]] as a date value, used for validation with intl short format.
     * @since 2.0.8

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

Consider simplifying this complex logical expression.
Open

        if ($date === false || ($errors !== false && ($errors['error_count'] || $errors['warning_count'])) || ($this->strictDateFormat && $date->format($format) !== $value)) {
            return false;
        }
Severity: Major
Found in framework/validators/DateValidator.php - About 40 mins to fix

    Function parseDateValuePHP has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

        private function parseDateValuePHP($value, $format)
        {
            $hasTimeInfo = strpbrk($format, 'HhGgisU') !== false;
            // if no time was provided in the format string set timezone to default one to match yii\i18n\Formatter::formatDateTimeValue()
            $timezone = $hasTimeInfo ? $this->timeZone : $this->defaultTimeZone;
    Severity: Minor
    Found in framework/validators/DateValidator.php - About 25 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

    The method init() has an NPath complexity of 23040. The configured NPath complexity threshold is 200.
    Open

        public function init()
        {
            parent::init();
            if ($this->message === null) {
                $this->message = Yii::t('yii', 'The format of {attribute} is invalid.');

    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 validateAttribute() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10.
    Open

        public function validateAttribute($model, $attribute)
        {
            $value = $model->$attribute;
            if ($this->isEmpty($value)) {
                if ($this->timestampAttribute !== null) {

    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

    The method init() has a Cyclomatic Complexity of 20. The configured cyclomatic complexity threshold is 10.
    Open

        public function init()
        {
            parent::init();
            if ($this->message === null) {
                $this->message = Yii::t('yii', 'The format of {attribute} is invalid.');

    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

    Remove error control operator '@' on line 390.
    Open

        private function parseDateValueIntl($value, $format)
        {
            $formatter = $this->getIntlDateFormatter($format);
            // enable strict parsing to avoid getting invalid date values
            $formatter->setLenient(false);

    ErrorControlOperator

    Error suppression should be avoided if possible as it doesn't just suppress the error, that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. Consider changing error_reporting() level and/or setting up your own error handler.

    Example

    function foo($filePath) {
        $file = @fopen($filPath); // hides exceptions
        $key = @$array[$notExistingKey]; // assigns null to $key
    }

    Source http://phpmd.org/rules/cleancode.html#errorcontroloperator

    TODO found
    Open

            // TODO consider merging these methods into single one at 2.1

    Avoid excessively long variable names like $timestampAttributeTimeZone. Keep variable name length under 25.
    Open

        public $timestampAttributeTimeZone = 'UTC';

    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