Completed
Pull Request — master (#751)
by Greg
06:34 queued 03:24
created

WatchTest::testMonitorWithOneEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use AspectMock\Test as test;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, test.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
5
class WatchTest extends \Codeception\TestCase\Test
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...
6
{
7
    /**
8
     * @var AspectMock\Proxy\AnythingClassProxy
9
     */
10
    protected $resourceWatcher;
11
12
    public function _before()
13
    {
14
        if (!class_exists('Lurker\\ResourceWatcher')) {
15
            $this->resourceWatcher = test::spec(
0 ignored issues
show
Bug introduced by
The method make does only exist in AspectMock\Proxy\ClassProxy, but not in AspectMock\Proxy\InstanceProxy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
16
                'Lurker\ResourceWatcher',
17
                [
18
                    'start' => true,
19
                    'track' => true,
20
                    'addListener' => true
21
                ]
22
            )->make();
23
        } else {
24
            $this->resourceWatcher = test::double(
0 ignored issues
show
Documentation Bug introduced by
It seems like \AspectMock\Test::double...'addListener' => true)) can also be of type object<AspectMock\Proxy\ClassProxy> or object<AspectMock\Proxy\InstanceProxy>. However, the property $resourceWatcher is declared as type object<AspectMock\Proxy\AnythingClassProxy>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
25
                'Lurker\ResourceWatcher',
26
                [
27
                    'start' => true,
28
                    'track' => true,
29
                    'addListener' => true
30
                ]
31
            );
32
        }
33
    }
34
35
    public function testMonitorWithOneEvent()
36
    {
37
        $task = new Robo\Task\Base\Watch($this);
38
39
        $task->monitor(
40
            'src',
41
            function () {
42
                //do nothing
43
            },
44
            1 // CREATE
45
        )->run();
46
47
        $this->resourceWatcher->verifyInvokedOnce('track');
48
    }
49
50
    public function testMonitorWithTwoEvents()
51
    {
52
        $task = new Robo\Task\Base\Watch($this);
53
54
        $task->monitor(
55
            'src',
56
            function () {
57
                //do nothing
58
            },
59
            [
0 ignored issues
show
Documentation introduced by
array(1, 4) is of type array<integer,integer,{"...nteger","1":"integer"}>, but the function expects a integer.

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...
60
                1, //CREATE
61
                4, //DELETE
62
            ]
63
        )->run();
64
65
        $this->resourceWatcher->verifyInvokedMultipleTimes('track', 2);
66
    }
67
}