LTreeTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLtreeParentColumn() 0 3 1
A getLtreeLevel() 0 3 2
A getLtreeParentId() 0 4 2
A getLtreePath() 0 7 3
A getLtreePathColumn() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\LTree\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Umbrellio\LTree\Interfaces\LTreeInterface;
9
use Umbrellio\LTree\Interfaces\LTreeModelInterface;
10
use Umbrellio\LTree\Types\LTreeType;
11
12
/**
13
 * @mixin Model
14
 */
15
trait LTreeTrait
16
{
17 27
    public function getLtreeParentColumn(): string
18
    {
19 27
        return 'parent_id';
20
    }
21
22 38
    public function getLtreePathColumn(): string
23
    {
24 38
        return 'path';
25
    }
26
27 4
    public function getLtreeParentId(): ?int
28
    {
29 4
        $value = $this->getAttribute($this->getLtreeParentColumn());
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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

29
        /** @scrutinizer ignore-call */ 
30
        $value = $this->getAttribute($this->getLtreeParentColumn());
Loading history...
30 4
        return $value ? (int) $value : null;
31
    }
32
33 32
    public function getLtreePath($mode = LTreeInterface::AS_ARRAY)
34
    {
35 32
        $path = $this->getAttribute($this->getLtreePathColumn());
36 32
        if ($mode === LTreeModelInterface::AS_ARRAY) {
37 23
            return $path !== null ? explode(LTreeType::TYPE_SEPARATE, $path) : [];
38
        }
39 13
        return (string) $path;
40
    }
41
42 3
    public function getLtreeLevel(): int
43
    {
44 3
        return is_array($path = $this->getLtreePath()) ? count($path) : 1;
45
    }
46
}
47