DataList::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Moo\HasOneSelector\ORM;
4
5
use Exception;
6
use Moo\HasOneSelector\Form\GridField;
7
use SilverStripe\ORM\DataList as BaseDataList;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * Class DataList is data list to manage add/remove managed object from the
12
 * HasOneSelectorField class.
13
 */
14
class DataList extends BaseDataList
15
{
16
    protected ?GridField $gridField = null;
17
18
    /**
19
     * HasOneSelectorDataList constructor.
20
     */
21 8
    public function __construct(GridField $gridField)
22
    {
23 8
        $this->gridField = $gridField;
24
25 8
        parent::__construct($gridField->getDataClass());
26
    }
27
28
    /**
29
     * Set the current selected record into the has one relation.
30
     *
31
     * @param DataObject $item
32
     *
33
     * @throws Exception
34
     */
35 1
    public function add($item): void
36
    {
37 1
        $this->gridField->setRecord($item);
0 ignored issues
show
Bug introduced by
The method setRecord() does not exist on null. ( Ignorable by Annotation )

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

37
        $this->gridField->/** @scrutinizer ignore-call */ 
38
                          setRecord($item);

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...
38
    }
39
40
    /**
41
     * Clear the record within the has one relation.
42
     *
43
     * @param DataObject $item
44
     *
45
     * @throws Exception
46
     */
47 1
    public function remove($item): void
48
    {
49 1
        $this->gridField->setRecord(null);
50
    }
51
}
52