HasOneHandler::handleHasOneType()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 18
rs 8.4444
cc 8
nc 11
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 HasOneRelationHandler
10
 *
11
 * @property-read \Dynamic\Salsify\Model\Mapper|HasOneHandler $owner
12
 */
13
class HasOneHandler extends Extension
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $field_types = [
0 ignored issues
show
introduced by
The private property $field_types is not used, and could be removed.
Loading history...
19
        'HasOne' => [
20
            'requiresWrite' => true,
21
            'requiresSalsifyObjects' => false,
22
            'allowsModification' => true,
23
        ],
24
    ];
25
26
    /**
27
     * @param string|DataObject $class
28
     * @param $data
29
     * @param $dataField
30
     * @param $config
31
     * @param $dbField
32
     *
33
     * @return int|DataObject
34
     * @throws \Exception
35
     */
36
    public function handleHasOneType($class, $data, $dataField, $config, $dbField)
0 ignored issues
show
Unused Code introduced by
The parameter $dataField 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

36
    public function handleHasOneType($class, $data, /** @scrutinizer ignore-unused */ $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...
37
    {
38
        if (!array_key_exists('relation', $config) || !is_array($config['relation'])) {
39
            return preg_match('/ID$/', $dbField) ? 0 : null;
40
        }
41
42
        $relationConfig = $config['relation'];
43
        $relatedClass = array_key_first($relationConfig);
44
45
        foreach ($this->owner->yieldKeyVal($relationConfig[$relatedClass]) as $dbField => $salsifyField) {
0 ignored issues
show
Bug introduced by
The method yieldKeyVal() does not exist on Dynamic\Salsify\TypeHandler\Relation\HasOneHandler. ( Ignorable by Annotation )

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

45
        foreach ($this->owner->/** @scrutinizer ignore-call */ yieldKeyVal($relationConfig[$relatedClass]) as $dbField => $salsifyField) {

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...
introduced by
$dbField is overwriting one of the parameters of this function.
Loading history...
46
            if ($this->owner->handleShouldSkip($class, $dbField, $salsifyField, $data)) {
0 ignored issues
show
Bug introduced by
The method handleShouldSkip() does not exist on Dynamic\Salsify\TypeHandler\Relation\HasOneHandler. ( Ignorable by Annotation )

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

46
            if ($this->owner->/** @scrutinizer ignore-call */ handleShouldSkip($class, $dbField, $salsifyField, $data)) {

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...
Bug introduced by
It seems like $dbField can also be of type true; however, parameter $dbField of Dynamic\Salsify\Model\Mapper::handleShouldSkip() does only seem to accept string, 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

46
            if ($this->owner->handleShouldSkip($class, /** @scrutinizer ignore-type */ $dbField, $salsifyField, $data)) {
Loading history...
47
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SilverStripe\ORM\DataObject|integer.
Loading history...
48
            }
49
        }
50
51
        $object = $this->owner->mapToObject($relatedClass, $relationConfig[$relatedClass], $data);
0 ignored issues
show
Bug introduced by
The method mapToObject() does not exist on Dynamic\Salsify\TypeHandler\Relation\HasOneHandler. ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $object = $this->owner->mapToObject($relatedClass, $relationConfig[$relatedClass], $data);

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...
52
        $objectID = $object ? $object->ID : 0;
0 ignored issues
show
introduced by
$object is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
53
        return preg_match('/ID$/', $dbField) ? $objectID : $object;
54
    }
55
}
56