Completed
Push — master ( 304470...5b1cad )
by Colin
9s
created

PrimarySlug::findBySlugOrFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cviebrock\EloquentSluggable;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class PrimarySlug
11
 * Helper trait for defining the primary slug of a model.
12
 *
13
 * @package Cviebrock\EloquentSluggable
14
 */
15
trait PrimarySlug
16
{
17
18
    /**
19
     * Primary slug column of this model.
20
     *
21
     * @return string
22
     */
23
    public function getSlugKeyName()
24
    {
25
        if (property_exists($this, 'slugKeyName')) {
26
            return $this->slugKeyName;
0 ignored issues
show
Bug introduced by
The property slugKeyName 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...
27
        }
28
29
        return array_first(array_keys($this->sluggable()));
0 ignored issues
show
Bug introduced by
It seems like sluggable() 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...
30
    }
31
32
    /**
33
     * Primary slug value of this model.
34
     *
35
     * @return string
36
     */
37
    public function getSlugKey()
38
    {
39
        return $this->getAttribute($this->getSlugKeyName());
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
40
    }
41
42
    /**
43
     * Query scope for finding a model by primary slug.
44
     *
45
     * @param Builder $scope
46
     * @param string $slug
47
     * @return Builder
48
     */
49
    public function scopeWhereSlug($scope, $slug)
50
    {
51
        return $scope->where($this->getSlugKeyName(), $slug);
52
    }
53
54
    /**
55
     * Find Model by primary slug. Fallback to find by id. Fail if not found.
56
     *
57
     * @param string $slug
58
     * @return Model|Collection|null
59
     */
60
    public static function findBySlug($slug)
61
    {
62
        return static::whereSlug($slug)->first();
0 ignored issues
show
Bug introduced by
The method whereSlug() does not exist on Cviebrock\EloquentSluggable\PrimarySlug. Did you maybe mean scopeWhereSlug()?

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...
63
    }
64
65
    /**
66
     * Find by primary slug. Fail if not found.
67
     *
68
     * @param string $slug
69
     * @return Model
70
     */
71
    public static function findBySlugOrFail($slug)
72
    {
73
        return static::whereSlug($slug)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method whereSlug() does not exist on Cviebrock\EloquentSluggable\PrimarySlug. Did you maybe mean scopeWhereSlug()?

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...
74
    }
75
76
}
77