ManyHandler::handleManyRelationType()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 23
rs 9.5222
cc 5
nc 4
nop 5
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Relation;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Class RelationHandler
10
 * @package Dynamic\Salsify\TypeHandler
11
 *
12
 * @property-read \Dynamic\Salsify\Model\Mapper|ManyHandler $owner
13
 */
14
class ManyHandler extends Extension
15
{
16
17
    /**
18
     * @var array
19
     */
20
    private static $field_types = [
0 ignored issues
show
introduced by
The private property $field_types is not used, and could be removed.
Loading history...
21
        'ManyRelation' => [
22
            'requiresWrite' => true,
23
            'requiresSalsifyObjects' => false,
24
            'allowsModification' => true,
25
        ],
26
    ];
27
28
    /**
29
     * @param string|DataObject $class
30
     * @param $data
31
     * @param $dataField
32
     * @param $config
33
     * @param $dbField
34
     *
35
     * @return int|DataObject|array
36
     * @throws \Exception
37
     */
38
    public function handleManyRelationType($class, $data, $dataField, $config, $dbField)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed. ( Ignorable by Annotation )

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

38
    public function handleManyRelationType(/** @scrutinizer ignore-unused */ $class, $data, $dataField, $config, $dbField)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $dbField is not used and could be removed. ( Ignorable by Annotation )

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

38
    public function handleManyRelationType($class, $data, $dataField, $config, /** @scrutinizer ignore-unused */ $dbField)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        if (!array_key_exists('relation', $config) || !is_array($config['relation'])) {
41
            return [];
42
        }
43
44
        $fieldData = $data[$dataField];
45
        $related = [];
46
47
        foreach ($this->owner->yieldSingle($fieldData) as $entry) {
0 ignored issues
show
Bug introduced by
The method yieldSingle() does not exist on Dynamic\Salsify\TypeHandler\Relation\ManyHandler. ( Ignorable by Annotation )

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

47
        foreach ($this->owner->/** @scrutinizer ignore-call */ yieldSingle($fieldData) as $entry) {

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...
48
            $entryData = array_merge($data, [
49
                $dataField => $entry,
50
            ]);
51
52
            if (is_array($entry)) {
53
                $entryData = array_merge($entryData, $entry);
54
            }
55
56
            $relationConfig = $config['relation'];
57
            $relatedClass = array_key_first($relationConfig);
58
            $related[] = $this->owner->mapToObject($relatedClass, $relationConfig[$relatedClass], $entryData);
0 ignored issues
show
Bug introduced by
The method mapToObject() does not exist on Dynamic\Salsify\TypeHandler\Relation\ManyHandler. ( Ignorable by Annotation )

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

58
            /** @scrutinizer ignore-call */ 
59
            $related[] = $this->owner->mapToObject($relatedClass, $relationConfig[$relatedClass], $entryData);

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...
59
        }
60
        return $related;
61
    }
62
}
63