YetiForceCompany/YetiForceCRM

View on GitHub
app/Fields/Double.php

Summary

Maintainability
A
55 mins
Test Coverage
A
100%

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

    public static function truncateZeros(string $value)
    {
        $seperator = \App\User::getCurrentUserModel()->getDetail('currency_decimal_separator');
        if (false === strpos($value, $seperator)) {
            return $value;
Severity: Minor
Found in app/Fields/Double.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

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

    public static function formatToDisplay(?string $value, $fixed = true): string
Severity: Minor
Found in app/Fields/Double.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

Avoid using static access to class 'App\Fields\Integer' in method 'formatToDisplay'.
Open

        $display = Integer::formatToDisplay($integer);
Severity: Minor
Found in app/Fields/Double.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 '\App\User' in method 'formatToDisplay'.
Open

        $userModel = \App\User::getCurrentUserModel();
Severity: Minor
Found in app/Fields/Double.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 '\App\User' in method 'formatToDb'.
Open

        $userModel = \App\User::getCurrentUserModel();
Severity: Minor
Found in app/Fields/Double.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

Refactor the code to avoid updating the loop counter "$i" within the loop body.
Open

                --$i;
Severity: Major
Found in app/Fields/Double.php by sonar-php

A for loop stop condition should test the loop counter against an invariant value (i.e. one that is true at both the beginning and ending of every loop iteration). Ideally, this means that the stop condition is set to a local variable just before the loop begins.

Stop conditions that are not invariant are slightly less efficient, as well as being difficult to understand and maintain, and likely lead to the introduction of errors in the future.

This rule tracks three types of non-invariant stop conditions:

  • When the loop counters are updated in the body of the for loop
  • When the stop condition depend upon a method call
  • When the stop condition depends on an object property, since such properties could change during the execution of the loop.

Noncompliant Code Example

for ($i = 0; $i < 10; $i++) {
  echo $i;
  if(condition) {
    $i = 20;
  }
}

Compliant Solution

for ($i = 0; $i < 10; $i++) {
  echo $i;
}

See

  • MISRA C:2004, 13.6 - Numeric variables being used within a for loop for iteration counting shall not be modified in the body of the loop.
  • MISRA C++:2008, 6-5-3 - The loop-counter shall not be modified within condition or statement.

Define a constant instead of duplicating this literal "currency_decimal_separator" 3 times.
Open

        $seperator = \App\User::getCurrentUserModel()->getDetail('currency_decimal_separator');
Severity: Critical
Found in app/Fields/Double.php by sonar-php

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.

Call to method getCurrentUserModel from undeclared class \App\User (Did you mean class \Tests\App\User)
Open

        $seperator = \App\User::getCurrentUserModel()->getDetail('currency_decimal_separator');
Severity: Critical
Found in app/Fields/Double.php by phan

Call to method getCurrentUserModel from undeclared class \App\User (Did you mean class \Tests\App\User)
Open

        $userModel = \App\User::getCurrentUserModel();
Severity: Critical
Found in app/Fields/Double.php by phan

Call to method getCurrentUserModel from undeclared class \App\User (Did you mean class \Tests\App\User)
Open

        $userModel = \App\User::getCurrentUserModel();
Severity: Critical
Found in app/Fields/Double.php by phan

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

            return $value;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        [$integer, $decimal] = array_pad(explode('.', $value, 2), 2, false);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        if (-1 !== $i) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        return $value;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @return string
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        for ($i = \strlen($value) - 1; $i >= 0; --$i) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            $value = 0;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        return (float) preg_replace('/[^0-9\.-]/', '', $value);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * Function to truncate zeros.
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

                --$i;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * Function to display number in user format.
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @param bool        $fixed
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        if (empty($value)) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        } elseif ($decimal) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $value = str_replace($decimalSeperator, '.', $value);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    public static function truncateZeros(string $value)
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $seperator = \App\User::getCurrentUserModel()->getDetail('currency_decimal_separator');
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $display = Integer::formatToDisplay($integer);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * Convert number to format for database.
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        if (empty($value)) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $userModel = \App\User::getCurrentUserModel();
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        if ($fixed) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $groupSeperator = $userModel->getDetail('currency_grouping_separator');
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            $value = number_format((float) $value, $userModel->getDetail('no_of_currency_decimals'), '.', '');
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $value = str_replace($groupSeperator, '', $value);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @return string
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @param string|null $value
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @param string|null $value
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @param string $value
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            if ('0' !== $value[$i]) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            $value = substr($value, 0, $i + 1);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            return 0;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        if (false === strpos($value, $seperator)) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            if ($value[$i] === $seperator) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

                break;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    public static function formatToDisplay(?string $value, $fixed = true): string
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

                break;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $decimalSeperator = $userModel->getDetail('currency_decimal_separator');
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        if ($userModel->getDetail('truncate_trailing_zeros')) {
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            $display = static::truncateZeros($display . $decimalSeperator . $decimal);
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        return $display;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    public static function formatToDb(?string $value): float
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

            $display .= $decimalSeperator . $decimal;
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

     * @return float
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $userModel = \App\User::getCurrentUserModel();
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

        $decimalSeperator = $userModel->getDetail('currency_decimal_separator');
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Fields/Double.php by phpcodesniffer

There are no issues that match your filters.

Category
Status