Passed
Push — master ( 1a4dd8...786e6d )
by Carlos
06:24
created

BelongsToMany::updateExistingPivot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-follow
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\LaravelFollow;
13
14
use Illuminate\Database\Eloquent\Relations\BelongsToMany as BaseBelongsToMany;
15
16
class BelongsToMany extends BaseBelongsToMany
17
{
18
    /**
19
     * Attach a model to the parent.
20
     *
21
     * @param mixed $id
22
     * @param array $attributes
23
     * @param bool  $touch
24
     */
25
    public function attach($id, array $attributes = [], $touch = true)
26
    {
27
        $this->parent->setPivotChanges('attach', $this->getRelationName(), [
28
            $id => $attributes,
29
        ]);
30
31
        if (false === $this->parent->firePivotAttachingEvent()) {
32
            return false;
33
        }
34
35
        $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...
36
37
        $this->parent->firePivotAttachedEvent();
38
39
        return $result;
40
    }
41
42
    /**
43
     * Detach models from the relationship.
44
     *
45
     * @param mixed $ids
46
     * @param bool  $touch
47
     *
48
     * @return int
49
     */
50
    public function detach($ids = null, $touch = true)
51
    {
52
        if (is_null($ids)) {
53
            $ids = $this->query->pluck(
54
                $this->query->qualifyColumn($this->relatedKey)
55
            )->toArray();
56
        }
57
58
        $idsWithAttributes = collect($ids)->mapWithKeys(function ($id) {
59
            return [$id => []];
60
        })->all();
61
62
        $this->parent->setPivotChanges('detach', $this->getRelationName(), $idsWithAttributes);
63
64
        if (false === $this->parent->firePivotDetachingEvent()) {
65
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
66
        }
67
68
        $result = parent::detach($ids, $touch);
69
70
        $this->parent->firePivotDetachedEvent();
71
72
        return $result;
73
    }
74
75
    /**
76
     * Update an existing pivot record on the table.
77
     *
78
     * @param mixed $id
79
     * @param array $attributes
80
     * @param bool  $touch
81
     *
82
     * @return int
83
     */
84
    public function updateExistingPivot($id, array $attributes, $touch = true)
85
    {
86
        $this->parent->setPivotChanges('update', $this->getRelationName(), [
87
            $id => $attributes,
88
        ]);
89
90
        if (false === $this->parent->firePivotUpdatingEvent()) {
91
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
92
        }
93
94
        $result = parent::updateExistingPivot($id, $attributes, $touch);
95
96
        $this->parent->firePivotUpdatedEvent();
97
98
        return $result;
99
    }
100
}
101