Issues (3627)

CampaignBundle/Entity/SlaveConnectionTrait.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CampaignBundle\Entity;
13
14
use Doctrine\DBAL\Connection;
15
use Doctrine\DBAL\Connections\MasterSlaveConnection;
16
use Mautic\CampaignBundle\Executioner\ContactFinder\Limiter\ContactLimiter;
17
18
/**
19
 * Trait SlaveConnectionTrait.
20
 */
21
trait SlaveConnectionTrait
22
{
23
    /**
24
     * Get a connection, preferring a slave connection if available and prudent.
25
     *
26
     * If a query is being executed with a limiter with specific contacts
27
     * then this could be a real-time request being handled so we should avoid forcing a slave connection.
28
     *
29
     * @return Connection
30
     */
31
    private function getSlaveConnection(ContactLimiter $limiter = null)
32
    {
33
        /** @var Connection $connection */
34
        $connection = $this->getEntityManager()->getConnection();
0 ignored issues
show
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $connection = $this->/** @scrutinizer ignore-call */ getEntityManager()->getConnection();
Loading history...
35
        if ($connection instanceof MasterSlaveConnection) {
36
            if (
37
                !$limiter
38
                || !($limiter->getContactId() || $limiter->getContactIdList())
0 ignored issues
show
Bug Best Practice introduced by
The expression $limiter->getContactId() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
39
            ) {
40
                $connection->connect('slave');
41
            }
42
        }
43
44
        return $connection;
45
    }
46
}
47