Passed
Push — master ( 0afaad...0eb314 )
by Tim
01:07 queued 13s
created

SiteTreeCanonicalExtension::getorsetCanonicalURL()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 8
eloc 15
c 2
b 0
f 1
nc 13
nop 0
dl 0
loc 29
rs 8.4444
1
<?php
2
3
namespace DorsetDigital\SilverStripeCanonical;
4
5
use SilverStripe\View\HTML;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\SiteConfig\SiteConfig;
10
use SilverStripe\CMS\Model\SiteTreeExtension;
11
12
class SiteTreeCanonicalExtension extends SiteTreeExtension
13
{
14
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
15
        'CanonicalURL' => 'Text'
16
    ];
17
18
    public function updateCMSFields(FieldList $fields)
19
    {
20
        if ($MetaToggle = $fields->fieldByName('Root.Main.Metadata')) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $MetaToggle is correct as $fields->fieldByName('Root.Main.Metadata') targeting SilverStripe\Forms\FieldList::fieldByName() 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...
21
            if ($url = $this->getorsetCanonicalURL()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
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...
22
                $MetaToggle->push($MetaCanonical = TextField::create('CanonicalURL', _t(__CLASS__ . '.LinkOverride', "Override canonical URL")));
23
                $MetaCanonical
24
                    ->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...
25
                    ->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).'));
26
            } else {
27
                $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>'));
28
            }
29
            $MetaCanonical->setRightTitle(_t(__CLASS__ . '.LinkFieldRightTitle', 'Used to identify the original resource (URL) to prevent being considered as "duplicate content".'));
30
        }
31
    }
32
33
    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...
34
    {
35
        $siteConfig = SiteConfig::current_site_config();
36
        if (filter_var($siteConfig->CanonicalDomain, FILTER_VALIDATE_URL)) {
37
            $canonicalBase = trim($siteConfig->CanonicalDomain, '/');
38
39
            // dynamic value
40
            if (method_exists($this->owner, 'CanonicalLink')) {
41
                $link = $this->owner->CanonicalLink();
42
            }
43
44
            // canonical link on Page
45
            if (isset($this->owner->CanonicalURL) && $this->owner->CanonicalURL != null) {
46
                $link = $this->owner->CanonicalURL;
47
            }
48
49
            // add canonicalBase if relative URL
50
            if (isset($link)) {
51
                $urlArray = parse_url($link);
52
                if (!isset($urlArray['host']) && !isset($urlArray['scheme'])) {
53
                    $canonicalBase = $urlArray['scheme'] . '://' . $urlArray['host'];
54
                    $link = $canonicalBase . $link;
55
                }
56
            } else {
57
                // default link with base
58
                $link = $canonicalBase . $this->owner->Link();
59
            }
60
61
            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...
62
        }
63
    }
64
65
    public function MetaTags(&$tags)
66
    {
67
        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...
68
            $atts = [
69
                'rel' => 'canonical',
70
                'href' => $canonLink
71
            ];
72
            $canonTag = HTML::createTag('link', $atts);
73
74
            $tagsArray = explode(PHP_EOL, $tags);
75
            $tagPattern = 'rel="canonical"';
76
77
            $tagSearch = function ($val) use ($tagPattern) {
78
                return (stripos($val, $tagPattern) !== false ? true : false);
79
            };
80
81
            $currentTags = array_filter($tagsArray, $tagSearch);
82
            $cleanedTags = array_diff($tagsArray, $currentTags);
83
84
            $cleanedTags[] = $canonTag;
85
86
            $tags = implode(PHP_EOL, $cleanedTags);
87
        }
88
    }
89
}
90