AttachesToUsers   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserCart() 0 17 3
A attachTo() 0 12 2
A overwrite() 0 10 1
1
<?php
2
3
namespace Treestoneit\ShoppingCart\Concerns;
4
5
use Illuminate\Contracts\Auth\Authenticatable;
6
use Treestoneit\ShoppingCart\Models\Cart as CartModel;
7
8
trait AttachesToUsers
9
{
10
    /**
11
     * Load the given user's shopping cart.
12
     *
13
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
14
     * @return static
15
     */
16
    public function loadUserCart(Authenticatable $user): self
17
    {
18
        // If the user doesn't yet have a saved cart, we'll
19
        // just attach the current one to the user and exit.
20
        if (! $cart = CartModel::whereUserId($user->getAuthIdentifier())->with('items')->first()) {
21
            return $this->attachTo($user);
22
        }
23
24
        // If the current cart is empty, we'll load the saved one.
25
        if ($this->items()->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like items() 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...
26
            return $this->refreshCart($cart);
0 ignored issues
show
Bug introduced by
It seems like refreshCart() 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...
27
        }
28
29
        // Otherwise, we'll overwrite the saved cart with the current one.
30
        // TODO add a strategy to be able to merge with the saved cart
31
        return $this->overwrite($user);
32
    }
33
34
    /**
35
     * Attach the current cart to the given user.
36
     *
37
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
38
     * @return static
39
     */
40
    public function attachTo(Authenticatable $user): self
41
    {
42
        $this->cart->fill([
0 ignored issues
show
Bug introduced by
The property cart 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...
43
            'user_id' => $user->getAuthIdentifier(),
44
        ]);
45
46
        if ($this->cart->exists) {
47
            $this->cart->save();
48
        }
49
50
        return $this;
51
    }
52
53
    /**
54
     * Delete any old carts belonging to the given user and attach
55
     * the current cart to them.
56
     *
57
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
58
     * @return static
59
     */
60
    protected function overwrite(Authenticatable $user): self
61
    {
62
        CartModel::whereUserId($user->getAuthIdentifier())
0 ignored issues
show
Bug introduced by
The method whereKeyNot does only exist in Illuminate\Database\Eloquent\Builder, but not in Treestoneit\ShoppingCart\Models\Cart.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
63
                 ->whereKeyNot($this->cart->getKey())
64
                 // Delete them using Eloquent so that events will be fired
65
                 // and trigger deletion of cart items as well.
66
                 ->get()->each->delete();
67
68
        return $this->attachTo($user);
69
    }
70
}
71