Completed
Pull Request — master (#320)
by Alexander
03:17
created

DeclareParentsAdvisor::getAdvice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Aop\Support;
13
14
use Go\Aop\Advice;
15
use Go\Aop\IntroductionAdvisor;
16
use Go\Aop\IntroductionInfo;
17
use Go\Aop\Pointcut\PointcutClassFilterTrait;
18
use Go\Aop\PointFilter;
19
use ReflectionClass;
20
21
/**
22
 * Introduction advisor delegating to the given object.
23
 */
24
class DeclareParentsAdvisor extends AbstractGenericAdvisor implements IntroductionAdvisor
25
{
26
    use PointcutClassFilterTrait;
27
28
    /**
29
     * Introduction information
30
     *
31
     * @var IntroductionInfo
32
     */
33
    protected $advice;
34
35
    /**
36
     * Creates an advisor for declaring mixins via traits and interfaces.
37
     *
38
     * @param PointFilter $classFilter Class filter
39
     * @param IntroductionInfo $info Introduction information
40
     */
41
    public function __construct(PointFilter $classFilter, IntroductionInfo $info)
42
    {
43
        $this->classFilter = $classFilter;
44
        parent::__construct($info);
45
    }
46
47
    /**
48
     * Can the advised interfaces be implemented by the introduction advice?
49
     *
50
     * Invoked before adding an IntroductionAdvisor.
51
     *
52
     * @return void
53
     * @throws \InvalidArgumentException if the advised interfaces can't be implemented by the introduction advice
54
     */
55
    public function validateInterfaces()
56
    {
57
        $refInterface      = new ReflectionClass(reset($this->advice->getInterfaces()));
0 ignored issues
show
Bug introduced by
$this->advice->getInterfaces() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
58
        $refImplementation = new ReflectionClass(reset($this->advice->getTraits()));
0 ignored issues
show
Bug introduced by
$this->advice->getTraits() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
59
        if (!$refInterface->isInterface()) {
60
            throw new \InvalidArgumentException("Only interface can be introduced");
61
        }
62
        if (!$refImplementation->isTrait()) {
63
            throw new \InvalidArgumentException("Only trait can be used as implementation");
64
        }
65
66
        foreach ($refInterface->getMethods() as $interfaceMethod) {
67
            if (!$refImplementation->hasMethod($interfaceMethod->name)) {
68
                throw new \DomainException("Implementation requires method {$interfaceMethod->name}");
69
            }
70
        }
71
    }
72
}
73