SingleTableInheritanceQueryTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 19
ccs 5
cts 5
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareSingleTableInheritance() 0 4 1
A prepare() 0 4 1
1
<?php
2
3
namespace SamIT\Yii2\SingleTableInheritance;
4
5
/**
6
 * Trait SingleTableInheritanceQueryTrait
7
 * This trait will automatically add a type filter during `prepare()`. If you define a prepare method in the class that
8
 * uses this trait make sure to call `prepareSingleTableInheritance()`.
9
 * The using class should be an ActiveQuery class
10
 * @package SamIT\Yii2\SingleTableInheritance
11
 */
12
trait SingleTableInheritanceQueryTrait
13
{
14
    /**
15
     * @var string
16
     */
17
    public $modelClass;
18
    abstract public function andFilterWhere(array $condition);
19
20
    final protected function prepareSingleTableInheritance(): void
21
    {
22 2
        $modelClass = $this->modelClass;
23
        $this->andFilterWhere([$modelClass::getInheritanceColumn() => $modelClass::getTypeFromClass($modelClass)]);
24 2
    }
25 2
26 2
27
    public function prepare($builder)
28
    {
29 2
        $this->prepareSingleTableInheritance();
30
        return parent::prepare($builder);
31 2
    }
32
}
33