AbstractSyncedRoutine   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 61
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 9 3
A execute() 0 12 2
A getReflection() 0 13 4
1
<?php
2
/*
3
 * This file is part of the Respect\Rest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Respect\Rest\Routines;
10
11
use ReflectionFunction;
12
use ReflectionMethod;
13
use ReflectionObject;
14
use ReflectionClass;
15
use Closure;
16
use Respect\Rest\Request;
17
18
/** Base class for routines that sync parameters */
19
abstract class AbstractSyncedRoutine extends AbstractRoutine implements ParamSynced
20
{
21
    /**
22
     * @var Reflector
23
     */
24
    protected $reflection;
25
26
    /**
27
     * Return parameters that can be used with the routine.
28
     *
29
     * @return array
30
     */
31 20
    public function getParameters()
32
    {
33 20
        $reflection = $this->getReflection();
34 20
        if (!$reflection instanceof ReflectionObject && !$reflection instanceof ReflectionClass) {
35 17
            return $this->getReflection()->getParameters();
0 ignored issues
show
Bug introduced by
The method getParameters does only exist in ReflectionFunction and ReflectionMethod, but not in ReflectionClass.

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...
36
        }
37
38 3
        return array();
39
    }
40
41
    /**
42
     * Executes the routine and return its result.
43
     *
44
     * @param  Respect\Rest\Request $request
45
     * @param  array                $params
46
     * @return mixed
47
     */
48 16
    public function execute(Request $request, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50 16
        $callback = $this->getCallback();
51 16
        if (is_string($callback)) {
52 1
            $reflection      = $this->getReflection();
53 1
            $routineInstance = $reflection->newInstanceArgs($params);
0 ignored issues
show
Bug introduced by
The method newInstanceArgs does only exist in ReflectionClass, but not in ReflectionFunction and ReflectionMethod.

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...
54
55 1
            return $routineInstance();
56
        }
57
58 15
        return call_user_func_array($callback, $params);
59
    }
60
61
    /**
62
     * Returns a concrete ReflectionFunctionAbstract for this routine callback.
63
     *
64
     * @return Reflector
65
     */
66 20
    protected function getReflection()
67
    {
68 20
        $callback = $this->getCallback();
69 20
        if (is_array($callback)) {
70 1
            return new ReflectionMethod($callback[0], $callback[1]);
71 19
        } elseif ($callback instanceof Closure) {
72 16
            return new ReflectionFunction($callback);
73 3
        } elseif (is_string($callback)) {
74 1
            return new ReflectionClass($callback);
75
        } else {
76 2
            return new ReflectionObject($callback);
77
        }
78
    }
79
}
80