DataList   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 36
ccs 7
cts 7
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 3 1
A __construct() 0 5 1
A add() 0 3 1
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