|
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( |
|
|
|
|
|
|
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
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.