Test Failed
Pull Request — master (#54)
by
unknown
02:54
created

SaveRelationsTrait::_prepareLoadData()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 28
rs 9.1111
1
<?php
2
3
namespace lhs\Yii2SaveRelationsBehavior;
4
5
/**
6
 * Trait SaveRelationsTrait
7
 * @package lhs\Yii2SaveRelationsBehavior
8
 *
9
 * @mixin SaveRelationsBehavior
10
 */
11
trait SaveRelationsTrait
12
{
13
14
    public function load($data, $formName = null)
15
    {
16
        $loaded = parent::load($data, $formName);
17
        if ($loaded && $this->hasMethod('loadRelations')) {
0 ignored issues
show
Bug introduced by
It seems like hasMethod() 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

17
        if ($loaded && $this->/** @scrutinizer ignore-call */ hasMethod('loadRelations')) {
Loading history...
18
            $this->_prepareLoadData($data);
19
20
            $this->loadRelations($data);
0 ignored issues
show
Bug introduced by
It seems like loadRelations() 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

20
            $this->/** @scrutinizer ignore-call */ 
21
                   loadRelations($data);
Loading history...
21
        }
22
        return $loaded;
23
    }
24
25
    /**
26
     * @param $data
27
     */
28
    private function _prepareLoadData(&$data) {
29
        $scope = $formName === null ? $this->formName() : $formName;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $formName seems to be never defined.
Loading history...
Bug introduced by
It seems like formName() 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
        $scope = $formName === null ? $this->/** @scrutinizer ignore-call */ formName() : $formName;
Loading history...
30
31
        foreach ($this->relations as $key => $value) {
32
            if (is_int($key)) {
33
                $relationName = $value;
34
            } else {
35
                $relationName = $key;
36
            }
37
38
            if(!isset($data[$scope][$relationName])) {
39
                continue;
40
            }
41
42
            $relation = $this->getRelation($relationName);
0 ignored issues
show
Bug introduced by
It seems like getRelation() 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

42
            /** @scrutinizer ignore-call */ 
43
            $relation = $this->getRelation($relationName);
Loading history...
43
44
            if(!$relation) {
45
                continue;
46
            }
47
48
            $modelClass = $relation->modelClass;
49
            /** @var ActiveQuery $relationalModel */
50
            $relationalModel = new $modelClass;
51
            $keyName = $relationalModel->formName();
52
53
            $data[$keyName] = $data[$scope][$relationName];
54
55
            unset($data[$scope][$relationName]);
56
        }
57
    }
58
}
59