Passed
Pull Request — master (#7)
by
unknown
02:52
created

SiteTreeCanonicalExtension::updateCMSFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 3
eloc 9
c 4
b 0
f 2
nc 3
nop 1
dl 0
loc 11
rs 9.9666
1
<?php
2
namespace DorsetDigital\SilverStripeCanonical;
3
4
use SilverStripe\View\HTML;
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Forms\LiteralField;
8
use SilverStripe\SiteConfig\SiteConfig;
9
use SilverStripe\CMS\Model\SiteTreeExtension;
10
11
class SiteTreeCanonicalExtension extends SiteTreeExtension
12
{
13
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
14
        'CanonicalURL' => 'Text'
15
    ];
16
    
17
    public function updateCMSFields(FieldList $fields) {
18
        if ($MetaToggle = $fields->fieldByName('Root.Main.Metadata')) {
19
            if ($url = $this->getorsetCanonicalURL()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $url is correct as $this->getorsetCanonicalURL() targeting DorsetDigital\SilverStri...:getorsetCanonicalURL() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
20
                $MetaToggle->push($MetaCanonical = TextField::create('CanonicalURL', _t(__CLASS__ . '.LinkOverride',"Override canonical URL")));
21
                $MetaCanonical
22
                    ->setAttribute('placeholder', $this->getorsetCanonicalURL())
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getorsetCanonicalURL() targeting DorsetDigital\SilverStri...:getorsetCanonicalURL() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
23
                    ->setDescription(_t(__CLASS__ . '.LinkOverrideDesc','Only set this if another URL should count as the original (e.g. of reposting a blog post from another source).'));
24
            } else {
25
                $MetaToggle->push($MetaCanonical = LiteralField::create("CanonicalURL", '<p class="form__field-label">' . _t(__CLASS__ . '.LinkFieldPlaceholder','Canonical-URLs needs a Canoinical-Domain in <a href="/admin/settings">SiteConfig</a>') . '</p>'));
26
            }
27
            $MetaCanonical->setRightTitle(_t(__CLASS__ . '.LinkFieldRightTitle','Used to identify the original resource (URL) to prevent being considered as "duplicate content".'));
28
        }
29
    }
30
31
    function getorsetCanonicalURL() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
        $siteConfig = SiteConfig::current_site_config();
33
        if (filter_var($siteConfig->CanonicalDomain, FILTER_VALIDATE_URL)) {
34
            $canonicalBase = trim($siteConfig->CanonicalDomain, '/');
35
36
            // dynamic value
37
            if (method_exists($this->owner, 'CanonicalLink')) {
38
                $link = $this->owner->CanonicalLink();
39
            }
40
41
            // canonical link on Page
42
            if (isset($this->owner->CanonicalURL)) {
43
                $link = $this->owner->CanonicalURL;
44
            }
45
46
            // add canonicalBase if relative URL
47
            if (isset($link)) {
48
                $urlArray = parse_url($link);
49
                if (!isset($urlArray['host']) && !isset($urlArray['scheme'])) {
50
                    $canonicalBase = $urlArray['scheme'] . '://' . $urlArray['host'];
51
                    $link = $canonicalBase . $link;
52
                }
53
            } else {
54
                // default link with base
55
                $link = $canonicalBase . $this->owner->Link();
56
            }
57
58
            return $link;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $link does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
        }
60
    }
61
62
    public function MetaTags(&$tags)
63
    {
64
        if ($canonLink = $this->getorsetCanonicalURL()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $canonLink is correct as $this->getorsetCanonicalURL() targeting DorsetDigital\SilverStri...:getorsetCanonicalURL() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
            $atts = [
66
                'rel' => 'canonical',
67
                'href' => $canonLink
68
            ];
69
            $canonTag = HTML::createTag('link', $atts);
70
71
            $tagsArray = explode(PHP_EOL, $tags);
72
            $tagPattern = 'rel="canonical"';
73
74
            $tagSearch = function($val) use ($tagPattern) {
75
                return (stripos($val, $tagPattern) !== false ? true : false);
76
            };
77
78
            $currentTags = array_filter($tagsArray, $tagSearch);
79
            $cleanedTags = array_diff($tagsArray, $currentTags);
80
81
            $cleanedTags[ ] = $canonTag;
82
83
            $tags = implode(PHP_EOL, $cleanedTags);
84
        }
85
    }
86
}
87