BelongsToMany::detach()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 25
rs 9.7998
1
<?php
2
3
namespace Signifly\PivotEvents;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany as BaseBelongsToMany;
6
7
class BelongsToMany extends BaseBelongsToMany
8
{
9
    /**
10
     * Attach a model to the parent.
11
     *
12
     * @param  mixed  $id
13
     * @param  array  $attributes
14
     * @param  bool   $touch
15
     * @return void
16
     */
17
    public function attach($id, array $attributes = [], $touch = true)
18
    {
19
        $this->parent->setPivotChanges('attach', $this->getRelationName(), [
20
            $this->parseId($id) => $attributes,
21
        ]);
22
23
        if ($this->parent->firePivotAttachingEvent() === false) {
24
            return false;
25
        }
26
27
        $result = parent::attach($id, $attributes, $touch);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as parent::attach($id, $attributes, $touch) targeting Illuminate\Database\Eloq...ithPivotTable::attach() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
29
        $this->parent->firePivotAttachedEvent();
30
31
        $this->parent->resetPivotChanges();
32
33
        return $result;
34
    }
35
36
    /**
37
     * Detach models from the relationship.
38
     *
39
     * @param  mixed  $ids
40
     * @param  bool  $touch
41
     * @return int
42
     */
43
    public function detach($ids = null, $touch = true)
44
    {
45
        if (is_null($ids)) {
46
            $ids = $this->query->pluck(
47
                $this->query->qualifyColumn($this->relatedKey)
48
            )->toArray();
49
        }
50
51
        $idsWithAttributes = collect($ids)->mapWithKeys(function ($id) {
52
            return [$id => []];
53
        })->all();
54
55
        $this->parent->setPivotChanges('detach', $this->getRelationName(), $idsWithAttributes);
56
57
        if ($this->parent->firePivotDetachingEvent() === false) {
58
            return false;
59
        }
60
61
        $result = parent::detach($ids, $touch);
62
63
        $this->parent->firePivotDetachedEvent();
64
65
        $this->parent->resetPivotChanges();
66
67
        return $result;
68
    }
69
70
    /**
71
     * Update an existing pivot record on the table.
72
     *
73
     * @param  mixed  $id
74
     * @param  array  $attributes
75
     * @param  bool   $touch
76
     * @return int
77
     */
78
    public function updateExistingPivot($id, array $attributes, $touch = true)
79
    {
80
        $this->parent->setPivotChanges('update', $this->getRelationName(), [
81
            $this->parseId($id) => $attributes,
82
        ]);
83
84
        if ($this->parent->firePivotUpdatingEvent() === false) {
85
            return false;
86
        }
87
88
        $result = parent::updateExistingPivot($id, $attributes, $touch);
89
90
        $this->parent->firePivotUpdatedEvent();
91
92
        $this->parent->resetPivotChanges();
93
94
        return $result;
95
    }
96
}
97