TestingGroupClass::methodRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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
    /**
20
     * @return array
21
     */
22
    public function getResultCreateTree1c()
23
    {
24
        /**
25
         * @var Group $group
26
         * @var GroupInterface $class
27
         */
28
        self::getMethodResult('createTree1c', ['cml.classifier.groups']);
29
        $params = self::getParams(['cml.classifier.groups']);
30
        $class = self::getPropertyClass();
31
        return $this->createTree($params[0], $class);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type yii\db\ActiveRecord; however, parameter $class of carono\exchange1c\models...roupClass::createTree() does only seem to accept carono\exchange1c\interfaces\GroupInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

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