Completed
Push — master ( 48c70c...7bac57 )
by Nicolaas
01:40
created

CMSEditLinkField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 21 6
1
<?php
2
3
4
class CMSEditLinkField extends ReadonlyField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    protected $linkedObject = '';
7
8
    /**
9
     *
10
     * @param string $name                 e.g. MyLinkedObjectID
11
     * @param string $title                e.g. My Fancy Title
12
     * @param DataObject $linkedObject     e.g. $this->MyLinkedObjectID()
13
     * @param string $methodOrVariable     (OPTIONAL) - e.g. MyFullTitle
14
     */
15
    public function __construct($name, $title, $linkedObject, $methodOrVariable = 'getTitle')
16
    {
17
        $name .= '_CMSEditLink';
18
        if ($linkedObject && $linkedObject->exists() && $linkedObject->hasMethod('CMSEditLink')) {
19
            $this->linkedObject = $linkedObject;
0 ignored issues
show
Documentation Bug introduced by
It seems like $linkedObject of type object<DataObject> is incompatible with the declared type string of property $linkedObject.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20
            if($linkedObject->hasMethod($methodOrVariable)) {
21
                $description = $linkedObject->$methodOrVariable();
22
            } elseif(isset($linkedObject->$methodOrVariable)) {
23
                $description = $linkedObject->$methodOrVariable;
24
            } else {
25
                $description = 'ERROR!';
26
                user_error($methodOrVariable.' does not exist on '.$linkedObject.' (as method or variable)');
27
            }
28
            $content = '<p><a href="'.$linkedObject->CMSEditLink().'">'.$description.'</a></h3>';
29
            $this->dontEscape = true;
30
31
            return parent::__construct($name, $title, $content);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
32
        } else {
33
            return parent::__construct($name, $title);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
34
        }
35
    }
36
37
}
38