Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — main (#4854)
by Pedro
30:01 queued 14:59
created

Entity::modelMethodIsRelationship()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 29
rs 9.2222
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Components\Attributes;
4
5
use Backpack\CRUD\app\Library\Components\Interfaces\SmartAttributeInterface;
6
use Backpack\CRUD\app\Library\Components\Interfaces\SmartCollectionInterface;
7
use Illuminate\Support\Str;
8
9
class Entity extends BaseAttribute implements SmartAttributeInterface
10
{
11
    public static function getDefault(SmartCollectionInterface $attributes)
12
    {
13
        $model = $attributes->hasAttribute('baseModel') ? (new $attributes->getAttributeValue('baseModel')) : app('crud')->model;
0 ignored issues
show
Bug introduced by
Accessing getAttributeValue on the interface Backpack\CRUD\app\Librar...martCollectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
14
15
        $fieldName = $attributes->getAttributeValue('name');
16
17
        //if the name is dot notation we are sure it's a relationship
18
        if (strpos($fieldName, '.') !== false) {
19
            $possibleMethodName = Str::of($fieldName)->before('.');
20
21
            return self::modelMethodIsRelationship($model, $possibleMethodName) ? $fieldName : false;
22
        }
23
24
        // if there's a method on the model with this name
25
        if (method_exists($model, $fieldName)) {
26
            // check model method for possibility of being a relationship
27
            return self::modelMethodIsRelationship($model, $fieldName);
28
        }
29
30
        // if the name ends with _id and that method exists,
31
        // we can probably use it as an entity
32
        if (Str::endsWith($fieldName, '_id')) {
33
            $possibleMethodName = Str::replaceLast('_id', '', $fieldName);
34
35
            if (method_exists($model, $possibleMethodName)) {
36
                // check model method for possibility of being a relationship
37
                return self::modelMethodIsRelationship($model, $possibleMethodName);
38
            }
39
        }
40
41
        return false;
42
    }
43
44
    public static function getValidationRules(SmartCollectionInterface $attributes): array
45
    {
46
        return ['required'];
47
    }
48
49
    public static function getAttributeName(): string
50
    {
51
        return 'entity';
52
    }
53
54
    /**
55
     * Checks the properties of the provided method to better verify if it could be a relation.
56
     * Case the method is not public, is not a relation.
57
     * Case the return type is Attribute, or extends Attribute is not a relation method.
58
     * If the return type extends the Relation class is for sure a relation
59
     * Otherwise we just assume it's a relation.
60
     *
61
     * DEV NOTE: In future versions we will return `false` when no return type is set and make the return type mandatory for relationships.
62
     *           This function should be refactored to only check if $returnType is a subclass of Illuminate\Database\Eloquent\Relations\Relation.
63
     *
64
     * @param $model
65
     * @param $method
66
     * @return bool|string
67
     */
68
    private static function modelMethodIsRelationship($model, $method)
69
    {
70
        $methodReflection = new \ReflectionMethod($model, $method);
71
72
        // relationship methods function does not have parameters
73
        if ($methodReflection->getNumberOfParameters() > 0) {
74
            return false;
75
        }
76
77
        // relationships are always public methods.
78
        if (! $methodReflection->isPublic()) {
79
            return false;
80
        }
81
82
        $returnType = $methodReflection->getReturnType();
83
84
        if ($returnType) {
85
            $returnType = $returnType->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

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

85
            /** @scrutinizer ignore-call */ 
86
            $returnType = $returnType->getName();
Loading history...
86
87
            if (is_a($returnType, 'Illuminate\Database\Eloquent\Casts\Attribute', true)) {
88
                return false;
89
            }
90
91
            if (is_a($returnType, 'Illuminate\Database\Eloquent\Relations\Relation', true)) {
92
                return $method;
93
            }
94
        }
95
96
        return $method;
97
    }
98
}
99