Method::with()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\Selector;
12
13
use Cubiche\Core\Delegate\Delegate;
14
15
/**
16
 * Method Selector Class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 */
20
class Method extends Field
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $args;
26
27
    /**
28
     * @param string $name
29
     * @param array  $args
30
     */
31
    public function __construct($name, array $args = array())
32
    {
33
        parent::__construct($name);
34
35
        $this->args = $args;
36
    }
37
38
    /**
39
     * @return $this
40
     */
41
    public function with()
42
    {
43
        $this->args = \func_get_args();
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function args()
52
    {
53
        return $this->args;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function apply($value)
60
    {
61
        if (!is_object($value)) {
62
            throw new \RuntimeException('Trying to call method of non-object');
63
        }
64
65
        if (!method_exists($value, $this->name)) {
66
            throw new \RuntimeException(\sprintf('Undefined method %s::%s', \get_class($value), $this->name));
67
        }
68
69
        $method = Delegate::fromMethod($value, $this->name);
70
        if ($method->reflection()->isPrivate() || $method->reflection()->isProtected()) {
0 ignored issues
show
Bug introduced by
The method isPrivate does only exist in ReflectionMethod, but not in ReflectionFunction.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Bug introduced by
The method isProtected does only exist in ReflectionMethod, but not in ReflectionFunction.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71
            throw new \RuntimeException(
72
                \sprintf('Trying to call non-public method %s::%s', \get_class($value), $this->name)
73
            );
74
        }
75
76
        return $method->invokeWith($this->args());
77
    }
78
}
79