HandleModel::getTableColumns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ianrizky\Illuminate\Database\Eloquent\Concerns;
4
5
use Illuminate\Database\Connection;
6
use Illuminate\Database\ConnectionInterface;
7
8
trait HandleModel
9
{
10
    /**
11
     * Create a new Eloquent model instance in static way.
12
     *
13
     * @param  array  $attributes
14
     * @return static
15
     */
16
    public static function instance(array $attributes = [])
17
    {
18
        return new static($attributes);
0 ignored issues
show
Unused Code introduced by
The call to Ianrizky\Illuminate\Data...dleModel::__construct() has too many arguments starting with $attributes. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        return /** @scrutinizer ignore-call */ new static($attributes);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
19
    }
20
21
    /**
22
     * Get the table associated with the model in static way.
23
     *
24
     * @return string
25
     */
26
    public static function getTableName(): string
27
    {
28
        return static::instance()->getTable();
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on Ianrizky\Illuminate\Data...nt\Concerns\HandleModel. Did you maybe mean getTableName()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        return static::instance()->/** @scrutinizer ignore-call */ getTable();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
    }
30
31
    /**
32
     * Get the current connection name for the model in static way.
33
     *
34
     * @return string|null
35
     */
36
    public static function connectionName(): ?string
37
    {
38
        return static::instance()->getConnectionName();
0 ignored issues
show
Bug introduced by
It seems like getConnectionName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        return static::instance()->/** @scrutinizer ignore-call */ getConnectionName();
Loading history...
39
    }
40
41
    /**
42
     * Get the database connection for the model in static way.
43
     *
44
     * @return \Illuminate\Database\Connection
45
     */
46
    public static function connection(): Connection
47
    {
48
        return static::instance()->getConnection();
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        return static::instance()->/** @scrutinizer ignore-call */ getConnection();
Loading history...
49
    }
50
51
    /**
52
     * Start a new database transaction for the model in static way.
53
     *
54
     * @return \Illuminate\Database\Connection
55
     */
56
    public static function beginTransaction(): Connection
57
    {
58
        return tap(static::connection(), function (ConnectionInterface $connection) {
59
            $connection->beginTransaction();
60
        });
61
    }
62
63
    /**
64
     * Get the table morph name using on polymorphic relation associated with the model in static way.
65
     *
66
     * @return string
67
     */
68
    public static function getMorphName(): string
69
    {
70
        $model = static::instance();
71
72
        if (property_exists($model, 'morphName')) {
73
            return $model->morphName;
74
        }
75
76
        return get_class($model);
77
    }
78
79
    /**
80
     * Return list of column of given table.
81
     *
82
     * @return array
83
     */
84
    public static function getTableColumns(): array
85
    {
86
        $model = static::instance();
87
88
        return $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
89
    }
90
}
91