RoutesWithFakeIds::resolveRouteBinding()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php namespace Propaganistas\LaravelFakeId;
2
3
use Illuminate\Support\Facades\App;
4
use Exception;
5
6
trait RoutesWithFakeIds
7
{
8
    /**
9
     * Get the value of the model's route key.
10
     *
11
     * @return mixed
12
     */
13 9
    public function getRouteKey()
14
    {
15 9
        return App::make('fakeid')->encode($this->getKey());
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
16
    }
17
18
    /**
19
     * Retrieve model for route model binding
20
     *
21
     * @param  mixed  $value
22
     * @return \Illuminate\Database\Eloquent\Model|null
23
     */
24 9
    public function resolveRouteBinding($value)
25
    {
26
        try {
27 9
            $value = App::make('fakeid')->decode($value);
28 3
        } catch (Exception $e) {
29 3
            return null;
30
        }
31
32 6
        return $this->where($this->getRouteKeyName(), $value)->first();
0 ignored issues
show
Bug introduced by
The method getRouteKeyName() does not exist on Propaganistas\LaravelFakeId\RoutesWithFakeIds. Did you maybe mean getRouteKey()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
It seems like where() 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...
33
    }
34
}
35