LinkedVisitor::visit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (canHandlerVisitee() instead of doVisit()). Are you sure this is correct? If so, you might want to change this to $this->canHandlerVisitee().

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:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
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