Completed
Push — master ( 6ffe9e...612694 )
by Gabriel
03:27
created

RecordsTrait::reduceEmailsByType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Nip\MailModule\Models\EmailsTable\Traits\Cleanup;
4
5
use Nip\Database\Query\Update;
6
7
/**
8
 * Trait RecordsTrait
9
 * @package Nip\Mail\Models\Cleanup
10
 */
11
trait RecordsTrait
12
{
13
    protected static $sentDateField = 'date_sent';
14
15
    protected $daysToKeepData = 365;
16
17
    /**
18
     * @return \Nip\Database\Result
19
     */
20 1
    public function reduceOldEmailsData()
21
    {
22
        /** @var Update $query */
23 1
        $query = $this->newUpdateQuery();
0 ignored issues
show
Bug introduced by
It seems like newUpdateQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24 1
        $query->where(
25 1
            '`' . $this::getSentDateField() . '` <= DATE_SUB(CURRENT_DATE(), INTERVAL ' . $this->daysToKeepData . ' DAY)'
26
        );
27 1
        $query->data([
0 ignored issues
show
Unused Code introduced by
The call to Update::data() has too many arguments starting with array('vars' => '', 'bod... 'compiled_body' => '').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28 1
            'vars' => '',
29
            'body' => '',
30
            'compiled_subject' => '',
31
            'compiled_body' => '',
32
        ]);
33
34 1
        return $query->execute();
35
    }
36
37
    /**
38
     * @return string
39
     */
40 2
    public static function getSentDateField(): string
41
    {
42 2
        return self::$sentDateField;
43
    }
44
45
    /**
46
     * @param string $sentDateField
47
     */
48
    public static function setSentDateField(string $sentDateField)
49
    {
50
        self::$sentDateField = $sentDateField;
51
    }
52
53
    /**
54
     * @return float[]|int[]
55
     */
56
    public static function reduceEmailsByType()
57
    {
58
        if (method_exists())
59
        $types[            '*'] = 365 * 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$types was never initialized. Although not strictly required by PHP, it is generally a good practice to add $types = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
60
        return $types;
0 ignored issues
show
Bug introduced by
The variable $types does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
61
    }
62
}
63