DynamicDispatchVisitorTests::visitDataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 17
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\ExpressionToStringConverter;
14
use Cubiche\Core\Visitor\Tests\Fixtures\Mult;
15
use Cubiche\Core\Visitor\Tests\Fixtures\SmartExpressionToStringConverter;
16
use Cubiche\Core\Visitor\Tests\Fixtures\Sum;
17
use Cubiche\Core\Visitor\Tests\Fixtures\Value;
18
use Cubiche\Core\Visitor\Tests\Fixtures\Variable;
19
20
/**
21
 * Dynamic Dispatch Visitor Tests Class.
22
 *
23
 * @author Karel Osorio Ramírez <[email protected]>
24
 */
25
class DynamicDispatchVisitorTests extends DynamicDispatchVisitorTestCase
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function visitDataProvider()
31
    {
32
        return array(
33
            array(
34
                $this->newMockInstance(Calculator::class),
35
                new Mult(new Value(3), new Value(2)),
36
                'visitMult',
37
                6,
38
            ),
39
            array(
40
                $this->newMockInstance(ExpressionToStringConverter::class),
41
                new Sum(new Value(1), new Value(2)),
42
                'visitOperator',
43
                '(1+2)',
44
            ),
45
            array(
46
                $this->newMockInstance(SmartExpressionToStringConverter::class),
47
                new Sum(new Value(1), new Value(2)),
48
                'visitOperator',
49
                '1+2',
50
            ),
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function canHandlerVisiteeDataProvider()
58
    {
59
        $data = parent::canHandlerVisiteeDataProvider();
60
61
        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...
62
            array(new Calculator(), new Sum(new Value(1), new Value(2)), true),
63
            array(new Calculator(), new Variable('x'), false),
64
            array(new ExpressionToStringConverter(), new Sum(new Value(1), new Value(2)), true),
65
        ));
66
    }
67
}
68