Completed
Pull Request — master (#195)
by Robbie
02:05
created

CommentsGridFieldTest::testNewRow()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 4
nop 0
1
<?php
2
3
namespace SilverStripe\Comments\Tests;
4
5
use ReflectionClass;
6
use ReflectionException;
7
use SilverStripe\Comments\Model\Comment;
8
use SilverStripe\Comments\Admin\CommentsGridField;
9
use SilverStripe\Dev\SapphireTest;
10
11
class CommentsGridFieldTest extends SapphireTest
12
{
13
    public function testNewRow()
14
    {
15
        $gridfield = new CommentsGridField('testfield', 'testfield');
16
        //   protected function newRow($total, $index, $record, $attributes, $content) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
17
        $comment = new Comment();
18
        $comment->Name = 'Fred Bloggs';
19
        $comment->Comment = 'This is a comment';
20
        $attr = array();
21
22
        try {
23
            $class  = new ReflectionClass($gridfield);
24
            $method = $class->getMethod('newRow');
25
            $method->setAccessible(true);
26
        } catch (ReflectionException $e) {
27
            $this->fail($e->getMessage());
28
        }
29
30
        $params = array(1, 1, $comment, $attr, $comment->Comment);
31
        $newRow = $method->invokeArgs($gridfield, $params);
0 ignored issues
show
Bug introduced by
The variable $method 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...
32
        $this->assertEquals('<tr>This is a comment</tr>', $newRow);
33
34
        $attr = array('class' => 'cssClass');
35
        $params = array(1, 1, $comment, $attr, $comment->Comment);
36
        $newRow = $method->invokeArgs($gridfield, $params);
37
        $this->assertEquals('<tr class="cssClass">This is a comment</tr>', $newRow);
38
39
        $comment->markSpam();
40
        $params = array(1, 1, $comment, $attr, $comment->Comment);
41
        $newRow = $method->invokeArgs($gridfield, $params);
42
        $this->assertEquals('<tr class="cssClass spam">This is a comment</tr>', $newRow);
43
    }
44
}
45