Code Duplication    Length = 29-29 lines in 2 locations

src/ServiceProvider.php 1 location

@@ 190-218 (lines=29) @@
187
     * @return string
188
     * @throws \UnexpectedValueException
189
     */
190
    protected function generateSlug(string $source, array $config, string $attribute): string
191
    {
192
        $separator          = $config['separator'];
193
        $method             = $config['method'];
194
        $maxLength          = $config['maxLength'];
195
        $maxLengthKeepWords = $config['maxLengthKeepWords'];
196
197
        if ($method === null) {
198
            $slugEngine = $this->getSlugEngine($attribute);
199
            $slug       = $slugEngine->slugify($source, $separator);
200
        } elseif (is_callable($method)) {
201
            $slug = call_user_func($method, $source, $separator);
202
        } else {
203
            throw new \UnexpectedValueException('Sluggable "method" for ' . get_class($this->model) . ':' . $attribute . ' is not callable nor null.');
204
        }
205
206
        $len = mb_strlen($slug);
207
        if (is_string($slug) && $maxLength && $len > $maxLength) {
208
            $reverseOffset    = $maxLength - $len;
209
            $lastSeparatorPos = mb_strrpos($slug, $separator, $reverseOffset);
210
            if ($maxLengthKeepWords && $lastSeparatorPos !== false) {
211
                $slug = mb_substr($slug, 0, $lastSeparatorPos);
212
            } else {
213
                $slug = trim(mb_substr($slug, 0, $maxLength), $separator);
214
            }
215
        }
216
217
        return $slug;
218
    }
219
220
    /**
221
     * Return a class that has a `slugify()` method, used to convert

src/Services/SlugService.php 1 location

@@ 176-204 (lines=29) @@
173
     * @return string
174
     * @throws \UnexpectedValueException
175
     */
176
    protected function generateSlug(string $source, array $config, string $attribute): string
177
    {
178
        $separator          = $config['separator'];
179
        $method             = $config['method'];
180
        $maxLength          = $config['maxLength'];
181
        $maxLengthKeepWords = $config['maxLengthKeepWords'];
182
183
        if ($method === null) {
184
            $slugEngine = $this->getSlugEngine($attribute);
185
            $slug       = $slugEngine->slugify($source, $separator);
186
        } elseif (is_callable($method)) {
187
            $slug = call_user_func($method, $source, $separator);
188
        } else {
189
            throw new \UnexpectedValueException('Sluggable "method" for ' . get_class($this->model) . ':' . $attribute . ' is not callable nor null.');
190
        }
191
192
        $len = mb_strlen($slug);
193
        if (is_string($slug) && $maxLength && $len > $maxLength) {
194
            $reverseOffset    = $maxLength - $len;
195
            $lastSeparatorPos = mb_strrpos($slug, $separator, $reverseOffset);
196
            if ($maxLengthKeepWords && $lastSeparatorPos !== false) {
197
                $slug = mb_substr($slug, 0, $lastSeparatorPos);
198
            } else {
199
                $slug = trim(mb_substr($slug, 0, $maxLength), $separator);
200
            }
201
        }
202
203
        return $slug;
204
    }
205
206
    /**
207
     * Return a class that has a `slugify()` method, used to convert