Passed
Push — master ( 42f5ed...ac80ca )
by Robbie
01:47
created

BaseElementCMSEditLinkExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B updateCMSEditLink() 0 35 3
1
<?php
2
3
namespace DNADesign\ElementalList\Extension;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use DNADesign\ElementalList\Model\ElementList;
7
use SilverStripe\CMS\Controllers\CMSPageEditController;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Core\Extension;
10
11
/**
12
 * Class BaseElementCMSEditLinkExtension
13
 *
14
 * BaseElement can be nested, CMSEditLink() needs to be updated to reflect that
15
 *
16
 * @property BaseElementCMSEditLinkExtension|$this $owner
17
 * @package DNADesign\ElementalList\Extension
18
 */
19
class BaseElementCMSEditLinkExtension extends Extension
20
{
21
    /**
22
     * @param string $link
23
     */
24
    public function updateCMSEditLink(&$link)
25
    {
26
        /** @var $owner BaseElement */
27
        $owner = $this->owner;
28
29
        $relationName = $owner->getAreaRelationName();
30
        $page = $owner->getPage(true);
0 ignored issues
show
Unused Code introduced by
The call to DNADesign\Elemental\Models\BaseElement::getPage() has too many arguments starting with true. ( Ignorable by Annotation )

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

30
        /** @scrutinizer ignore-call */ 
31
        $page = $owner->getPage(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
32
        if (!$page) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $page of type null|false is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
33
            return;
34
        }
35
36
        if ($page instanceof ElementList) {
37
            // nested bock - we need to get edit link of parent block
38
            $link = Controller::join_links(
39
                $page->CMSEditLink(),
40
                'ItemEditForm/field/' . $page->getOwnedAreaRelationName() . '/item/',
41
                $owner->ID
42
            );
43
44
            // remove edit link from parent CMS link
45
            $link = preg_replace('/\/item\/([\d]+)\/edit/', '/item/$1', $link);
46
        } else {
47
            // block is directly under a non-block object - we have reached the top of nesting chain
48
            $link = Controller::join_links(
49
                singleton(CMSPageEditController::class)->Link('EditForm'),
50
                $page->ID,
51
                'field/' . $relationName . '/item/',
52
                $owner->ID
53
            );
54
        }
55
56
        $link = Controller::join_links(
57
            $link,
58
            'edit'
59
        );
60
    }
61
}
62