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

HasTablesClassesMap   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 38
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableClassNameByType() 0 7 3
A makeTableObjectNameByType() 0 5 1
1
<?php
2
3
namespace LaraGeoData\Console\Commands;
4
5
use LaraGeoData\Database\Tables\Geonames;
6
use LaraGeoData\Database\Tables\GeoTable;
7
use LaraGeoData\Database\Tables\Postalcodes;
8
9
trait HasTablesClassesMap
10
{
11
12
    /**
13
     * map describe command type and table class.
14
     *
15
     * @var array
16
     */
17
    protected array $classesMap = [
18
        'geonames'    => Geonames::class,
19
        'postalcodes' => Postalcodes::class,
20
    ];
21
22
    /**
23
     * @param string $type
24
     * @return GeoTable
25
     * @throws \Exception
26
     */
27 14
    public function getTableClassNameByType(string $type)
28
    {
29 14
        if (!isset($this->classesMap[$type]) || !is_a($this->classesMap[$type], GeoTable::class, true)) {
30 1
            throw new \Exception("Class for type [{$type}] not found.");
31
        }
32
33 13
        return $this->classesMap[$type];
34
    }
35
36
    /**
37
     * @param string $type
38
     * @param        ...$attributes
39
     * @return mixed
40
     * @throws \Exception
41
     */
42 14
    public function makeTableObjectNameByType(string $type, ...$attributes): GeoTable
43
    {
44 14
        $tableClassName = $this->getTableClassNameByType($type);
45
46 13
        return new $tableClassName(...$attributes);
0 ignored issues
show
Unused Code introduced by
The call to LaraGeoData\Database\Tab...GeoTable::__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

46
        return /** @scrutinizer ignore-call */ new $tableClassName(...$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...
47
    }
48
}
49