Completed
Push — master ( 145864...6ab5d4 )
by Dmitry
03:34
created

TestingTest.php$0 ➔ testMock()   A

Complexity

Conditions 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
1
<?php
2
3
namespace Test;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
use Basis\Test;
8
9
class TestingTest extends TestSuite
10
{
11
    public function test()
12
    {
13
        $userTest = new class($this->app) extends Test {
0 ignored issues
show
Unused Code introduced by
The call to anonymous//test/php/Test...st.php$0::__construct() has too many arguments starting with $this->app.

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...
14
            public function testMock($frameworkTest)
15
            {
16
                $config = [
17
                    [['name' => 'nekufa'], ['value' => 'nekufa!']],
18
                    [['name' => 'bazyaba'], function () {
19
                        return ['value' => 'bazyaba!'];
20
                    }],
21
                    [[], ['value' => 'anonymous!']],
22
                ];
23
                foreach ($config as [$params, $value]) {
24
                    $this->mock('service.call', $params)->willReturn($value);
0 ignored issues
show
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
25
                }
26
27
                foreach ($config as [$params, $value]) {
28
                    $result = $this->dispatch('service.call', $params);
29
                    if (is_callable($value)) {
30
                        $value = $value();
0 ignored issues
show
Bug introduced by
The variable $value 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...
31
                    }
32
                    $frameworkTest->assertSame(get_object_vars($result), $value);
33
                }
34
            }
35
        };
36
37
        $userTest->setup();
38
        $userTest->testMock($this);
39
    }
40
}
41