Completed
Push — 2.x ( ee1bb7...bfb9a9 )
by Akihito
05:07
created

ReflectionMethod::setObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the Ray.Aop package
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace Ray\Aop;
8
9
final class ReflectionMethod extends \ReflectionMethod implements Reader
10
{
11
    /**
12
     * @var WeavedInterface
13
     */
14
    private $object;
15
16
    /**
17
     * @var string
18
     */
19
    private $method;
20
21
    /**
22
     * Set dependencies
23
     *
24
     * @param WeavedInterface   $object
25
     * @param \ReflectionMethod $method
26
     */
27 3
    public function setObject(WeavedInterface $object, \ReflectionMethod $method)
28
    {
29 3
        $this->object = $object;
30 3
        $this->method = $method->getName();
31 3
    }
32
33
    /**
34
     * @return ReflectionClass
35
     */
36 2
    public function getDeclaringClass()
37
    {
38 2
        $originalClass = (new \ReflectionClass($this->object))->getParentClass()->getName();
39 2
        $class =  new ReflectionClass($originalClass);
40 2
        $class->setObject($this->object);
41
42 2
        return $class;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function getAnnotations()
49
    {
50 2
        $annotations = unserialize($this->object->methodAnnotations);
1 ignored issue
show
Bug introduced by
Accessing methodAnnotations on the interface Ray\Aop\WeavedInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51 2
        if (isset($annotations[$this->method])) {
52 1
            return $annotations[$this->method];
53
        }
54
55 1
        return [];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function getAnnotation($annotationName)
62
    {
63 2
        $annotations = $this->getAnnotations();
64 2
        if (isset($annotations[$annotationName])) {
65 1
            return $annotations[$annotationName];
66
        }
67
68 1
        return null;
69
    }
70
}
71