Passed
Push — master ( 188cdd...047a07 )
by Dmitry
02:45
created

MetadataExtractor::createReflectionFunction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 7
loc 7
rs 9.4285
cc 3
eloc 4
nc 4
nop 1
1
<?php
2
3
namespace Tonic\Component\ApiLayer\JsonRpcExtensions\Documentation;
4
5
use phpDocumentor\Reflection\DocBlock;
6
use Tonic\Component\ApiLayer\JsonRpc\Method\MethodCollection;
7
use Tonic\Component\Reflection\ReflectionFunctionFactory;
8
use Tonic\Component\Reflection\TypeResolver;
9
10
/**
11
 * Extracts metadata from method collection.
12
 */
13
class MetadataExtractor
14
{
15
    /**
16
     * @var TypeResolver
17
     */
18
    private $typeResolver;
19
20
    /**
21
     * @param TypeResolver $typeResolver
22
     */
23
    public function __construct(TypeResolver $typeResolver)
24
    {
25
        $this->typeResolver = $typeResolver;
26
    }
27
28
    /**
29
     * @param MethodCollection $methodCollection
30
     *
31
     * @return array
32
     */
33
    public function extract(MethodCollection $methodCollection)
34
    {
35
        $metadata = [];
36
        /** @var callable $callable */
37
        foreach ($methodCollection as $methodName => $callable) {
38
            $reflectionFunction = ReflectionFunctionFactory::createFromCallable($callable);
0 ignored issues
show
Documentation introduced by
$callable is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
40
            $requestObjectClass = $this->determineRequestObjectClass($reflectionFunction);
41
            $responseObjectClass = $this->determineResponseObjectClass($reflectionFunction);
42
43
            $docBlock = new DocBlock($reflectionFunction);
44
45
            $metadata[] = [
46
                'method' => $methodName,
47
                'description' => $docBlock->getShortDescription(),
48
                'parameters' => $this->extractParameters($requestObjectClass),
49
                'returns' => $this->extractParameters($responseObjectClass),
50
            ];
51
        }
52
53
        return $metadata;
54
    }
55
56
    /**
57
     * @param string $class
58
     * @param array  $processed
59
     *
60
     * @return array
61
     */
62
    private function extractParameters($class, $processed = [])
63
    {
64
        if (isset($processed[$class])) {
65
            return [];
66
        }
67
        $processed[$class] = true;
68
69
        $reflectionClass = new \ReflectionClass($class);
70
        $parameters = [];
71
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
72
            $type = $this->determinePropertyType($reflectionProperty);
73
74
            $isCollection = false;
75 View Code Duplication
            if (($pos = strpos($type, '[')) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                // if type is collection of types
77
                $isCollection = true;
78
                $type = substr($type, 0, $pos);
79
            }
80
81
            $docBlock = new DocBlock($reflectionProperty);
82
83
            $parameter = [
84
                'name' => $reflectionProperty->getName(),
85
                'description' => $docBlock->getShortDescription(),
86
                'type' => $type.($isCollection ? '[]' : ''),
87
            ];
88
            if (class_exists($type)) {
89
                $parameter = array_merge($parameter, [
90
                    'type' => 'object'.($isCollection ? '[]' : ''),
91
                    'properties' => $this->extractParameters($type, $processed),
92
                ]);
93
            }
94
95
            $parameters[] = $parameter;
96
        }
97
98
        return $parameters;
99
    }
100
101
    /**
102
     * @param \ReflectionFunctionAbstract $reflectionFunction
103
     *
104
     * @return string
105
     */
106
    private function determineRequestObjectClass(\ReflectionFunctionAbstract $reflectionFunction)
107
    {
108
        $reflectionParameters = $reflectionFunction->getParameters();
109
        /** @var \ReflectionParameter $reflectionParameter */
110
        $reflectionParameter = reset($reflectionParameters);
111
112
        return $reflectionParameter->getClass()->getName();
0 ignored issues
show
Bug introduced by
Consider using $reflectionParameter->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
113
    }
114
115
    /**
116
     * @param \ReflectionFunctionAbstract $reflectionFunction
117
     *
118
     * @return string
119
     */
120
    private function determineResponseObjectClass(\ReflectionFunctionAbstract $reflectionFunction)
121
    {
122
        return $this->typeResolver->resolveFunctionReturnType($reflectionFunction);
0 ignored issues
show
Compatibility introduced by
$reflectionFunction of type object<ReflectionFunctionAbstract> is not a sub-type of object<ReflectionMethod>. It seems like you assume a child class of the class ReflectionFunctionAbstract to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
123
    }
124
125
    /**
126
     * @param \ReflectionProperty $reflectionProperty
127
     *
128
     * @return null|string
129
     */
130
    private function determinePropertyType(\ReflectionProperty $reflectionProperty)
131
    {
132
        return $this->typeResolver->resolvePropertyType($reflectionProperty);
133
    }
134
}
135