YetiForceCompany/YetiForceCRM

View on GitHub
modules/OSSMailScanner/models/PrefixScannerAction.php

Summary

Maintainability
A
1 hr
Test Coverage
F
0%

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

    public function findAndBind()
    {
        $mailId = $this->mail->getMailCrmId();
        $recordId = 0;
        if ($mailId) {
Severity: Minor
Found in modules/OSSMailScanner/models/PrefixScannerAction.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

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

    protected function add()
    {
        $returnIds = [];
        $crmId = false;
        if (\App\Cache::has('getRecordByPrefix', $this->prefix)) {
Severity: Minor
Found in modules/OSSMailScanner/models/PrefixScannerAction.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 '79', column '18').
Open

            $crmId = (new \App\Db\Query())

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

Missing class import via use statement (line '122', column '25').
Open

        $queryGenerator = new App\QueryGenerator($this->moduleName);

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

Missing class import via use statement (line '90', column '19').
Open

            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('date'));

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

Missing class import via use statement (line '107', column '19').
Open

            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('udate_formated'));

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

Missing class import via use statement (line '44', column '18').
Open

            $query = (new \App\Db\Query())->select(['vtiger_ossmailview_relation.crmid'])

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 using static access to class 'CRMEntity' in method 'add'.
Open

            $moduleObject = CRMEntity::getInstance($this->moduleName);

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\Cache' in method 'add'.
Open

            $crmId = \App\Cache::get('getRecordByPrefix', $this->prefix);

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\Mail\RecordFinder' in method 'findAndBind'.
Open

                } elseif (\Config\Modules\OSSMailScanner::$searchPrefixInBody && ($this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('body'), $this->moduleName, true))) {

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 '106', column '31').
Open

    protected function addByBody()
    {
        $returnIds = [];
        if ($this->prefix && !empty($crmId = $this->findRecord())) {
            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('udate_formated'));

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\Cache' in method 'add'.
Open

        if (\App\Cache::has('getRecordByPrefix', $this->prefix)) {

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

            } else {
                $this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('subject'), $this->moduleName);
                if ($this->prefix) {
                    $recordId = $this->add();
                } elseif (\Config\Modules\OSSMailScanner::$searchPrefixInBody && ($this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('body'), $this->moduleName, true))) {

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

    public function findAndBind()
    {
        $mailId = $this->mail->getMailCrmId();
        $recordId = 0;
        if ($mailId) {

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\Mail\RecordFinder' in method 'findAndBind'.
Open

                $this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('subject'), $this->moduleName);

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

        } else {
            $moduleObject = CRMEntity::getInstance($this->moduleName);
            $tableIndex = $moduleObject->tab_name_index[$this->tableName];
            $crmId = (new \App\Db\Query())
                ->select([$tableIndex])

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 '\App\Cache' in method 'add'.
Open

                \App\Cache::save('getRecordByPrefix', $this->prefix, $crmId, \App\Cache::LONG);

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 "getRecordByPrefix" 3 times.
Open

        if (\App\Cache::has('getRecordByPrefix', $this->prefix)) {

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\Query::select
Open

            $query = (new \App\Db\Query())->select(['vtiger_ossmailview_relation.crmid'])

Reference to static property searchPrefixInBody from undeclared class \Config\Modules\OSSMailScanner
Open

                } elseif (\Config\Modules\OSSMailScanner::$searchPrefixInBody && ($this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('body'), $this->moduleName, true))) {

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

        return $queryGenerator->createQuery()->scalar() ?? false;

Returning type false but findRecord() is declared to return int|null
Open

        return $queryGenerator->createQuery()->scalar() ?? false;

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

            $crmId = (new \App\Db\Query())

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

        $queryGenerator->addNativeCondition([$this->tableName . '.' . $this->tableColumn => $this->prefix]);

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

        $queryGenerator = new App\QueryGenerator($this->moduleName);

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

        $queryGenerator->setOrder('modifiedtime', 'DESC');

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

abstract class OSSMailScanner_PrefixScannerAction_Model

The class OSSMailScanner_PrefixScannerAction_Model is not named in CamelCase.
Open

abstract class OSSMailScanner_PrefixScannerAction_Model
{
    public $prefix;
    /**
     * @var string Module name

CamelCaseClassName

Since: 0.2

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

Example

class class_name {
}

Source

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

    public $prefix;

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

            if (!empty($returnIds)) {

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

     * Add.

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

     *

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

            $crmId = \App\Cache::get('getRecordByPrefix', $this->prefix);

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

                $returnIds[] = $crmId;

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

        return $returnIds;

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

        $returnIds = [];

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

            $crmId = (new \App\Db\Query())

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

                ->from($this->tableName)

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

        return $queryGenerator->createQuery()->scalar() ?? false;

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

    /**

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

     * Process.

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

                ->from('vtiger_ossmailview_relation')

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

            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('udate_formated'));

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

            if ($status) {

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

     * @param OSSMail_Mail_Model $mail

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

        if ($mailId) {

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

                $recordId = $returnIds;

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

            $moduleObject = CRMEntity::getInstance($this->moduleName);

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

        $returnIds = [];

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

        return $returnIds;

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

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

        if (\App\Cache::has('getRecordByPrefix', $this->prefix)) {

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

        if ($crmId) {

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

        if ($this->prefix && !empty($crmId = $this->findRecord())) {

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

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

    public $tableName;

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

                $this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('subject'), $this->moduleName);

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

                    $recordId = $this->addByBody();

Line exceeds 120 characters; contains 133 characters
Open

            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('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

        }

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

        $queryGenerator->addNativeCondition([$this->tableName . '.' . $this->tableColumn => $this->prefix]);

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

     * @var string Module name

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

    public function findAndBind()

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

                ->where(['ossmailviewid' => $mailId, 'setype' => $this->moduleName])

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

                ->andWhere(['<>', 'vtiger_crmentity.deleted', 1])

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

            $tableIndex = $moduleObject->tab_name_index[$this->tableName];

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

        $crmId = false;

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

                ->scalar();

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

    protected function addByBody()

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

                $returnIds[] = $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

                    $recordId = $this->add();

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

                } elseif (\Config\Modules\OSSMailScanner::$searchPrefixInBody && ($this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('body'), $this->moduleName, true))) {

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

            }

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

    protected function add()

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

                ->where(['vtiger_crmentity.deleted' => 0, $this->tableName . '.' . $this->tableColumn => $this->prefix])

Line exceeds 120 characters; contains 143 characters
Open

            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('udate_formated'));

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

     * Find record by prefix.

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|int|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

            $query = (new \App\Db\Query())->select(['vtiger_ossmailview_relation.crmid'])

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

                if ($this->prefix) {

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

     */

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

            $status = (new OSSMailView_Relation_Model())->addRelation($this->mail->getMailCrmId(), $crmId, $this->mail->get('date'));

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

    public function findRecord()

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

     * @var OSSMail_Mail_Model Mail instance

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

        $recordId = 0;

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

                ->orderBy(['modifiedtime' => \SORT_DESC]);

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

                ->innerJoin('vtiger_crmentity', "$this->tableName.$tableIndex = 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

    }

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

     * @return int|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

    abstract public function process(OSSMail_Mail_Model $mail);

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

                ->innerJoin('vtiger_crmentity', 'vtiger_crmentity.crmid = vtiger_ossmailview_relation.crmid')

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

            if ($crmId) {

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

                \App\Cache::save('getRecordByPrefix', $this->prefix, $crmId, \App\Cache::LONG);

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

     * Add by body.

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

        $mailId = $this->mail->getMailCrmId();

Line exceeds 120 characters; contains 130 characters
Open

                $this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('subject'), $this->moduleName);

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

        $queryGenerator = new App\QueryGenerator($this->moduleName);

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

    public $mail;

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

            } else {

Line exceeds 120 characters; contains 202 characters
Open

                } elseif (\Config\Modules\OSSMailScanner::$searchPrefixInBody && ($this->prefix = \App\Mail\RecordFinder::getRecordNumberFromString($this->mail->get('body'), $this->moduleName, true))) {

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

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

                ->select([$tableIndex])

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

            if ($status) {

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

     * Find and bind.

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

    {

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

            $returnIds = $query->column();

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

        $queryGenerator->setOrder('modifiedtime', 'DESC');

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

abstract class OSSMailScanner_PrefixScannerAction_Model

There are no issues that match your filters.

Category
Status