YetiForceCompany/YetiForceCRM

View on GitHub
app/Layout/Media.php

Summary

Maintainability
A
35 mins
Test Coverage
F
0%

Function getImages has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    public static function getImages(): array
    {
        if (null === self::$images) {
            self::$images = [];
            $dataReader = (new \App\Db\Query())->from(static::TABLE_NAME_MEDIA)->where(['status' => 1, 'fieldname' => 'image'])->createCommand()->query();
Severity: Minor
Found in app/Layout/Media.php - About 35 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

Missing class import via use statement (line '33', column '23').
Open

            $dataReader = (new \App\Db\Query())->from(static::TABLE_NAME_MEDIA)->where(['status' => 1, 'fieldname' => 'image'])->createCommand()->query();
Severity: Minor
Found in app/Layout/Media.php by phpmd

MissingImport

Since: 2.7.0

Importing all external classes in a file through use statements makes them clearly visible.

Example

function make() {
    return new \stdClass();
}

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

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

    public static function getImageHtml(string $value): string
    {
        $icon = '';
        if ($value && !\App\Json::isEmpty($value)) {
            ['type' => $type, 'name' => $name] = \App\Json::decode($value);
Severity: Minor
Found in app/Layout/Media.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 '\App\Json' in method 'getImageHtml'.
Open

            ['type' => $type, 'name' => $name] = \App\Json::decode($value);
Severity: Minor
Found in app/Layout/Media.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\Json' in method 'getImageHtml'.
Open

        if ($value && !\App\Json::isEmpty($value)) {
Severity: Minor
Found in app/Layout/Media.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

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

                $row['relativePath'] = "{$row['path']}{$row['key']}.{$row['ext']}";
Severity: Critical
Found in app/Layout/Media.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 undeclared method \App\Db::createCommand
Open

        $dbCommand = \App\Db::getInstance()->createCommand();
Severity: Critical
Found in app/Layout/Media.php by phan

Call to undeclared method \App\Db\Query::from
Open

            $dataReader = (new \App\Db\Query())->from(static::TABLE_NAME_MEDIA)->where(['status' => 1, 'fieldname' => 'image'])->createCommand()->query();
Severity: Critical
Found in app/Layout/Media.php by phan

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

        return self::$images;
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

Line exceeds 120 characters; contains 154 characters
Open

            $dataReader = (new \App\Db\Query())->from(static::TABLE_NAME_MEDIA)->where(['status' => 1, 'fieldname' => 'image'])->createCommand()->query();
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                    $path = $path = substr($path, 12, \strlen($path));
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                $row['relativePath'] = "{$row['path']}{$row['key']}.{$row['ext']}";
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            $dataReader->close();
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        if (null === self::$images) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @param string $key
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * Images.
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            while ($row = $dataReader->read()) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    public static function getImages(): array
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            self::$images = [];
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    /** @var string Media table name. */
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @var array
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                self::$images[$row['key']] = $row;
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * Get image data.
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    protected static $images;
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        return self::getImages()[$key] ?? [];
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        if ($path && !file_exists(self::getImages()[$key]['relativePath'])) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        return $path;
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                $icon = '<img class="icon-img--picklist mr-1" src="' . $src . '">';
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @return string
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    public const TABLE_NAME_MEDIA = 'u_#__file_upload';
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    public static function removeImage(string $key): bool
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * Get image URL.
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                $icon = "<span class=\"{$name} mr-1\"></span>";
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     */
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    public static function getImage(string $key): array
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    public static function getImageHtml(string $value): string
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @param string $key
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            if ('icon' === $type) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @return bool
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        return ($path = self::getImages()[$key]['relativePath'] ?? '') && file_exists($path) && $dbCommand->delete(self::TABLE_NAME_MEDIA, ['key' => $key])->execute() && unlink($path);
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

Line exceeds 120 characters; contains 184 characters
Open

        return ($path = self::getImages()[$key]['relativePath'] ?? '') && file_exists($path) && $dbCommand->delete(self::TABLE_NAME_MEDIA, ['key' => $key])->execute() && unlink($path);
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                $path = $row['path'];
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * Get image HTML.
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @param string $value json
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        if ($value && !\App\Json::isEmpty($value)) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                $row['src'] = "{$path}{$row['key']}.{$row['ext']}";
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @return string
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        $icon = '';
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

                if (IS_PUBLIC_DIR && 0 === strpos($path, 'public_html/')) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    public static function getImageUrl(string $key): string
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            $path = '';
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    }
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @return array
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * Delete image file.
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     * @param string $key
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        $path = self::getImages()[$key]['src'] ?? '';
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            $dataReader = (new \App\Db\Query())->from(static::TABLE_NAME_MEDIA)->where(['status' => 1, 'fieldname' => 'image'])->createCommand()->query();
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

    /**
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            ['type' => $type, 'name' => $name] = \App\Json::decode($value);
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

            } elseif ('image' === $type && ($src = self::getImageUrl($name))) {
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        return $icon;
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

        $dbCommand = \App\Db::getInstance()->createCommand();
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

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

     *
Severity: Minor
Found in app/Layout/Media.php by phpcodesniffer

There are no issues that match your filters.

Category
Status