InheritsRelationsFromParentModel::getForeignKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base\app\Models\Traits;
4
5
use Illuminate\Support\Str;
6
use ReflectionClass;
7
8
/**
9
 * This trait helps a child model (ex: BackpackUser) inherit all relationships of its parent model (ex: User).
10
 * Laravel by default doesn't do that, so packages like Backpack\PermissionManager can't see relationships
11
 * on the BackpackUser model, because they haven't been inherited from User.
12
 *
13
 * The code below has been copy-pasted from https://github.com/tightenco/parental on Jul 19th 2019.
14
 */
15
trait InheritsRelationsFromParentModel
16
{
17
    public $hasParent = true;
18
19
    public static function bootHasParent()
20
    {
21
        static::creating(function ($model) {
22
            if ($model->parentHasHasChildrenTrait()) {
23
                $model->forceFill(
24
                    [$model->getInheritanceColumn() => $model->classToAlias(get_class($model))]
25
                );
26
            }
27
        });
28
        static::addGlobalScope(function ($query) {
29
            $instance = new static();
30
            if ($instance->parentHasHasChildrenTrait()) {
31
                $query->where($instance->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance)));
0 ignored issues
show
Bug introduced by
It seems like getInheritanceColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like classToAlias() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
            }
33
        });
34
    }
35
36
    public function parentHasHasChildrenTrait()
37
    {
38
        return $this->hasChildren ?? false;
0 ignored issues
show
Bug introduced by
The property hasChildren does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
    }
40
41
    public function getTable()
42
    {
43
        if (!isset($this->table)) {
44
            return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass()))));
45
        }
46
47
        return $this->table;
0 ignored issues
show
Bug introduced by
The property table does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
    }
49
50
    public function getForeignKey()
51
    {
52
        return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey;
0 ignored issues
show
Bug introduced by
The property primaryKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
    }
54
55
    public function joiningTable($related, $instance = null)
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed.

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

Loading history...
56
    {
57
        $relatedClassName = method_exists((new $related()), 'getClassNameForRelationships')
58
            ? (new $related())->getClassNameForRelationships()
59
            : class_basename($related);
60
        $models = [
61
            Str::snake($relatedClassName),
62
            Str::snake($this->getClassNameForRelationships()),
63
        ];
64
        sort($models);
65
66
        return strtolower(implode('_', $models));
67
    }
68
69
    public function getClassNameForRelationships()
70
    {
71
        return class_basename($this->getParentClass());
72
    }
73
74
    public function getMorphClass()
75
    {
76
        if ($this->parentHasHasChildrenTrait()) {
77
            $parentClass = $this->getParentClass();
78
79
            return (new $parentClass())->getMorphClass();
80
        }
81
82
        return parent::getMorphClass();
83
    }
84
85
    protected function getParentClass()
86
    {
87
        static $parentClassName;
88
89
        return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName();
90
    }
91
}
92