Completed
Pull Request — master (#11)
by Andrea Marco
02:56
created

IncludesListing::listForEloquent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Rexlabs\Laravel\Smokescreen\Console;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
8
/**
9
 * The includes listing.
10
 *
11
 */
12
class IncludesListing
13
{
14
    /**
15
     * The map of available relations.
16
     *
17
     * @var array
18
     */
19
    protected $relationsMap = [
20
        'hasOne' => 'item',
21
        'morphOne' => 'item',
22
        'belongsTo' => 'item',
23
        'morphTo' => 'item',
24
        'hasMany' => 'collection',
25
        'hasManyThrough' => 'collection',
26
        'morphMany' => 'collection',
27
        'belongsToMany' => 'collection',
28
        'morphToMany' => 'collection',
29
        'morphedByMany' => 'collection',
30
    ];
31
32
    /**
33
     * List the includes of the given Eloquent model.
34
     *
35
     * @param string $class
36
     * @return array
37
     */
38
    public function listForEloquent(string $class) : array
39
    {
40
        $list = [];
41
        $methods = (new ReflectionClass($class))->getMethods(ReflectionMethod::IS_PUBLIC);
42
        $declaredMethods = array_filter($methods, function ($method) use ($class) {
43
            return $method->getDeclaringClass()->getName() == $class;
44
        });
45
46
        foreach ($declaredMethods as $method) {
47
            if ($type = $this->getResourceTypeByMethodReturnType($method)) {
48
                $list[$method->getName()] = "relation|{$type}";
49
            } elseif ($type = $this->getResourceTypeByMethodDefinition($method)) {
50
                $list[$method->getName()] = "relation|{$type}";
51
            }
52
        }
53
54
        return $list;
55
56
        // implement getResourceTypeByMethodDefinition
57
        // investigate custom relation names with actual relation name
58
        // ^ if different may not relate on $method->getName()
59
    }
60
61
    /**
62
     * Retrieve the type of the resource based on the given method return type.
63
     *
64
     * @param ReflectionMethod $method
65
     * @return ?string
66
     */
67
    protected function getResourceTypeByMethodReturnType(ReflectionMethod $method) : ?string
68
    {
69
        $returnType = (string) $method->getReturnType();
70
        $namespace = 'Illuminate\Database\Eloquent\Relations';
71
72
        if (!starts_with($returnType, $namespace)) {
73
            return null;
74
        }
75
76
        $relation = lcfirst(class_basename($returnType));
77
78
        return $this->relationsMap[$relation] ?? null;
79
    }
80
81
    /**
82
     * Retrieve the type of the resource based on the given method definition.
83
     *
84
     * @param ReflectionMethod $method
85
     * @return ?string
86
     */
87
    protected function getResourceTypeByMethodDefinition(ReflectionMethod $method) : ?string
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    protected function getResourceTypeByMethodDefinition(/** @scrutinizer ignore-unused */ ReflectionMethod $method) : ?string

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

Loading history...
88
    {
89
        return null;
90
    }
91
}
92