prepareSingleTableInheritance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
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