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
Push — poc-backpack-components ( 0ddd2c...dbc69d )
by Pedro
26:57
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\AttributeCollection;
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Librar...nts\AttributeCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Backpack\CRUD\app\Library\Components\Interfaces\SmartAttributeInterface;
7
use Backpack\CRUD\app\Library\Components\Interfaces\SmartCollectionInterface;
8
use Illuminate\Support\Str;
9
10
class Entity extends BaseAttribute implements SmartAttributeInterface
11
{
12
    public static function getDefault(SmartCollectionInterface $attributes)
13
    {
14
        $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...
15
16
        $fieldName = $attributes->getAttributeValue('name');
17
18
        //if the name is dot notation we are sure it's a relationship
19
        if (strpos($fieldName, '.') !== false) {
20
            $possibleMethodName = Str::of($fieldName)->before('.');
21
22
            return self::modelMethodIsRelationship($model, $possibleMethodName) ? $fieldName : false;
23
        }
24
25
        // if there's a method on the model with this name
26
        if (method_exists($model, $fieldName)) {
27
            // check model method for possibility of being a relationship
28
            return self::modelMethodIsRelationship($model, $fieldName);
29
        }
30
31
        // if the name ends with _id and that method exists,
32
        // we can probably use it as an entity
33
        if (Str::endsWith($fieldName, '_id')) {
34
            $possibleMethodName = Str::replaceLast('_id', '', $fieldName);
35
36
            if (method_exists($model, $possibleMethodName)) {
37
                // check model method for possibility of being a relationship
38
                return self::modelMethodIsRelationship($model, $possibleMethodName);
39
            }
40
        }
41
42
        return false;
43
    }
44
45
    public static function getValidation()
46
    {
47
        return ['required'];
48
    }
49
50
    public static function getAttributeName(): string
51
    {
52
        return 'entity';
53
    }
54
55
    /**
56
     * Checks the properties of the provided method to better verify if it could be a relation.
57
     * Case the method is not public, is not a relation.
58
     * Case the return type is Attribute, or extends Attribute is not a relation method.
59
     * If the return type extends the Relation class is for sure a relation
60
     * Otherwise we just assume it's a relation.
61
     *
62
     * DEV NOTE: In future versions we will return `false` when no return type is set and make the return type mandatory for relationships.
63
     *           This function should be refactored to only check if $returnType is a subclass of Illuminate\Database\Eloquent\Relations\Relation.
64
     *
65
     * @param $model
66
     * @param $method
67
     * @return bool|string
68
     */
69
    private static function modelMethodIsRelationship($model, $method)
70
    {
71
        $methodReflection = new \ReflectionMethod($model, $method);
72
73
        // relationship methods function does not have parameters
74
        if ($methodReflection->getNumberOfParameters() > 0) {
75
            return false;
76
        }
77
78
        // relationships are always public methods.
79
        if (! $methodReflection->isPublic()) {
80
            return false;
81
        }
82
83
        $returnType = $methodReflection->getReturnType();
84
85
        if ($returnType) {
86
            $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

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