timegridio/concierge

View on GitHub
src/Traits/Preferenceable.php

Summary

Maintainability
A
2 hrs
Test Coverage

Function cast has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    private function cast($value, $type)
    {
        switch ($type) {
            case 'bool':
                return boolval($value);
Severity: Minor
Found in src/Traits/Preferenceable.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.
Open

                    return serialize($value);
Severity: Major
Found in src/Traits/Preferenceable.php - About 30 mins to fix

    Avoid too many return statements within this method.
    Open

                    return $value;
    Severity: Major
    Found in src/Traits/Preferenceable.php - About 30 mins to fix

      Avoid too many return statements within this method.
      Open

                          return unserialize($value);
      Severity: Major
      Found in src/Traits/Preferenceable.php - About 30 mins to fix

        Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'pref'.
        Open

                    Cache::put("{$this->slug}.'/'.{$key}", $value, 60);
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        StaticAccess

        Since: 1.4.0

        Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

        Example

        class Foo
        {
            public function bar()
            {
                Bar::baz();
            }
        }

        Source https://phpmd.org/rules/cleancode.html#staticaccess

        Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'pref'.
        Open

                Cache::put("{$this->slug}.'/'.{$key}", $value, 60);
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        StaticAccess

        Since: 1.4.0

        Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

        Example

        class Foo
        {
            public function bar()
            {
                Bar::baz();
            }
        }

        Source https://phpmd.org/rules/cleancode.html#staticaccess

        Avoid using static access to class '\Illuminate\Support\Facades\Cache' in method 'pref'.
        Open

                if($value = Cache::get("{$this->slug}.'/'.{$key}"))
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        StaticAccess

        Since: 1.4.0

        Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

        Example

        class Foo
        {
            public function bar()
            {
                Bar::baz();
            }
        }

        Source https://phpmd.org/rules/cleancode.html#staticaccess

        The method cast uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
        Open

                        } else {
                            return unserialize($value);
                        }
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        ElseExpression

        Since: 1.4.0

        An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

        Example

        class Foo
        {
            public function bar($flag)
            {
                if ($flag) {
                    // one branch
                } else {
                    // another branch
                }
            }
        }

        Source https://phpmd.org/rules/cleancode.html#elseexpression

        The method pref uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
        Open

                } else {
                    $default = Preference::getDefault($this, $key);
                    $value = $default->value();
                    $type = $default->type();
                }
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        ElseExpression

        Since: 1.4.0

        An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

        Example

        class Foo
        {
            public function bar($flag)
            {
                if ($flag) {
                    // one branch
                } else {
                    // another branch
                }
            }
        }

        Source https://phpmd.org/rules/cleancode.html#elseexpression

        Avoid assigning values to variables in if clauses and the like (line '44', column '13').
        Open

            public function pref($key, $value = null, $type = 'string')
            {
                if (isset($value)) {
                    $this->preferences()->updateOrCreate(['key' => $key], ['value' => $this->cast($value, $type),
                                                                           'type'  => $type, ]);
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        IfStatementAssignment

        Since: 2.7.0

        Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

        Example

        class Foo
        {
            public function bar($flag)
            {
                if ($foo = 'bar') { // possible typo
                    // ...
                }
                if ($baz = 0) { // always false
                    // ...
                }
            }
        }

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

        Avoid using static access to class '\Timegridio\Concierge\Models\Preference' in method 'pref'.
        Open

                    $default = Preference::getDefault($this, $key);
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        StaticAccess

        Since: 1.4.0

        Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

        Example

        class Foo
        {
            public function bar()
            {
                Bar::baz();
            }
        }

        Source https://phpmd.org/rules/cleancode.html#staticaccess

        Avoid assigning values to variables in if clauses and the like (line '39', column '12').
        Open

            public function pref($key, $value = null, $type = 'string')
            {
                if (isset($value)) {
                    $this->preferences()->updateOrCreate(['key' => $key], ['value' => $this->cast($value, $type),
                                                                           'type'  => $type, ]);
        Severity: Minor
        Found in src/Traits/Preferenceable.php by phpmd

        IfStatementAssignment

        Since: 2.7.0

        Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

        Example

        class Foo
        {
            public function bar($flag)
            {
                if ($foo = 'bar') { // possible typo
                    // ...
                }
                if ($baz = 0) { // always false
                    // ...
                }
            }
        }

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

        There are no issues that match your filters.

        Category
        Status