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); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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
|
|
|
|
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.