YetiForceCompany/YetiForceCRM

View on GitHub
modules/com_vtiger_workflow/VTTaskManager.php

Summary

Maintainability
A
45 mins
Test Coverage
F
56%

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

    public function getTasksForWorkflow($workflowId, $active = true)
    {
        $cacheName = "{$workflowId}:{$active}";
        if (\App\Cache::staticHas('getTasksForWorkflow', $cacheName)) {
            return \App\Cache::staticGet('getTasksForWorkflow', $cacheName);
Severity: Minor
Found in modules/com_vtiger_workflow/VTTaskManager.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 '92', column '15').
Open

        $row = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['task_id' => $taskId])->one();

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

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

    public function getTasksForWorkflow($workflowId, $active = true)

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

Missing class import via use statement (line '114', column '22').
Open

        $dataReader = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['workflow_id' => $workflowId])->orderBy(['sequence' => SORT_ASC])->createCommand()->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

Avoid using static access to class 'VTTaskType' in method 'createTask'.
Open

        $taskTypeInstance = VTTaskType::getInstanceFromTaskType($taskType);

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 'getTasksForWorkflow'.
Open

        if (\App\Cache::staticHas('getTasksForWorkflow', $cacheName)) {

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 'getTasksForWorkflow'.
Open

            return \App\Cache::staticGet('getTasksForWorkflow', $cacheName);

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 'getTasksForWorkflow'.
Open

        \App\Cache::staticSave('getTasksForWorkflow', $cacheName, $tasks);

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\Db' in method 'saveTask'.
Open

        $db = App\Db::getInstance();

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 "com_vtiger_workflowtasks" 5 times.
Open

            $db->createCommand()->update('com_vtiger_workflowtasks', ['summary' => $task->summary, 'task' => serialize($task)], ['task_id' => $taskId])->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 "workflow_id" 6 times.
Open

            'workflow_id' => $task->workflowId,

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

            $db->createCommand()->update('com_vtiger_workflowtasks', ['summary' => $task->summary, 'task' => serialize($task)], ['task_id' => $taskId])->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 "getTasksForWorkflow" 3 times.
Open

        if (\App\Cache::staticHas('getTasksForWorkflow', $cacheName)) {

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.

Return type of saveTask() is undeclared type \The
Open

    public function saveTask(VTTask $task)

Reference to undeclared property \VTTask->workflowId
Open

            'workflow_id' => $task->workflowId,

Suspicious type bool|true of a variable or expression used to build a string. (Expected type to be able to cast to a string)
Open

        $cacheName = "{$workflowId}:{$active}";

Returning type array|bool but retrieveTask() is declared to return \VTTask
Open

        return $task;

Expected an object instance when accessing an instance property, but saw an expression with type array|bool
Open

        $task->workflowId = $row['workflow_id'];

Reference to undeclared property \VTTask->id
Open

        if (!empty($task->id) && is_numeric($task->id)) {

Reference to undeclared property \VTTask->summary
Open

            $db->createCommand()->update('com_vtiger_workflowtasks', ['summary' => $task->summary, 'task' => serialize($task)], ['task_id' => $taskId])->execute();

Reference to undeclared property \VTTask->id
Open

            $taskId = $task->id;

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

        $dataReader = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['workflow_id' => $workflowId])->orderBy(['sequence' => SORT_ASC])->createCommand()->query();

Reference to undeclared property \VTTask->email
Open

            if (isset($task->email) && !\is_array($task->email)) {

Reference to undeclared property \VTTask->summary
Open

            'summary' => $task->summary,

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

            $db->createCommand()->update('com_vtiger_workflowtasks', ['summary' => $task->summary, 'task' => serialize($task)], ['task_id' => $taskId])->execute();

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

        $db->createCommand()->insert('com_vtiger_workflowtasks', [

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

        App\Db::getInstance()->createCommand()->delete('com_vtiger_workflowtasks', ['task_id' => $taskId])->execute();

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

        $row = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['task_id' => $taskId])->one();

Reference to undeclared property \VTTask->sequence
Open

            'sequence' => $task->sequence,

Returning type string but saveTask() is declared to return \The
Open

        return $db->getLastInsertID();

Reference to undeclared property \VTTask->email
Open

                $task->email = [$task->email];

Expected an object instance when accessing an instance property, but saw an expression with type array|bool
Open

        $task->id = $row['task_id'];

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

class VTTaskManager

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 17 and the first side effect is on line 11.
Open

<?php

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

        $db = App\Db::getInstance();

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

        if (!empty($task->id) && is_numeric($task->id)) {

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

            //How do I check whether a member exists in php?

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

     * Delete task by id.

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

     * @param int $taskId

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

        require_once $taskTypeInstance->get('classpath');

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 tasks for workflow.

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

            $task = unserialize($row['task']);

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

     * Retrieve a task from the database.

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

        $row = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['task_id' => $taskId])->one();

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 (!empty($taskType)) {

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 (isset($task->email) && !\is_array($task->email)) {

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

        return $task;

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

    public function unserializeTask($str)

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

     * Save the task into the database.

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

            $taskId = $task->id;

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

            'task' => serialize($task),

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

            'sequence' => $task->sequence,

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

    /**

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

        $task->active = 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

        }

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

                require_once "tasks/$taskType.php";

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

        $taskType = self::taskName($str);

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 VTTask

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 getTasksForWorkflow($workflowId, $active = true)

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

        $dataReader = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['workflow_id' => $workflowId])->orderBy(['sequence' => SORT_ASC])->createCommand()->query();

Line exceeds 120 characters; contains 222 characters
Open

        $dataReader = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['workflow_id' => $workflowId])->orderBy(['sequence' => SORT_ASC])->createCommand()->query();

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

        while ($row = $dataReader->read()) {

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

     * @param VTTask $task The task instance to save

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

    public function saveTask(VTTask $task)

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

            }

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

            $db->createCommand()->update('com_vtiger_workflowtasks', ['summary' => $task->summary, 'task' => serialize($task)], ['task_id' => $taskId])->execute();

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

            return $taskId;

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

        ])->execute();

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

        App\Db::getInstance()->createCommand()->delete('com_vtiger_workflowtasks', ['task_id' => $taskId])->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

        $db = App\Db::getInstance();

Line exceeds 120 characters; contains 163 characters
Open

            $db->createCommand()->update('com_vtiger_workflowtasks', ['summary' => $task->summary, 'task' => serialize($task)], ['task_id' => $taskId])->execute();

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

            'workflow_id' => $task->workflowId,

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 $taskType

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

     *

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

        $taskTypeInstance = VTTaskType::getInstanceFromTaskType($taskType);

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

    {

Line exceeds 120 characters; contains 153 characters
Open

        $row = (new \App\Db\Query())->select(['task_id', 'workflow_id', 'task'])->from('com_vtiger_workflowtasks')->where(['task_id' => $taskId])->one();

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

        $task->workflowId = $row['workflow_id'];

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

        return $task;

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

        $cacheName = "{$workflowId}:{$active}";

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

     * Create a new class instance.

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

     *

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

        $taskClass = $taskTypeInstance->get('classname');

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

        $task = new $taskClass();

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

        $task = $this->unserializeTask($row['task']);

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

     * @param bool $active

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

        \App\Cache::staticSave('getTasksForWorkflow', $cacheName, $tasks);

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

     * @param string $str

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

     * When a new task is saved for the first time a field is added to it called

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

        $task->workflowId = $workflowId;

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

    public function retrieveTask($taskId)

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

        $task->id = $row['task_id'];

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

            $task->workflowId = $row['workflow_id'];

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

     * Unserialize task string.

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

     * @return The id of the task

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

        $db->createCommand()->insert('com_vtiger_workflowtasks', [

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 (\App\Cache::staticHas('getTasksForWorkflow', $cacheName)) {

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

        $tasks = [];

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

            $taskType = self::taskName($row['task']);

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

                continue;

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

            $task->id = $row['task_id'];

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

            if ($active && !$task->active) {

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

            'summary' => $task->summary,

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

     * @param int    $workflowId

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

        $task->summary = '';

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

     * @param int  $workflowId

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

        return $tasks;

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

     * id that stores the task id used in the database.

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

                $task->email = [$task->email];

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

        return $db->getLastInsertID();

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

    public function deleteTask($taskId)

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

     * @return VTTask The retrieved task

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 createTask($taskType, $workflowId)

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

     * @param $taskId The id of the task to retrieve

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

            return \App\Cache::staticGet('getTasksForWorkflow', $cacheName);

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

            $tasks[] = $task;

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 (!empty($taskType)) {

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

     * @return string

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

        return $matches[1];

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

        return unserialize($str);

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

            require_once "tasks/$taskType.php";

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 static function taskName($serializedTask)

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 $serializedTask

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

     * Return task name.

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

        preg_match('/"([^"]+)"/', $serializedTask, $matches);

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

        $matches = [];

There are no issues that match your filters.

Category
Status