YetiForceCompany/YetiForceCRM

View on GitHub
modules/OSSMailView/relations/GetRecordToMails.php

Summary

Maintainability
A
45 mins
Test Coverage
F
0%

Function create has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    public function create(int $sourceRecordId, int $destinationRecordId): bool
    {
        $return = false;
        $data = ['ossmailviewid' => $sourceRecordId, 'crmid' => $destinationRecordId];
        if (!$this->isExists($data)) {
Severity: Minor
Found in modules/OSSMailView/relations/GetRecordToMails.php - About 45 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 '71', column '22').
Open

        return (bool) (new \App\Db\Query())->from(self::TABLE_NAME)->where($data)->exists();

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 '53', column '46').
Open

    public function create(int $sourceRecordId, int $destinationRecordId): bool
    {
        $return = false;
        $data = ['ossmailviewid' => $sourceRecordId, 'crmid' => $destinationRecordId];
        if (!$this->isExists($data)) {

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 '\Users_Privileges_Model' in method 'create'.
Open

            if ($return && ($parentId = \Users_Privileges_Model::getParentRecord($destinationRecordId))) {

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 '\Users_Privileges_Model' in method 'create'.
Open

                if ($this->addRelation($data, $date) && ($parentId = \Users_Privileges_Model::getParentRecord($parentId))) {

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 assigning values to variables in if clauses and the like (line '51', column '20').
Open

    public function create(int $sourceRecordId, int $destinationRecordId): bool
    {
        $return = false;
        $data = ['ossmailviewid' => $sourceRecordId, 'crmid' => $destinationRecordId];
        if (!$this->isExists($data)) {

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

Define a constant instead of duplicating this literal "ossmailviewid" 4 times.
Open

        return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $destinationRecordId, 'ossmailviewid' => $sourceRecordId])->execute();

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.

Define a constant instead of duplicating this literal "crmid" 5 times.
Open

        return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $destinationRecordId, 'ossmailviewid' => $sourceRecordId])->execute();

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 addJoin from undeclared class \App\QueryGenerator (Did you mean class \Tests\App\QueryGenerator)
Open

        $this->relationModel->getQueryGenerator()

Call to undeclared method \App\Db::createCommand
Open

        return (bool) \App\Db::getInstance()->createCommand()->update(self::TABLE_NAME,

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

        return (bool) (new \App\Db\Query())->from(self::TABLE_NAME)->where($data)->exists();

Call to undeclared method \App\Db::createCommand
Open

        return (bool) \App\Db::getInstance()->createCommand()->insert(self::TABLE_NAME, $data)->execute();

Reference to undeclared property \OSSMailView_GetRecordToMails_Relation->date (Did you mean $data)
Open

            $date = $this->date ?? \Vtiger_Record_Model::getInstanceById($sourceRecordId, 'OSSMailView')->get('date');

Call to undeclared method \App\Db::createCommand
Open

        return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $destinationRecordId, 'ossmailviewid' => $sourceRecordId])->execute();

Each class must be in a namespace of at least one level (a top-level vendor name)
Open

class OSSMailView_GetRecordToMails_Relation extends \App\Relation\RelationAbstraction

The class OSSMailView_GetRecordToMails_Relation is not named in CamelCase.
Open

class OSSMailView_GetRecordToMails_Relation extends \App\Relation\RelationAbstraction
{
    /**
     * @var string Name of the table that stores relations.
     */

CamelCaseClassName

Since: 0.2

It is considered best practice to use the CamelCase notation to name classes.

Example

class class_name {
}

Source

Only one argument is allowed per line in a multi-line function call
Open

        ['ossmailviewid' => $toRecordId], ['ossmailviewid' => $fromRecordId, 'crmid' => $relatedRecordId])->execute();

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 delete(int $sourceRecordId, int $destinationRecordId): bool

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

    {

Line exceeds 120 characters; contains 170 characters
Open

        return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $destinationRecordId, 'ossmailviewid' => $sourceRecordId])->execute();

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 $result;

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

            ->addJoin(['INNER JOIN', self::TABLE_NAME, self::TABLE_NAME . '.crmid = vtiger_crmentity.crmid'])

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

    }

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

    /** {@inheritdoc} */

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

    /** {@inheritdoc} */

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

     * @var string Name of the table that stores relations.

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 create(int $sourceRecordId, int $destinationRecordId): bool

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

        $result = false;

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

        if (!$this->isExists($data)) {

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

    /** {@inheritdoc} */

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 ($this->addRelation($data, $date) && ($parentId = \Users_Privileges_Model::getParentRecord($parentId))) {

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

            $result = $this->addToDB($data);

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

        return (bool) $return;

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

     * Add relation to DB.

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

     * Add relation if exists.

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

        $this->relationModel->getQueryGenerator()

Line exceeds 120 characters; contains 126 characters
Open

            ->addNativeCondition([self::TABLE_NAME . '.ossmailviewid' => $this->relationModel->get('parentRecord')->getId()]);

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) (new \App\Db\Query())->from(self::TABLE_NAME)->where($data)->exists();

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 array  $data

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) \App\Db::getInstance()->createCommand()->update(self::TABLE_NAME,

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

    }

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

    /** {@inheritdoc} */

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

    }

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 = $this->date ?? \Vtiger_Record_Model::getInstanceById($sourceRecordId, 'OSSMailView')->get('date');

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

                $data['crmid'] = $parentId;

Line exceeds 120 characters; contains 124 characters
Open

                if ($this->addRelation($data, $date) && ($parentId = \Users_Privileges_Model::getParentRecord($parentId))) {

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

                    $this->addRelation($data, $date);

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

     * @param array $data

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

     * @param array $data

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

     * @param string $date

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

        ['ossmailviewid' => $toRecordId], ['ossmailviewid' => $fromRecordId, 'crmid' => $relatedRecordId])->execute();

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

    /**

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

    /** {@inheritdoc} */

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

        return Vtiger_Relation_Model::RELATION_M2M;

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

     * Check if relation exists.

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 (bool) \App\Db::getInstance()->createCommand()->insert(self::TABLE_NAME, $data)->execute();

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) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $destinationRecordId, 'ossmailviewid' => $sourceRecordId])->execute();

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

                    $data['crmid'] = $parentId;

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 getQuery()

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

        if (!$this->isExists($data)) {

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

            $return = $this->addToDB(array_merge($data, ['date' => $date]));

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 addToDB(array $data): bool

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

            $data['date'] = $date;

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

    public function transfer(int $relatedRecordId, int $fromRecordId, int $toRecordId): bool

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

    public const TABLE_NAME = 'vtiger_ossmailview_relation';

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

        $return = false;

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

            if ($return && ($parentId = \Users_Privileges_Model::getParentRecord($destinationRecordId))) {

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

    public function getRelationType(): int

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

            ->addNativeCondition([self::TABLE_NAME . '.ossmailviewid' => $this->relationModel->get('parentRecord')->getId()]);

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

        $data = ['ossmailviewid' => $sourceRecordId, 'crmid' => $destinationRecordId];

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

    public function isExists(array $data): bool

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

        }

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 addRelation(array $data, string $date): bool

Class name "OSSMailView_GetRecordToMails_Relation" is not in camel caps format
Open

class OSSMailView_GetRecordToMails_Relation extends \App\Relation\RelationAbstraction

Opening parenthesis of a multi-line function call must be the last content on the line
Open

        return (bool) \App\Db::getInstance()->createCommand()->update(self::TABLE_NAME,

Closing parenthesis of a multi-line function call must be on a line by itself
Open

        ['ossmailviewid' => $toRecordId], ['ossmailviewid' => $fromRecordId, 'crmid' => $relatedRecordId])->execute();

There are no issues that match your filters.

Category
Status