Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#382)
by Cristian
03:31
created

SlugService::makeSlugUnique()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 9
nop 3
dl 0
loc 48
rs 5.5102
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class SlugService extends \Cviebrock\EloquentSluggable\Services\SlugService
8
{
9
    /**
10
     * Slug the current model.
11
     *
12
     * @param \Illuminate\Database\Eloquent\Model $model
13
     * @param bool $force
14
     * @return bool
15
     */
16
    public function slug(Model $model, $force = false)
17
    {
18
        $this->setModel($model);
19
20
        $attributes = [];
21
22
        foreach ($this->model->sluggable() as $attribute => $config) {
23
            if (is_numeric($attribute)) {
24
                $attribute = $config;
25
                $config = $this->getConfiguration();
26
            } else {
27
                $config = $this->getConfiguration($config);
28
            }
29
30
            $slug = $this->buildSlug($attribute, $config, $force);
31
32
            // customized for Backpack using SpatieTranslatable
33
            // save the attribute as a JSON
34
            $this->model->setAttribute($attribute.'->'.$model->getLocale(), $slug);
35
36
            $attributes[] = $attribute;
37
        }
38
39
        return $this->model->isDirty($attributes);
40
    }
41
42
    /**
43
     * Checks if the slug should be unique, and makes it so if needed.
44
     *
45
     * @param string $slug
46
     * @param array $config
47
     * @param string $attribute
48
     * @return string
49
     */
50
    protected function makeSlugUnique($slug, array $config, $attribute)
51
    {
52
        if (! $config['unique']) {
53
            return $slug;
54
        }
55
56
        $separator = $config['separator'];
57
58
        // find all models where the slug is like the current one
59
        $list = $this->getExistingSlugs($slug, $attribute, $config);
60
61
        // if ...
62
        // 	a) the list is empty, or
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
        // 	b) our slug isn't in the list
64
        // ... we are okay
65
        if (
66
            $list->count() === 0 ||
67
            $list->contains($slug) === false
68
        ) {
69
            return $slug;
70
        }
71
72
        // if our slug is in the list, but
73
        // 	a) it's for our model, or
74
        //  b) it looks like a suffixed version of our slug
75
        // ... we are also okay (use the current slug)
76
        if ($list->has($this->model->getKey())) {
77
            $currentSlug = $list->get($this->model->getKey());
78
79
            if (
80
                $currentSlug === $slug ||
81
                strpos($currentSlug, $slug) === 0
82
            ) {
83
                return $currentSlug;
84
            }
85
        }
86
87
        $method = $config['uniqueSuffix'];
88
        if ($method === null) {
89
            $suffix = $this->generateSuffix($slug, $separator, $list);
90
        } elseif (is_callable($method)) {
91
            $suffix = call_user_func($method, $slug, $separator, $list);
92
        } else {
93
            throw new \UnexpectedValueException('Sluggable "reserved" for '.get_class($this->model).':'.$attribute.' is not null, an array, or a closure that returns null/array.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 180 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
94
        }
95
96
        return $slug.$separator.$suffix;
97
    }
98
}
99