PdfLabelTest::testConstruction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
require_once 'Intraface/modules/contact/PdfLabel.php';
3
require_once 'Intraface/Pdf.php';
4
5
class PdfLabelTest extends PHPUnit_Framework_TestCase
6
{
7
    function setup()
8
    {
9
        if (file_exists(TEST_PATH_TEMP.'pdf_label.pdf')) {
10
            unlink(TEST_PATH_TEMP.'pdf_label.pdf');
11
        }
12
    }
13
14
    function getPdfLabel()
15
    {
16
        return new Intraface_modules_contact_PdfLabel(0);
17
    }
18
19
    /////////////////////////////////////////////////////////
20
21
    function testConstruction()
22
    {
23
        $pdf = $this->getPdfLabel();
24
        $this->assertTrue(is_object($pdf));
25
    }
26
27
    function testGenerate()
28
    {
29
        $this->markTestSkipped(
30
            'This test is not passing.'
31
        );
32
        $pdf = $this->getPdfLabel();
33
34
        for ($i = 0; $i < 10; $i++) {
35
            $contacts[$i] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$contacts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $contacts = 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...
36
                'number' => $i,
37
                'name' => 'Test'.$i,
38
                'address' => array('address' => 'Vej '.$i,
39
                    'postcode' => '100'.$i,
40
                    'city' => 'By '.$i,
41
                    'country' => 'Land '.$i
42
                )
43
            );
44
        }
45
46
        $pdf->generate($contacts, 'random search', array('keyword1', 'keyword2'));
0 ignored issues
show
Bug introduced by
The variable $contacts 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...
47
48
        // file_put_contents(TEST_PATH_TEMP.'pdf_label.pdf', $pdf->output());
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
50
        $this->assertEquals(strlen(file_get_contents(dirname(__FILE__) .'/expected/pdf_label.pdf', 1)), strlen($pdf->output()));
51
    }
52
}
53