Completed
Push — master ( 02d548...e42a4c )
by Damian
02:25
created

CommentsGridFieldTest::testNewRow()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 35
rs 8.8571
cc 2
eloc 23
nc 4
nop 0
1
<?php
2
3
class CommentsGridFieldTest extends SapphireTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
	public function testNewRow() {
5
	   $gridfield = new CommentsGridField('testfield', 'testfield');
6
       //   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...
7
       $comment = new Comment();
8
       $comment->Name = 'Fred Bloggs';
9
       $comment->Comment = 'This is a comment';
10
       $attr = array();
11
12
13
       try {
14
            $class  = new ReflectionClass($gridfield);
15
            $method = $class->getMethod('newRow');
16
            $method->setAccessible(true);
17
        }
18
19
        catch (ReflectionException $e) {
20
            $this->fail($e->getMessage());
0 ignored issues
show
Bug introduced by
The method fail() does not seem to exist on object<CommentsGridFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
        }
22
23
        $params = array(1, 1, $comment, $attr, $comment->Comment);
24
        $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...
25
        $this->assertEquals('<tr>This is a comment</tr>', $newRow);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
27
        $attr = array('class' => 'cssClass');
28
        $params = array(1, 1, $comment, $attr, $comment->Comment);
29
        $newRow = $method->invokeArgs($gridfield, $params);
30
        $this->assertEquals('<tr class="cssClass">This is a comment</tr>', $newRow);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
32
        $comment->markSpam();
33
        $params = array(1, 1, $comment, $attr, $comment->Comment);
34
        $newRow = $method->invokeArgs($gridfield, $params);
35
        $this->assertEquals('<tr class="cssClass spam">This is a comment</tr>', $newRow);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37
38
	}
39
40
}
41