Test Failed
Pull Request — master (#254)
by Sergei
12:56
created

FormHydrator::populate()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 6
nop 5
dl 0
loc 28
rs 9.5222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Yiisoft\Hydrator\HydratorInterface;
8
9
use function is_array;
10
11
/**
12
 * @psalm-import-type MapType from HydratorInterface
13
 */
14
final class FormHydrator
15
{
16
    public function __construct(
17
        private HydratorInterface $hydrator,
18
    ) {
19
    }
20
21
    /**
22
     * @psalm-param MapType $map
23
     */
24
    public function populate(
25
        FormModel $model,
26
        mixed $data,
27
        array $map = [],
28
        bool $strict = false,
29
        ?string $scope = null
30
    ): bool {
31
        if (!is_array($data)) {
32
            return false;
33
        }
34
35
        $scope ??= $model->getFormName();
36
        if ($scope === '') {
37
            $hydrateData = $data;
38
        } else {
39
            if (!isset($data[$scope])) {
40
                return false;
41
            }
42
            $hydrateData = $data[$scope];
43
        }
44
45
        if (!is_array($hydrateData)) {
46
            return false;
47
        }
48
49
        $this->hydrator->hydrate($model, $hydrateData, $map, $strict);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Hydrator\HydratorInterface::hydrate() has too many arguments starting with $map. ( Ignorable by Annotation )

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

49
        $this->hydrator->/** @scrutinizer ignore-call */ 
50
                         hydrate($model, $hydrateData, $map, $strict);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
50
51
        return true;
52
    }
53
}
54