YetiForceCompany/YetiForceCRM

View on GitHub
modules/Notification/crons/Notifications.php

Summary

Maintainability
A
45 mins
Test Coverage
F
13%

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

    private function executeScheduled($row)
    {
        $currentTime = time();
        $timestampEndDate = empty($row['last_execution']) ? $currentTime : strtotime($row['last_execution'] . ' +' . $row['frequency'] . 'min');
        if ($currentTime >= $timestampEndDate) {
Severity: Minor
Found in modules/Notification/crons/Notifications.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 '24', column '17').
Open

        $query = (new \App\Db\Query())->from('u_#__watchdog_schedule');

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 '\Vtiger_Record_Model' in method 'markSentNotificationsAsRead'.
Open

                $noticeRecordModel = \Vtiger_Record_Model::getInstanceById($notification->get('notificationid'), 'Notification');

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 'Notification_Module_Model' in method 'existNotifications'.
Open

            return Notification_Module_Model::getEmailSendEntries($userId, $modules, $startDate, $endDate);

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\Privilege' in method 'executeScheduled'.
Open

            if (\App\Privilege::isPermitted(self::MODULE_NAME, 'ReceivingMailNotifications', false, $row['userid']) && $notificationsToSend) {

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\Config' in method 'executeScheduled'.
Open

                if ($ifEmailSend && \App\Config::module('Notification', 'AUTO_MARK_NOTIFICATIONS_READ_AFTER_EMAIL_SEND')) {

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\Mailer' in method 'executeScheduled'.
Open

                $ifEmailSend = \App\Mailer::sendFromTemplate([
                    'moduleName' => 'Notification',
                    'template' => 'SendNotificationsViaMail',
                    'to' => \App\User::getUserModel($row['userid'])->getDetail('email1'),
                    'startDate' => $row['last_execution'],

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 'Vtiger_Watchdog_Model' in method 'existNotifications'.
Open

        $scheduleData = Vtiger_Watchdog_Model::getWatchingModulesSchedule($userId, 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

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

        $timestampEndDate = empty($row['last_execution']) ? $currentTime : strtotime($row['last_execution'] . ' +' . $row['frequency'] . 'min');

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

    const MODULE_NAME = 'Notification';

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

            $notificationsToSend = $this->existNotifications($row['userid'], $row['last_execution'], $endDate);

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

                    'to' => \App\User::getUserModel($row['userid'])->getDetail('email1'),

Argument 1 (currentTime) is int but \Notification_Notifications_Cron::getEndDate() takes string defined at /code/modules/Notification/crons/Notifications.php:91
Open

            $endDate = $this->getEndDate($currentTime, $timestampEndDate, $row['frequency']);

Call to undeclared method \Vtiger_Record_Model::setMarked
Open

                $noticeRecordModel->setMarked();

Argument 2 (timestampEndDate) is false|int but \Notification_Notifications_Cron::getEndDate() takes string defined at /code/modules/Notification/crons/Notifications.php:91
Open

            $endDate = $this->getEndDate($currentTime, $timestampEndDate, $row['frequency']);

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

        $query = (new \App\Db\Query())->from('u_#__watchdog_schedule');

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

            \App\Db::getInstance()->createCommand()

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

class Notification_Notifications_Cron extends \App\CronHandler

The class Notification_Notifications_Cron is not named in CamelCase.
Open

class Notification_Notifications_Cron extends \App\CronHandler
{
    const MODULE_NAME = 'Notification';

    /** {@inheritdoc} */

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

    const MODULE_NAME = 'Notification';

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

        $dataReader = $query->createCommand()->query();

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

            if (\App\Privilege::isPermitted(self::MODULE_NAME, 'ReceivingMailNotifications', false, $row['userid']) && $notificationsToSend) {

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

                    'moduleName' => 'Notification',

Line exceeds 120 characters; contains 123 characters
Open

                if ($ifEmailSend && \App\Config::module('Notification', 'AUTO_MARK_NOTIFICATIONS_READ_AFTER_EMAIL_SEND')) {

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

                }

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

            \App\Db::getInstance()->createCommand()

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

                ->update('u_#__watchdog_schedule', ['last_execution' => $endDate], ['userid' => $row['userid']])

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

    {

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

        $currentTime = time();

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

        $timestampEndDate = empty($row['last_execution']) ? $currentTime : strtotime($row['last_execution'] . ' +' . $row['frequency'] . 'min');

Line exceeds 120 characters; contains 144 characters
Open

        $timestampEndDate = empty($row['last_execution']) ? $currentTime : strtotime($row['last_execution'] . ' +' . $row['frequency'] . 'min');

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

            $endDate = $this->getEndDate($currentTime, $timestampEndDate, $row['frequency']);

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

                if ($ifEmailSend && \App\Config::module('Notification', 'AUTO_MARK_NOTIFICATIONS_READ_AFTER_EMAIL_SEND')) {

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

     * Function set notifications as read.

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

                $noticeRecordModel = \Vtiger_Record_Model::getInstanceById($notification->get('notificationid'), 'Notification');

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

                    $this->markSentNotificationsAsRead($notificationsToSend);

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

        $scheduleData = Vtiger_Watchdog_Model::getWatchingModulesSchedule($userId, 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 string

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

    private function getEndDate($currentTime, $timestampEndDate, $frequency)

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

        $dataReader->close();

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

            $notificationsToSend = $this->existNotifications($row['userid'], $row['last_execution'], $endDate);

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

     *

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

     * Function get date.

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

     * @param string $currentTime

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

     */

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

        while ($timestampEndDate <= $currentTime && ($nextEndDateTime = $timestampEndDate + ($frequency * 60)) <= $currentTime) {

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

            foreach ($notificationsType as $notification) {

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

                $ifEmailSend = \App\Mailer::sendFromTemplate([

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 [];

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

        return date('Y-m-d H:i:s', $timestampEndDate);

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

    /**

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

    private function markSentNotificationsAsRead($notificationsToSend): void

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

        if ($currentTime >= $timestampEndDate) {

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

        $query = (new \App\Db\Query())->from('u_#__watchdog_schedule');

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

    private function executeScheduled($row)

Line exceeds 120 characters; contains 142 characters
Open

            if (\App\Privilege::isPermitted(self::MODULE_NAME, 'ReceivingMailNotifications', false, $row['userid']) && $notificationsToSend) {

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

     * @param string $startDate

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

        if ($scheduleData) {

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

            $modules = $scheduleData['modules'];

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

     * @param array $row

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

                    'template' => 'SendNotificationsViaMail',

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

                    'to' => \App\User::getUserModel($row['userid'])->getDetail('email1'),

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

            $timestampEndDate = $nextEndDateTime;

Line exceeds 120 characters; contains 129 characters
Open

                $noticeRecordModel = \Vtiger_Record_Model::getInstanceById($notification->get('notificationid'), 'Notification');

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

    }

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

     *

Line exceeds 120 characters; contains 129 characters
Open

        while ($timestampEndDate <= $currentTime && ($nextEndDateTime = $timestampEndDate + ($frequency * 60)) <= $currentTime) {

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

     */

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

                $noticeRecordModel->setMarked();

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

    public function process()

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

            $this->executeScheduled($row);

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

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

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

        }

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

                    'userId' => $row['userid'],

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

    private function existNotifications($userId, $startDate, $endDate): 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

    {

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

     * @param int    $frequency

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

        }

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

        foreach ($notificationsToSend as $notificationsType) {

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

     * Function executes the sending notifications action.

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

                    'startDate' => $row['last_execution'],

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

                    'endDate' => $endDate,

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

     * Function checks if notification exists.

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

     * @param int    $userId

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

            return Notification_Module_Model::getEmailSendEntries($userId, $modules, $startDate, $endDate);

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

     * @param mixed $notificationsToSend

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

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

     * @param string $timestampEndDate

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

class Notification_Notifications_Cron extends \App\CronHandler

There are no issues that match your filters.

Category
Status