Passed
Push — master ( ef3aeb...986e68 )
by Yaroslav
04:23
created

HasTableWithSuffix::makeUsingSuffix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LaraGeoData\Models;
4
5
trait HasTableWithSuffix
6
{
7
    /**
8
     * Get base table name (root name, without suffix).
9
     *
10
     * @return string
11
     */
12
    abstract public function getTableNameRoot(): string;
13
14
    /**
15
     * @inheritDoc
16
     */
17 4
    public function getTable(): string
18
    {
19 4
        if ($this->table) {
20 4
            return $this->table;
21
        }
22 2
        $tableName = $this->getTableNameRoot();
23 2
        if ($suffix = config('geonames.database.default_suffix')) {
24 2
            $tableName = "{$tableName}_{$suffix}";
25
        }
26
27 2
        return $tableName;
28
    }
29
30
    /**
31
     * Initialise model with suffixed table name.
32
     *
33
     * @param string|null $suffix
34
     * @return static
35
     */
36 4
    public static function makeUsingSuffix(?string $suffix = null): static
37
    {
38 4
        $instance  = new static();
39 4
        $tableName = $instance->getTableNameRoot();
40 4
        if ($suffix) {
41 4
            $tableName = "{$tableName}_{$suffix}";
42
        }
43
44 4
        return $instance->setTable($tableName);
0 ignored issues
show
Bug introduced by
It seems like setTable() 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

44
        return $instance->/** @scrutinizer ignore-call */ setTable($tableName);
Loading history...
45
    }
46
}
47