Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

TestingGroupClass::methodRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\exchange1c\models;
5
6
use carono\exchange1c\interfaces\GroupInterface;
7
use Zenwalker\CommerceML\Model\Group;
8
9
/**
10
 * Class TestingProductClass
11
 *
12
 * @package carono\exchange1c\models
13
 */
14
class TestingGroupClass extends TestingClass
15
{
16
    public $caption = 'Тестирование интерфейса группы';
17
    protected static $property = 'groupClass';
18
19
    public function getResultCreateTree1c()
20
    {
21
        /**
22
         * @var Group $group
23
         * @var GroupInterface $class
24
         */
25
        self::getMethodResult('createTree1c', ['cml.classifier.groups']);
26
        $params = self::getParams(['cml.classifier.groups']);
27
        $class = self::getPropertyClass();
28
        return $this->createTree($params[0], $class);
0 ignored issues
show
Bug introduced by
$class of type yii\db\ActiveRecord is incompatible with the type carono\exchange1c\interfaces\GroupInterface expected by parameter $class of carono\exchange1c\models...roupClass::createTree(). ( Ignorable by Annotation )

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

28
        return $this->createTree($params[0], /** @scrutinizer ignore-type */ $class);
Loading history...
29
    }
30
31
    /**
32
     * @param Group[] $groups
33
     * @param GroupInterface $class
34
     * @return array
35
     */
36
    private function createTree($groups, $class)
37
    {
38
        $result = [];
39
        $id = $class::getIdFieldName1c();
40
        foreach ($groups as $group) {
41
            if ($model = $class::find()->andWhere([$id => $group->id])->one()) {
42
                $result[] = $model;
43
            }
44
            if ($children = $group->getChildren()) {
45
                $result += $this->createTree($children, $class);
46
            }
47
        }
48
        return $result;
49
    }
50
51
    public static function methodRules()
52
    {
53
        return [
54
            'getIdFieldName1c' => [
55
                'return' => 'string',
56
                'auto' => true
57
            ],
58
            'createTree1c' => [
59
                'return' => false,
60
                'params' => ['cml.classifier.groups']
61
            ],
62
        ];
63
    }
64
}