Completed
Push — master ( 82d1fc...6f9908 )
by Ivannis Suárez
02:50 queued 29s
created

LinkedVisitorTests::visitNextDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Visitor\Tests\Units;
11
12
use Cubiche\Core\Visitor\Tests\Fixtures\Calculator;
13
use Cubiche\Core\Visitor\Tests\Fixtures\Evaluator;
14
use Cubiche\Core\Visitor\Tests\Fixtures\Sum;
15
use Cubiche\Core\Visitor\Tests\Fixtures\Value;
16
use Cubiche\Core\Visitor\Tests\Fixtures\Variable;
17
18
/**
19
 * Linked Visitor Tests Class.
20
 *
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
class LinkedVisitorTests extends LinkedVisitorTestCase
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function visitNextDataProvider()
29
    {
30
        return array(
31
            array(
32
                new Evaluator($calculator = $this->newMockInstance(Calculator::class)),
0 ignored issues
show
Documentation introduced by
$calculator = $this->new...ures\Calculator::class) is of type object|null, but the function expects a object<Cubiche\Core\Visi...ts\Fixtures\Calculator>.

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...
33
                $calculator,
34
                new Sum(new Value(1), new Value(2)),
35
                'visitSum',
36
                3,
37
            ),
38
            array(
39
                new Evaluator($calculator = $this->newMockInstance(Calculator::class), array('x' => 5)),
0 ignored issues
show
Documentation introduced by
$calculator = $this->new...ures\Calculator::class) is of type object|null, but the function expects a object<Cubiche\Core\Visi...ts\Fixtures\Calculator>.

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...
40
                $calculator,
41
                new Sum(new Value(1), new Variable('x')),
42
                'visitSum',
43
                6,
44
            ),
45
        );
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function visitDataProvider()
52
    {
53
        return array(
54
            array(
55
                $this->newMockInstance(Evaluator::class, null, null, array(new Calculator(), array('x' => 5))),
56
                new Variable('x'),
57
                'visitVariable',
58
                5,
59
            ),
60
        );
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function canHandlerVisiteeDataProvider()
67
    {
68
        $data = parent::canHandlerVisiteeDataProvider();
69
70
        return \array_merge($data, array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \array_merge($dat...res\Value(2)), true))); (array[]) is incompatible with the return type of the parent method Cubiche\Core\Visitor\Tes...dlerVisiteeDataProvider of type array<object|null|false>[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
71
            array(new Calculator(), new Sum(new Value(1), new Value(2)), true),
72
            array(new Calculator(), new Variable('x'), false),
73
            array(new Evaluator(new Calculator()), new Sum(new Value(1), new Value(2)), true),
74
            array(new Evaluator(new Calculator(), array('x' => 5)), new Sum(new Variable('x'), new Value(2)), true),
75
        ));
76
    }
77
}
78