Completed
Push — 4.4 ( cfe86a...592ab6 )
by Robbie
35:44 queued 28:14
created

TreeNode   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 10 1
A resetCounts() 0 7 1
1
<?php
2
3
namespace SilverStripe\ORM\Tests\DataObjectTest;
4
5
use SilverStripe\Dev\TestOnly;
6
use SilverStripe\ORM\Connect\Query;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\DB;
9
use SilverStripe\ORM\HasManyList;
10
use SilverStripe\ORM\Hierarchy\Hierarchy;
11
use SilverStripe\ORM\ManyManyList;
12
use SilverStripe\ORM\Queries\SQLUpdate;
13
14
/**
15
 * The purpose of this test class is to test recursive writes and make sure we don't get stuck in an infinite loop.
16
 * @property int $WriteCount Number of times this object was written sine the last call of `resetCount`
17
 */
18
class TreeNode extends DataObject implements TestOnly
19
{
20
    private static $table_name = 'DataObjectTest_TreeNode';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
21
22
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        'Title' => 'Varchar',
24
        'WriteCount' => 'Int'
25
    ];
26
27
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
28
        'Parent' => self::class,
29
        'Cycle' => self::class,
30
    ];
31
32
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
33
        'Children' => self::class,
34
    ];
35
36
    public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false)
37
    {
38
        // Force the component to fetch its Parent and Cycle relation so we have components to recursively write
39
        $this->Parent;
0 ignored issues
show
Bug Best Practice introduced by
The property Parent does not exist on SilverStripe\ORM\Tests\DataObjectTest\TreeNode. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
        $this->Cycle;
0 ignored issues
show
Bug Best Practice introduced by
The property Cycle does not exist on SilverStripe\ORM\Tests\DataObjectTest\TreeNode. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
42
        // Count a write attempts
43
        $this->WriteCount++;
44
45
        return parent::write($showDebug, $forceInsert, $forceWrite, $writeComponents);
46
    }
47
48
    /**
49
     * Reset the WriteCount on all TreeNodes
50
     */
51
    public function resetCounts()
52
    {
53
        $update = new SQLUpdate(
54
            sprintf('"%s"', self::baseTable()),
0 ignored issues
show
Bug Best Practice introduced by
The method SilverStripe\ORM\DataObject::baseTable() is not static, but was called statically. ( Ignorable by Annotation )

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

54
            sprintf('"%s"', self::/** @scrutinizer ignore-call */ baseTable()),
Loading history...
55
            ['"WriteCount"' => 0]
56
        );
57
        $results = $update->execute();
0 ignored issues
show
Unused Code introduced by
The assignment to $results is dead and can be removed.
Loading history...
58
    }
59
}
60