laravel/framework

View on GitHub
src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Summary

Maintainability
F
6 days
Test Coverage

File HasAttributes.php has 1186 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php

namespace Illuminate\Database\Eloquent\Concerns;

use BackedEnum;
Severity: Major
Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 3 days to fix

    Function addCastAttributesToArray has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes)
        {
            foreach ($this->getCasts() as $key => $value) {
                if (! array_key_exists($key, $attributes) ||
                    in_array($key, $mutatedAttributes)) {
    Severity: Minor
    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.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 originalIsEquivalent has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

        public function originalIsEquivalent($key)
        {
            if (! array_key_exists($key, $this->original)) {
                return false;
            }
    Severity: Minor
    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.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 castAttribute has 50 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        protected function castAttribute($key, $value)
        {
            $castType = $this->getCastType($key);
    
            if (is_null($value) && in_array($castType, static::$primitiveCastTypes)) {
    Severity: Minor
    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 2 hrs to fix

      Method originalIsEquivalent has 37 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function originalIsEquivalent($key)
          {
              if (! array_key_exists($key, $this->original)) {
                  return false;
              }
      Severity: Minor
      Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 1 hr to fix

        Method addCastAttributesToArray has 30 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes)
            {
                foreach ($this->getCasts() as $key => $value) {
                    if (! array_key_exists($key, $attributes) ||
                        in_array($key, $mutatedAttributes)) {
        Severity: Minor
        Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 1 hr to fix

          Method setAttribute has 30 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              public function setAttribute($key, $value)
              {
                  // First we will check for the presence of a mutator for the set operation
                  // which simply lets the developers tweak the attribute as it is set on
                  // this model, such as "json_encoding" a listing of data for storage.
          Severity: Minor
          Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 1 hr to fix

            Function setAttribute has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
            Open

                public function setAttribute($key, $value)
                {
                    // First we will check for the presence of a mutator for the set operation
                    // which simply lets the developers tweak the attribute as it is set on
                    // this model, such as "json_encoding" a listing of data for storage.
            Severity: Minor
            Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 1 hr 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

            Consider simplifying this complex logical expression.
            Open

                    if ($attribute === $original) {
                        return true;
                    } elseif (is_null($attribute)) {
                        return false;
                    } elseif ($this->isDateAttribute($key) || $this->isDateCastableWithCustomFormat($key)) {
            Severity: Major
            Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 1 hr to fix

              Function transformModelValue has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
              Open

                  protected function transformModelValue($key, $value)
                  {
                      // If the attribute has a get mutator, we will call that then return what
                      // it returns as the value, which is useful for transforming values on
                      // retrieval from the model to a form that is more useful for usage.
              Severity: Minor
              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 55 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

              Function relationsToArray has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
              Open

                  public function relationsToArray()
                  {
                      $attributes = [];
              
                      foreach ($this->getArrayableRelations() as $key => $value) {
              Severity: Minor
              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 55 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

              Function getClassCastableAttributeValue has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
              Open

                  protected function getClassCastableAttributeValue($key, $value)
                  {
                      $caster = $this->resolveCasterClass($key);
              
                      $objectCachingDisabled = $caster->withoutObjectCaching ?? false;
              Severity: Minor
              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 35 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.
              Open

                              return false;
              Severity: Major
              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                Avoid too many return statements within this method.
                Open

                                return (string) $value;
                Severity: Major
                Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                  Avoid too many return statements within this method.
                  Open

                                  return $this->fromJson($value, true);
                  Severity: Major
                  Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                    Avoid too many return statements within this method.
                    Open

                                return $this->fillJsonAttribute($key, $value);
                    Severity: Major
                    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                      Avoid too many return statements within this method.
                      Open

                              return is_numeric($attribute) && is_numeric($original)
                                  && strcmp((string) $attribute, (string) $original) === 0;
                      Severity: Major
                      Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                        Avoid too many return statements within this method.
                        Open

                                        return $this->asDateTime($value)->toImmutable();
                        Severity: Major
                        Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                          Avoid too many return statements within this method.
                          Open

                                          return new BaseCollection($this->fromJson($value));
                          Severity: Major
                          Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                            Avoid too many return statements within this method.
                            Open

                                        return $this->castAttribute($key, $attribute) ===
                                            $this->castAttribute($key, $original);
                            Severity: Major
                            Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                          return $this->getClassCastableAttributeValue($key, $value);
                              Severity: Major
                              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                Avoid too many return statements within this method.
                                Open

                                            return $this->getEnumCastableAttributeValue($key, $value);
                                Severity: Major
                                Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                  Avoid too many return statements within this method.
                                  Open

                                              return false;
                                  Severity: Major
                                  Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                    Avoid too many return statements within this method.
                                    Open

                                                return $this->fromJson($attribute) ===
                                                    $this->fromJson($original);
                                    Severity: Major
                                    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                      Avoid too many return statements within this method.
                                      Open

                                                  return $this->fromJson($attribute) === $this->fromJson($original);
                                      Severity: Major
                                      Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                        Avoid too many return statements within this method.
                                        Open

                                                    return false;
                                        Severity: Major
                                        Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                          Avoid too many return statements within this method.
                                          Open

                                                  return $value;
                                          Severity: Major
                                          Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                            Avoid too many return statements within this method.
                                            Open

                                                    return $this;
                                            Severity: Major
                                            Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                              Avoid too many return statements within this method.
                                              Open

                                                              return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original);
                                              Severity: Major
                                              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                Avoid too many return statements within this method.
                                                Open

                                                                return $this->fromJson($value);
                                                Severity: Major
                                                Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                  Avoid too many return statements within this method.
                                                  Open

                                                                  return (bool) $value;
                                                  Severity: Major
                                                  Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                    Avoid too many return statements within this method.
                                                    Open

                                                                    return $this->asDateTime($value);
                                                    Severity: Major
                                                    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                      Avoid too many return statements within this method.
                                                      Open

                                                              return $value;
                                                      Severity: Major
                                                      Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                        Avoid too many return statements within this method.
                                                        Open

                                                                        return $this->asDate($value)->toImmutable();
                                                        Severity: Major
                                                        Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                          Avoid too many return statements within this method.
                                                          Open

                                                                          return $this->asTimestamp($value);
                                                          Severity: Major
                                                          Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                            Avoid too many return statements within this method.
                                                            Open

                                                                    return $date ?: Date::parse($value);
                                                            Severity: Major
                                                            Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                              Avoid too many return statements within this method.
                                                              Open

                                                                          return abs($this->castAttribute($key, $attribute) - $this->castAttribute($key, $original)) < PHP_FLOAT_EPSILON * 4;
                                                              Severity: Major
                                                              Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                                Avoid too many return statements within this method.
                                                                Open

                                                                                return $this->asDate($value);
                                                                Severity: Major
                                                                Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

                                                                  Avoid too many return statements within this method.
                                                                  Open

                                                                              return $this->fromJson($attribute) === $this->fromJson($original);
                                                                  Severity: Major
                                                                  Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php - About 30 mins to fix

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

                                                                        protected function getCastType($key)
                                                                        {
                                                                            $castType = $this->getCasts()[$key];
                                                                    
                                                                            if (isset(static::$castTypeCache[$castType])) {
                                                                    Severity: Minor
                                                                    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.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

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

                                                                        protected function castAttribute($key, $value)
                                                                        {
                                                                            $castType = $this->getCastType($key);
                                                                    
                                                                            if (is_null($value) && in_array($castType, static::$primitiveCastTypes)) {
                                                                    Severity: Minor
                                                                    Found in src/Illuminate/Database/Eloquent/Concerns/HasAttributes.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

                                                                    There are no issues that match your filters.

                                                                    Category
                                                                    Status