Passed
Pull Request — master (#27)
by Matthew
01:56
created

HasOneHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handleHasOneType() 0 10 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\TypeHandler\Relation\HasOneHandler|\Dynamic\Salsify\Model\Mapper $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
    ];
21
22
    /**
23
     * @param string|DataObject $class
24
     * @param $data
25
     * @param $dataField
26
     * @param $config
27
     * @param $dbField
28
     *
29
     * @return int|DataObject
30
     * @throws \Exception
31
     */
32
    public function handleHasOneType($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

32
    public function handleHasOneType(/** @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 $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

32
    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...
33
    {
34
        if (!array_key_exists('relation', $config) || !is_array($config['relation'])) {
35
            return preg_match('/ID$/', $dbField) ? 0 : null;
36
        }
37
38
        $relationConfig = $config['relation'];
39
        $relatedClass = array_key_first($relationConfig);
40
        $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

40
        /** @scrutinizer ignore-call */ 
41
        $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...
41
        return preg_match('/ID$/', $dbField) ? $object->ID : $object;
42
    }
43
}
44