keboola/php-utils

View on GitHub
src/Keboola/Utils/getDataFromPath.php

Summary

Maintainability
A
2 hrs
Test Coverage

Function getDataFromPath has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

function getDataFromPath($path, $data, $separator = "/", $ignoreEmpty = true)
{
    if (isset($path) && $path !== $separator) {
        // TODO possibly add functions in the $path? Such as path/to/join(data) would do a join on that data..likely belongs to EX/(sub)Parser/TR
        $str = explode($separator, $path);
Severity: Minor
Found in src/Keboola/Utils/getDataFromPath.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 method getDataFromPath has a boolean flag argument $ignoreEmpty, which is a certain sign of a Single Responsibility Principle violation.
Open

function getDataFromPath($path, $data, $separator = "/", $ignoreEmpty = true)
Severity: Minor
Found in src/Keboola/Utils/getDataFromPath.php by phpmd

BooleanArgumentFlag

Since: 1.4.0

A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

Example

class Foo {
    public function bar($flag = true) {
    }
}

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

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

                } else {
                    throw new Exception\NoDataFoundException("Error parsing data. {$path} not found.");
                }
Severity: Minor
Found in src/Keboola/Utils/getDataFromPath.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 getDataFromPath uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

            } else {
                if ($ignoreEmpty == true) { // return if empty and ignore == true
                    $data = null;
                    break;
                } else {
Severity: Minor
Found in src/Keboola/Utils/getDataFromPath.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

TODO found
Open

        // TODO possibly add functions in the $path? Such as path/to/join(data) would do a join on that data..likely belongs to EX/(sub)Parser/TR
Severity: Minor
Found in src/Keboola/Utils/getDataFromPath.php by fixme

There are no issues that match your filters.

Category
Status