AllLinksDataObjects::getAllLinksInner()   C
last analyzed

Complexity

Conditions 15
Paths 25

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 28
c 1
b 0
f 0
nc 25
nop 1
dl 0
loc 49
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Api\Providers;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\DB;
8
use Sunnysideup\TemplateOverview\Api\AllLinksProviderBase;
9
10
class AllLinksDataObjects extends AllLinksProviderBase
11
{
12
    /**
13
     * @return array
14
     */
15
    public function getAllLinksInner(bool $inCMS)
16
    {
17
        //first() will return null or the object
18
        $return = [];
19
        $list = ClassInfo::subclassesFor(DataObject::class);
20
        $exceptForArray = array_merge($this->getListOfAllClasses(), [DataObject::class]);
21
        foreach ($list as $class) {
22
            if (! in_array($class, $exceptForArray, true)) {
23
                if ($this->isValidClass($class)) {
24
                    for ($i = 0; $i < $this->getNumberOfExamples(); ++$i) {
25
                        $obj = DataObject::get_one(
26
                            $class,
27
                            ['ClassName' => $class],
28
                            null,
29
                            DB::get_conn()->random() . ' ASC'
30
                        );
31
                        if (null !== $obj) {
32
                            if ($inCMS) {
33
                                if ($obj->hasMethod('CMSEditLink')) {
34
                                    $return[] = $obj->CMSEditLink();
35
                                }
36
37
                                if ($obj->hasMethod('CMSAddLink')) {
38
                                    $return[] = $obj->CMSAddLink();
39
                                }
40
41
                                if ($obj->hasMethod('CMSListLink')) {
42
                                    $return[] = $obj->CMSListLink();
43
                                }
44
45
                                if ($obj->hasMethod('PreviewLink')) {
46
                                    $return[] = $obj->PreviewLink();
47
                                }
48
                            } else {
49
                                if ($obj->hasMethod('Link') && ! (property_exists($obj, 'LinkID') && null !== $obj->LinkID)) {
50
                                    $return[] = $obj->Link();
51
                                }
52
53
                                if ($obj->hasMethod('getLink')) {
54
                                    $return[] = $obj->getLink();
55
                                }
56
                            }
57
                        }
58
                    }
59
                }
60
            }
61
        }
62
63
        return $return;
64
    }
65
}
66