Completed
Push — master ( 5b1cad...834a37 )
by Colin
02:55
created

SluggableScopeHelpers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSlugKeyName() 0 10 2
A getSlugKey() 0 4 1
A scopeWhereSlug() 0 4 1
A findBySlug() 0 4 1
A findBySlugOrFail() 0 4 1
1
<?php namespace Cviebrock\EloquentSluggable;
2
3
/**
4
 * Class PrimarySlug
5
 *
6
 * Helper trait for defining the primary slug of a model
7
 * and providing useful scopes and query methods.
8
 *
9
 * @package Cviebrock\EloquentSluggable
10
 */
11
trait SluggableScopeHelpers
12
{
13
14
    /**
15
     * Primary slug column of this model.
16
     *
17
     * @return string
18
     */
19
    public function getSlugKeyName()
20
    {
21
        if (property_exists($this, 'slugKeyName')) {
22
            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...
23
        }
24
25
        $keys = 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...
26
27
        return reset($keys);
28
    }
29
30
    /**
31
     * Primary slug value of this model.
32
     *
33
     * @return string
34
     */
35
    public function getSlugKey()
36
    {
37
        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...
38
    }
39
40
    /**
41
     * Query scope for finding a model by its primary slug.
42
     *
43
     * @param \Illuminate\Database\Eloquent\Builder $scope
44
     * @param string $slug
45
     * @return \Illuminate\Database\Eloquent\Builder
46
     */
47
    public function scopeWhereSlug($scope, $slug)
48
    {
49
        return $scope->where($this->getSlugKeyName(), $slug);
50
    }
51
52
    /**
53
     * Find a model by its primary slug.
54
     *
55
     * @param string $slug
56
     * @param array $columns
57
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
58
     */
59
    public static function findBySlug($slug, array $columns = ['*'])
60
    {
61
        return static::whereSlug($slug)->first($columns);
0 ignored issues
show
Bug introduced by
The method whereSlug() does not exist on Cviebrock\EloquentSluggable\SluggableScopeHelpers. 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...
62
    }
63
64
    /**
65
     * Find a model by its primary slug or throw an exception.
66
     *
67
     * @param string $slug
68
     * @param array $columns
69
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
70
     *
71
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
72
     */
73
    public static function findBySlugOrFail($slug, array $columns = ['*'])
74
    {
75
        return static::whereSlug($slug)->firstOrFail($columns);
0 ignored issues
show
Bug introduced by
The method whereSlug() does not exist on Cviebrock\EloquentSluggable\SluggableScopeHelpers. 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...
76
    }
77
}
78