Code Duplication    Length = 33-33 lines in 2 locations

src/ServiceProvider.php 1 location

@@ 256-288 (lines=33) @@
253
     * @return string
254
     * @throws \UnexpectedValueException
255
     */
256
    protected function validateSlug(string $slug, array $config, string $attribute): string
257
    {
258
        $separator = $config['separator'];
259
        $reserved  = $config['reserved'];
260
261
        if ($reserved === null) {
262
            return $slug;
263
        }
264
265
        // check for reserved names
266
        if ($reserved instanceof \Closure) {
267
            $reserved = $reserved($this->model);
268
        }
269
270
        if (is_array($reserved)) {
271
            if (in_array($slug, $reserved)) {
272
                $method = $config['uniqueSuffix'];
273
                if ($method === null) {
274
                    $suffix = $this->generateSuffix($slug, $separator, collect($reserved));
275
                } elseif (is_callable($method)) {
276
                    $suffix = $method($slug, $separator, collect($reserved));
277
                } else {
278
                    throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
279
                }
280
281
                return $slug . $separator . $suffix;
282
            }
283
284
            return $slug;
285
        }
286
287
        throw new \UnexpectedValueException('Sluggable "reserved" for ' . get_class($this->model) . ':' . $attribute . ' is not null, an array, or a closure that returns null/array.');
288
    }
289
290
    /**
291
     * Checks if the slug should be unique, and makes it so if needed.

src/Services/SlugService.php 1 location

@@ 242-274 (lines=33) @@
239
     * @return string
240
     * @throws \UnexpectedValueException
241
     */
242
    protected function validateSlug(string $slug, array $config, string $attribute): string
243
    {
244
        $separator = $config['separator'];
245
        $reserved  = $config['reserved'];
246
247
        if ($reserved === null) {
248
            return $slug;
249
        }
250
251
        // check for reserved names
252
        if ($reserved instanceof \Closure) {
253
            $reserved = $reserved($this->model);
254
        }
255
256
        if (is_array($reserved)) {
257
            if (in_array($slug, $reserved)) {
258
                $method = $config['uniqueSuffix'];
259
                if ($method === null) {
260
                    $suffix = $this->generateSuffix($slug, $separator, collect($reserved));
261
                } elseif (is_callable($method)) {
262
                    $suffix = $method($slug, $separator, collect($reserved));
263
                } else {
264
                    throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
265
                }
266
267
                return $slug . $separator . $suffix;
268
            }
269
270
            return $slug;
271
        }
272
273
        throw new \UnexpectedValueException('Sluggable "reserved" for ' . get_class($this->model) . ':' . $attribute . ' is not null, an array, or a closure that returns null/array.');
274
    }
275
276
    /**
277
     * Checks if the slug should be unique, and makes it so if needed.