EmailTest::testALotOfSaveEmails()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
require_once 'Intraface/shared/email/Email.php';
3
4
class EmailTest extends PHPUnit_Framework_TestCase
5
{
6
7
    private $kernel;
8
9
    function setUp()
10
    {
11
        $this->kernel = new Stub_Kernel;
12
    }
13
14
    function testConstruction()
15
    {
16
        $email = new Email($this->kernel);
17
        $this->assertTrue(is_object($email));
18
    }
19
20 View Code Duplication
    function testSave()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $belong_to_id = rand(1, 100000);
23
        $type_id = rand(1, 5);
24
        $contact_id = rand(1, 100000);
25
        $data = array(
26
            'subject' => 'Some subject',
27
            'body' => 'some body',
28
            'from_name' => 'x',
29
            'from_email' => '[email protected]',
30
            'belong_to' => $belong_to_id,
31
            'type_id' => $type_id,
32
            'contact_id' => $contact_id,
33
            'deadline' => date('Y-m-d H:i:s')
34
        );
35
        $email = new Email($this->kernel);
36
        $return = $email->save($data);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,string|inte...","deadline":"string"}>, but the function expects a object<Struct>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
38
        $this->assertTrue((is_numeric($return) and $return > 0));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
39
        $this->assertEquals($email->get('subject'), $data['subject']);
40
        $this->assertEquals($email->get('body'), $data['body']);
41
        $this->assertEquals($email->get('belong_to_id'), $data['belong_to']);
42
        $this->assertEquals($email->get('type_id'), $data['type_id']);
43
        $this->assertEquals($email->get('contact_id'), $data['contact_id']);
44
        $this->assertEquals($email->get('from_name'), $data['from_name']);
45
        $this->assertEquals($email->get('from_email'), $data['from_email']);
46
        $this->assertEquals($email->get('user_id'), $this->kernel->user->get('id'));
0 ignored issues
show
Unused Code introduced by
The call to Stub_User::get() has too many arguments starting with 'id'.

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...
47
    }
48
49 View Code Duplication
    function _testSaveWithEmptyFrom()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $belong_to_id = rand(1, 100000);
52
        $type_id = rand(1, 5);
53
        $contact_id = rand(1, 100000);
54
        $data = array(
55
            'subject' => 'Some subject',
56
            'body' => 'some body',
57
            'from_name' => '',
58
            'from_email' => '',
59
            'belong_to' => $belong_to_id,
60
            'type_id' => $type_id,
61
            'contact_id' => $contact_id,
62
            'deadline' => date('Y-m-d H:i:s')
63
        );
64
        $email = new Email($this->kernel);
65
        $return = $email->save($data);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,string|inte...","deadline":"string"}>, but the function expects a object<Struct>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
67
        $this->assertTrue((is_numeric($return) and $return > 0));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
68
        $this->assertEquals($email->get('subject'), $data['subject']);
69
        $this->assertEquals($email->get('body'), $data['body']);
70
        $this->assertEquals($email->get('belong_to_id'), $data['belong_to']);
71
        $this->assertEquals($email->get('type_id'), $data['type_id']);
72
        $this->assertEquals($email->get('contact_id'), $data['contact_id']);
73
        $this->assertEquals($email->get('from_name'), $data['from_name']);
74
        $this->assertEquals($email->get('from_email'), $data['from_email']);
75
        $this->assertEquals($email->get('user_id'), $this->kernel->user->get('id'));
0 ignored issues
show
Unused Code introduced by
The call to Stub_User::get() has too many arguments starting with 'id'.

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...
76
    }
77
78
    function testALotOfSaveEmails()
79
    {
80
        $number = 200;
81
        for ($i = 0; $i<$number; $i++) {
82
            $belong_to_id = rand(1, 100000);
83
            $type_id = rand(1, 5);
84
            $contact_id = rand(1, 100000);
85
            $data = array(
86
                'subject' => 'Some subject',
87
                'body' => 'some body',
88
                'from_name' => 'x',
89
                'from_email' => '[email protected]',
90
                'belong_to' => $belong_to_id,
91
                'type_id' => $type_id,
92
                'contact_id' => $contact_id,
93
                'deadline' => date('Y-m-d H:i:s')
94
            );
95
            $email = new Email($this->kernel);
96
            $return = $email->save($data);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,string|inte...","deadline":"string"}>, but the function expects a object<Struct>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
        }
98
        $this->assertEquals($i, $number);
99
    }
100
}
101