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
36:02 queued 23:20
created

EntityAttribute   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidation() 0 3 1
A getAttributeName() 0 3 1
A modelMethodIsRelationship() 0 29 6
B getDefault() 0 31 7
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Components\Attributes;
4
5
use Backpack\CRUD\app\Library\Components\AttributeCollection;
6
use Backpack\CRUD\app\Library\Components\Interfaces\AttributeInterface;
7
use Illuminate\Support\Str;
8
9
class EntityAttribute extends BaseAttribute implements AttributeInterface
10
{
11
    public static function getDefault(AttributeCollection $attributes)
12
    {
13
        $model = $attributes->hasAttribute('baseModel') ? (new $attributes->getAttributeValue('baseModel')) : static::provider()->model;
0 ignored issues
show
Bug introduced by
The property getAttributeValue does not seem to exist on Backpack\CRUD\app\Librar...nts\AttributeCollection.
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 getValidation()
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