wol-soft/php-json-schema-model-generator-production

View on GitHub
src/Traits/SerializableTrait.php

Summary

Maintainability
A
1 hr
Test Coverage
D
64%

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

    public function toArray(array $except = [], int $depth = 512)
    {
        if ($depth < 1) {
            return false;
        }
Severity: Minor
Found in src/Traits/SerializableTrait.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

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

    public function toArray(array $except = [], int $depth = 512)
    {
        if ($depth < 1) {
            return false;
        }
Severity: Minor
Found in src/Traits/SerializableTrait.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

Remove the unused function parameter "$depth".
Open

    protected function resolveSerializationHook(array $data, int $depth, array $except): array
Severity: Major
Found in src/Traits/SerializableTrait.php by sonar-php

Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.

Noncompliant Code Example

function doSomething($a, $b) { // "$a" is unused
  return compute($b);
}

Compliant Solution

function doSomething($b) {
  return compute($b);
}

Exceptions

Functions in classes that override a class or implement interfaces are ignored.

class C extends B {

  function doSomething($a, $b) {     // no issue reported on $b
    compute($a);
  }

}

See

  • MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
  • MISRA C:2012, 2.7 - There should be no unused parameters in functions
  • CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
  • CERT, MSC12-CPP. - Detect and remove code that has no effect

Remove the unused function parameter "$except".
Open

    protected function resolveSerializationHook(array $data, int $depth, array $except): array
Severity: Major
Found in src/Traits/SerializableTrait.php by sonar-php

Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.

Noncompliant Code Example

function doSomething($a, $b) { // "$a" is unused
  return compute($b);
}

Compliant Solution

function doSomething($b) {
  return compute($b);
}

Exceptions

Functions in classes that override a class or implement interfaces are ignored.

class C extends B {

  function doSomething($a, $b) {     // no issue reported on $b
    compute($a);
  }

}

See

  • MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
  • MISRA C:2012, 2.7 - There should be no unused parameters in functions
  • CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
  • CERT, MSC12-CPP. - Detect and remove code that has no effect

Avoid unused local variables such as '$value'.
Open

        foreach (get_class_vars(get_class($this)) as $key => $value) {
Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

UnusedLocalVariable

Since: 0.2

Detects when a local variable is declared and/or assigned, but not used.

Example

class Foo {
    public function doSomething()
    {
        $i = 5; // Unused
    }
}

Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable

Avoid unused parameters such as '$except'.
Open

    protected function resolveSerializationHook(array $data, int $depth, array $except): array
Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

UnusedFormalParameter

Since: 0.2

Avoid passing parameters to methods or constructors and then not using those parameters.

Example

class Foo
{
    private function bar($howdy)
    {
        // $howdy is not used
    }
}

Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

Avoid unused parameters such as '$depth'.
Open

    protected function resolveSerializationHook(array $data, int $depth, array $except): array
Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

UnusedFormalParameter

Since: 0.2

Avoid passing parameters to methods or constructors and then not using those parameters.

Example

class Foo
{
    private function bar($howdy)
    {
        // $howdy is not used
    }
}

Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

Property name "$_customSerializer" should not be prefixed with an underscore to indicate visibility
Open

    private static $_customSerializer = [];

Opening brace should be on a new line
Open

    private function _getSerializedValue($value, int $depth, array $except) {

Opening brace should be on a new line
Open

    private function _getCustomSerializerMethod(string $property) {

The variable $_customSerializer is not named in camelCase.
Open

    private function _getCustomSerializerMethod(string $property) {
        if (isset(self::$_customSerializer[$property])) {
            return self::$_customSerializer[$property];
        }

Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

CamelCaseVariableName

Since: 0.2

It is considered best practice to use the camelCase notation to name variables.

Example

class ClassName {
    public function doSomething() {
        $data_module = new DataModule();
    }
}

Source

The variable $_customSerializer is not named in camelCase.
Open

    private function _getCustomSerializerMethod(string $property) {
        if (isset(self::$_customSerializer[$property])) {
            return self::$_customSerializer[$property];
        }

Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

CamelCaseVariableName

Since: 0.2

It is considered best practice to use the camelCase notation to name variables.

Example

class ClassName {
    public function doSomething() {
        $data_module = new DataModule();
    }
}

Source

The variable $_customSerializer is not named in camelCase.
Open

    private function _getCustomSerializerMethod(string $property) {
        if (isset(self::$_customSerializer[$property])) {
            return self::$_customSerializer[$property];
        }

Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

CamelCaseVariableName

Since: 0.2

It is considered best practice to use the camelCase notation to name variables.

Example

class ClassName {
    public function doSomething() {
        $data_module = new DataModule();
    }
}

Source

The method _getCustomSerializerMethod is not named in camelCase.
Open

    private function _getCustomSerializerMethod(string $property) {
        if (isset(self::$_customSerializer[$property])) {
            return self::$_customSerializer[$property];
        }

Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

CamelCaseMethodName

Since: 0.2

It is considered best practice to use the camelCase notation to name methods.

Example

class ClassName {
    public function get_name() {
    }
}

Source

The method _getSerializedValue is not named in camelCase.
Open

    private function _getSerializedValue($value, int $depth, array $except) {
        if (is_array($value)) {
            $subData = [];
            foreach ($value as $subKey => $element) {
                $subData[$subKey] = $this->evaluateAttribute($element, $depth, $except);
Severity: Minor
Found in src/Traits/SerializableTrait.php by phpmd

CamelCaseMethodName

Since: 0.2

It is considered best practice to use the camelCase notation to name methods.

Example

class ClassName {
    public function get_name() {
    }
}

Source

There are no issues that match your filters.

Category
Status