Executable::getCallableReflection()   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 0
1
<?php
2
3
namespace Atreyu;
4
5
class Executable
6
{
7
    private $callableReflection;
8
    private $invocationObject;
9
    private $isInstanceMethod;
10
11
    public function __construct(\ReflectionFunctionAbstract $reflFunc, $invocationObject = null)
12
    {
13
        if ($reflFunc instanceof \ReflectionMethod) {
14
            $this->isInstanceMethod = true;
15
            $this->setMethodCallable($reflFunc, $invocationObject);
16
        } else {
17
            $this->isInstanceMethod = false;
18
            $this->callableReflection = $reflFunc;
19
        }
20
    }
21
22
    private function setMethodCallable(\ReflectionMethod $reflection, $invocationObject)
23
    {
24
        if (is_object($invocationObject)) {
25
            $this->callableReflection = $reflection;
26
            $this->invocationObject = $invocationObject;
27
        } elseif ($reflection->isStatic()) {
28
            $this->callableReflection = $reflection;
29
        } else {
30
            throw new \InvalidArgumentException(
31
                'ReflectionMethod callables must specify an invocation object'
32
            );
33
        }
34
    }
35
36
    public function __invoke()
37
    {
38
        $args = func_get_args();
39
        $reflection = $this->callableReflection;
40
41
        if ($this->isInstanceMethod) {
42
            return $reflection->invokeArgs($this->invocationObject, $args);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionFunctionAbstract as the method invokeArgs() does only exist in the following sub-classes of ReflectionFunctionAbstract: ReflectionFunction, ReflectionMethod. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
43
        }
44
45
        return $this->callableReflection->isClosure()
46
            ? $this->invokeClosureCompat($reflection, $args)
47
            : $reflection->invokeArgs($args);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionFunctionAbstract as the method invokeArgs() does only exist in the following sub-classes of ReflectionFunctionAbstract: ReflectionFunction, ReflectionMethod. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
48
    }
49
50
    /**
51
     * @TODO Remove this extra indirection when 5.3 support is dropped
52
     * @param $reflection
53
     * @param $args
54
     *
55
     * @return mixed
56
     */
57
    private function invokeClosureCompat($reflection, $args)
58
    {
59
        if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
60
            $scope = $reflection->getClosureScopeClass();
61
            $closure = \Closure::bind(
62
                $reflection->getClosure(),
63
                $reflection->getClosureThis(),
64
                $scope ? $scope->name : null
65
            );
66
            return call_user_func_array($closure, $args);
67
        } else {
68
            return $reflection->invokeArgs($args);
69
        }
70
    }
71
72
    public function getCallableReflection()
73
    {
74
        return $this->callableReflection;
75
    }
76
77
    public function getInvocationObject()
78
    {
79
        return $this->invocationObject;
80
    }
81
82
    public function isInstanceMethod()
83
    {
84
        return $this->isInstanceMethod;
85
    }
86
}
87