Test Failed
Branch softDeleteActiveQueryTrait (21292c)
by Angel
05:01
created

Workflow::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\workflow\models;
4
5
use roaresearch\yii2\rmdb\SoftDeleteActiveQuery;
6
use yii\db\ActiveQuery;
7
8
/**
9
 * Model class for table `{{%workflow}}`
10
 *
11
 * @property integer $id
12
 * @property string $name
13
 *
14
 * @property Stage[] $stages
15
 */
16
class Workflow extends \roaresearch\yii2\rmdb\models\PersistentEntity
17
{
18
    /**
19
     * @var string full class name of the model to be used for the relation
20
     * `getStages()`.
21
     */
22
    protected $stageClass = Stage::class;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public static function tableName()
28
    {
29
        return '{{%workflow}}';
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    protected function attributeTypecast(): ?array
36
    {
37
        return parent::attributeTypecast() + ['id' => 'integer'];
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function rules()
44
    {
45
        return [
46
            [['name'], 'required'],
47
            [['name'], 'string', 'min' => 6],
48
            [['name'], 'unique'],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function attributeLabels()
56
    {
57
        return array_merge([
58
            'id' => 'ID',
59
            'name' => 'Workflow name',
60
        ], parent::attributeLabels());
61
    }
62
63
    /**
64
     * @return ActiveQuery
65
     */
66
    public function getStages(): SoftDeleteActiveQuery
67
    {
68
        return $this->hasMany($this->stageClass, ['workflow_id' => 'id'])
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hasMany($t...->inverseOf('workflow') returns the type yii\db\ActiveQuery which includes types incompatible with the type-hinted return roaresearch\yii2\rmdb\SoftDeleteActiveQuery.
Loading history...
69
            ->inverseOf('workflow');
70
    }
71
72
    /**
73
     * @return ActiveQuery
74
     */
75
    public function getDetailStages(): ActiveQuery
76
    {
77
        $query = $this->getStages();
78
        $query->multiple = false;
79
80
        return $query->select([
81
            'workflow_id',
82
            'totalStages' => 'count(distinct id)',
83
        ])->asArray()
84
        ->inverseOf(null)
85
        ->groupBy('workflow_id');
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function getTotalStages(): int
92
    {
93
        return (int)$this->detailStages['totalStages'];
0 ignored issues
show
Bug Best Practice introduced by
The property detailStages does not exist on roaresearch\yii2\workflow\models\Workflow. Since you implemented __get, consider adding a @property annotation.
Loading history...
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    protected function softDeleteConf(): array
100
    {
101
        return parent::softDeleteConf() + [
0 ignored issues
show
Bug introduced by
The method softDeleteConf() does not exist on roaresearch\yii2\rmdb\models\PersistentEntity. Did you maybe mean softDeleteBehaviorConfig()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        return parent::/** @scrutinizer ignore-call */ softDeleteConf() + [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            'allowDeleteCallback' => function ($record) {
103
                return !$record->getStages()->exists();
104
            },
105
        ];
106
    }
107
}
108