1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Visitor; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract Linked Visitor class. |
15
|
|
|
* |
16
|
|
|
* @author Karel Osorio RamÃrez <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class LinkedVisitor extends DynamicDispatchVisitor |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var LinkedVisitor |
22
|
|
|
*/ |
23
|
|
|
private $next = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var LinkedVisitor |
27
|
|
|
*/ |
28
|
|
|
private $parent = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param LinkedVisitor $next |
32
|
|
|
* @param ResolverVisitorMethodInterface $resolver |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
LinkedVisitor $next = null, |
36
|
|
|
ResolverVisitorMethodInterface $resolver = null |
37
|
|
|
) { |
38
|
|
|
$this->next = $next === null ? new NullVisitor() : $next; |
39
|
|
|
$this->next->parent = $this; |
40
|
|
|
$this->parent = null; |
41
|
|
|
$this->resolver = $resolver === null ? new ResolverVisitorMethod() : $resolver; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function canHandlerVisitee(VisiteeInterface $visitee) |
48
|
|
|
{ |
49
|
|
|
return parent::canHandlerVisitee($visitee) || $this->next->canHandlerVisitee($visitee); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function visit(VisiteeInterface $visitee) |
56
|
|
|
{ |
57
|
|
|
return $this->doVisit($visitee, \func_get_args()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param VisiteeInterface $visitee |
62
|
|
|
* @param array $args |
63
|
|
|
* @param bool $fromParent |
64
|
|
|
*/ |
65
|
|
|
protected function doVisit(VisiteeInterface $visitee, array $args, $fromParent = false) |
66
|
|
|
{ |
67
|
|
|
if (!$fromParent && $this->parent !== null) { |
68
|
|
|
return \call_user_func_array(array($this->parent, 'visit'), $args); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (parent::canHandlerVisitee($visitee)) { |
|
|
|
|
72
|
|
|
return $this->visitWith($visitee, $args); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->next->doVisit($visitee, $args, true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected function notSupportedVisiteeException(VisiteeInterface $visitee) |
82
|
|
|
{ |
83
|
|
|
if ($this->parent !== null) { |
84
|
|
|
return $this->parent->notSupportedVisiteeException($visitee); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return parent::notSupportedVisiteeException($visitee); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.