YetiForceCompany/YetiForceCRM

View on GitHub
app/Conditions/QueryFields/DateField.php

Summary

Maintainability
A
1 hr
Test Coverage
F
22%

Function getOrderBy has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
Open

    public function getOrderBy($order = false): array
    {
        if ($order && 'DESC' === strtoupper($order)) {
            $sort = SORT_DESC;
        } else {
Severity: Minor
Found in app/Conditions/QueryFields/DateField.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

The method getOrderBy has a boolean flag argument $order, which is a certain sign of a Single Responsibility Principle violation.
Open

    public function getOrderBy($order = false): array

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

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

    public function getCondition(?string $operator = null)
    {
        $fn = 'operator' . ucfirst($this->operator);
        if (isset(\App\Condition::DATE_OPERATORS[$this->operator]) && !method_exists($this, $fn)) {
            $fn = 'getStdOperator';

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

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

        } else {
            $sort = SORT_ASC;
        }

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 using static access to class '\DateTimeRange' in method 'getStdValue'.
Open

            $date = \DateTimeRange::getDateRangeByType($this->operator);

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 '\App\Log' in method 'getCondition'.
Open

            \App\Log::error("Not found operator: {$fn}({$this->operator}) in  " . __CLASS__);

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 getStdValue uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

        } else {
            $date = \DateTimeRange::getDateRangeByType($this->operator);
        }

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

Define a constant instead of duplicating this literal "Y-m-d" 3 times.
Open

        return ['>', $this->getColumnName(), date('Y-m-d')];

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

Noncompliant Code Example

With the default threshold of 3:

function run() {
  prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
  execute('action1');
  release('action1');
}

Compliant Solution

ACTION_1 = 'action1';

function run() {
  prepare(ACTION_1);
  execute(ACTION_1);
  release(ACTION_1);
}

Exceptions

To prevent generating some false-positives, literals having less than 5 characters are excluded.

Avoid unused parameters such as '$operator'.
Open

    public function getCondition(?string $operator = null)

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

Call to method getModuleField from undeclared class \App\QueryGenerator (Did you mean class \Tests\App\QueryGenerator)
Open

                $field = $this->queryGenerator->getModuleField('time_start');
Severity: Critical
Found in app/Conditions/QueryFields/DateField.php by phan

Call to method error from undeclared class \App\Log
Open

            \App\Log::error("Not found operator: {$fn}({$this->operator}) in  " . __CLASS__);
Severity: Critical
Found in app/Conditions/QueryFields/DateField.php by phan

Returning type array{0:'\x3c=',1:string,2:string} but operatorMoreThanDaysAgo() is declared to return bool
Open

        return ['<=', $this->getColumnName(), date('Y-m-d', strtotime('-' . $this->getValue() . ' days'))];

Call to method getModuleField from undeclared class \App\QueryGenerator (Did you mean class \Tests\App\QueryGenerator)
Open

                $field = $this->queryGenerator->getModuleField('time_end');
Severity: Critical
Found in app/Conditions/QueryFields/DateField.php by phan

Avoid variables with short names like $fn. Configured minimum length is 3.
Open

        $fn = 'operator' . ucfirst($this->operator);

ShortVariable

Since: 0.2

Detects when a field, local, or parameter has a very short name.

Example

class Something {
    private $q = 15; // VIOLATION - Field
    public static function main( array $as ) { // VIOLATION - Formal
        $r = 20 + $this->q; // VIOLATION - Local
        for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
            $r += $this->q;
        }
    }
}

Source https://phpmd.org/rules/naming.html#shortvariable

Spaces must be used to indent lines; tabs are not allowed
Open

    use \App\Conditions\QueryTraits\Comparison;

Spaces must be used to indent lines; tabs are not allowed
Open

                $field = $this->queryGenerator->getModuleField('time_start');

Spaces must be used to indent lines; tabs are not allowed
Open

    use \App\Conditions\QueryTraits\ComparisonField;

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

        if ($order && 'DESC' === strtoupper($order)) {

Spaces must be used to indent lines; tabs are not allowed
Open

                    $orderBy[$field->getTableName() . '.' . $field->getColumnName()] = $sort;

Spaces must be used to indent lines; tabs are not allowed
Open

        return $orderBy;

Spaces must be used to indent lines; tabs are not allowed
Open

            }

Spaces must be used to indent lines; tabs are not allowed
Open

        if ('Calendar' === $this->getModuleName()) {

Spaces must be used to indent lines; tabs are not allowed
Open

            if ('date_start' === $this->fieldModel->getColumnName()) {

Spaces must be used to indent lines; tabs are not allowed
Open

     * Get order by.

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param mixed $order

Spaces must be used to indent lines; tabs are not allowed
Open

        $orderBy = [$this->getColumnName() => $sort];

Spaces must be used to indent lines; tabs are not allowed
Open

            } elseif ('due_date' === $this->fieldModel->getColumnName()) {

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param ?string $operator

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

            $sort = SORT_DESC;

Spaces must be used to indent lines; tabs are not allowed
Open

                if ($field) {

Spaces must be used to indent lines; tabs are not allowed
Open

     * Get condition.

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

            $sort = SORT_ASC;

Spaces must be used to indent lines; tabs are not allowed
Open

        }

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

                }

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

        } else {

Spaces must be used to indent lines; tabs are not allowed
Open

        }

Spaces must be used to indent lines; tabs are not allowed
Open

                }

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

                if ($field) {

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

    public function getOrderBy($order = false): array

Spaces must be used to indent lines; tabs are not allowed
Open

                    $orderBy[$field->getTableName() . '.' . $field->getColumnName()] = $sort;

Spaces must be used to indent lines; tabs are not allowed
Open

                $field = $this->queryGenerator->getModuleField('time_end');

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

        }, explode(',', $this->value));

Spaces must be used to indent lines; tabs are not allowed
Open

     * Get value.

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

        return array_map(function ($row) {

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

            $date = $this->getArrayValue();

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['<', $this->getColumnName(), $this->getValue()];

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorGreaterthannow()

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['<=', $this->getColumnName(), date('Y-m-d', strtotime('-' . $this->getValue() . ' days'))];

Spaces must be used to indent lines; tabs are not allowed
Open

        $value = $this->getArrayValue();

Spaces must be used to indent lines; tabs are not allowed
Open

     * DashBoard Calendar widget listview operator.

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return bool

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorMoreThanDaysAgo()

Spaces must be used to indent lines; tabs are not allowed
Open

    public function getStdOperator()

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorBw()

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

        }

Spaces must be used to indent lines; tabs are not allowed
Open

     * Between operator.

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array|bool

Spaces must be used to indent lines; tabs are not allowed
Open

        $fn = 'operator' . ucfirst($this->operator);

Spaces must be used to indent lines; tabs are not allowed
Open

        if ('custom' === $this->operator) {

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

        if (!($methodExists = method_exists($this, $fn))) {

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return mixed

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

        }

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

            $date = \DateTimeRange::getDateRangeByType($this->operator);

Spaces must be used to indent lines; tabs are not allowed
Open

     * Get value.

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

    public function getCondition(?string $operator = null)

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

        return $date;

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return mixed

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorIr()

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['>', $this->getColumnName(), date('Y-m-d')];

Spaces must be used to indent lines; tabs are not allowed
Open

     * MoreThanDaysAgo operator.

Spaces must be used to indent lines; tabs are not allowed
Open

        if (isset(\App\Condition::DATE_OPERATORS[$this->operator]) && !method_exists($this, $fn)) {

Spaces must be used to indent lines; tabs are not allowed
Open

        }

Spaces must be used to indent lines; tabs are not allowed
Open

     * Get array value.

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorA(): array

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['and', ['<=', $this->getColumnName(), $value], ['>=', 'vtiger_activity.due_date', $value]];

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

            return \current(explode(' ', $row));

Spaces must be used to indent lines; tabs are not allowed
Open

     * Greater operator.

Spaces must be used to indent lines; tabs are not allowed
Open

     */

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorSmallerthannow()

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

        } else {

Spaces must be used to indent lines; tabs are not allowed
Open

        $value = $this->getStdValue();

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['>', $this->getColumnName(), $this->getValue()];

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

            \App\Log::error("Not found operator: {$fn}({$this->operator}) in  " . __CLASS__);

Spaces must be used to indent lines; tabs are not allowed
Open

    public function getStdValue()

Spaces must be used to indent lines; tabs are not allowed
Open

     * After operator.

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

    public function getArrayValue()

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['between', $this->getColumnName(), $value[0], $value[1]];

Spaces must be used to indent lines; tabs are not allowed
Open

     * Before operator.

Spaces must be used to indent lines; tabs are not allowed
Open

    public function operatorB()

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

        $value = $this->getValue();

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

     * Smaller operator.

Spaces must be used to indent lines; tabs are not allowed
Open

     *

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

            $fn = 'getStdOperator';

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return mixed

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

    /**

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array

Spaces must be used to indent lines; tabs are not allowed
Open

    }

Spaces must be used to indent lines; tabs are not allowed
Open

        return $methodExists ? $this->{$fn}() : $methodExists;

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['between', $this->getColumnName(), $value[0], $value[1] ?? $value[0]];

Spaces must be used to indent lines; tabs are not allowed
Open

    {

Spaces must be used to indent lines; tabs are not allowed
Open

        return ['<', $this->getColumnName(), date('Y-m-d')];

There are no issues that match your filters.

Category
Status