UsesHashIds::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Laravel Model Hashids.
5
 *
6
 * (c) Javier Cabello <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jaacu\LaravelModelHashids\Traits;
13
14
use ReflectionClass;
15
use Vinkla\Hashids\Facades\Hashids;
16
use Illuminate\Database\Eloquent\Model;
17
18
trait UsesHashIds
19
{
20
    public static function bootUsesHashIds()
21
    {
22
        static::creating(function (Model $model) {
23
            $model->fillable(array_merge($model->fillable, ['hash_id']));
24
        });
25
26
        static::created(function (Model $model) {
27
            $app_name = config('app.short_name') ?? str_limit(config('app.name'), 5, '');
0 ignored issues
show
Deprecated Code introduced by
The function str_limit() has been deprecated with message: Str::limit() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
28
            $model->update([
29
               'hash_id' => strtolower($app_name).'_'.strtolower($model->getHashShortName()).'_'.Hashids::encode($model->id),
30
            ]);
31
        });
32
    }
33
34
    public function getRouteKeyName()
35
    {
36
        return 'hash_id';
37
    }
38
39
    public function getId()
40
    {
41
        return $this->hash_id;
0 ignored issues
show
Bug introduced by
The property hash_id 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...
42
    }
43
44
    public function getHashShortName()
45
    {
46
        return strtolower(str_limit((new ReflectionClass($this) )->getShortName(), 4, ''));
0 ignored issues
show
Deprecated Code introduced by
The function str_limit() has been deprecated with message: Str::limit() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
47
    }
48
}
49