Passed
Push — develop ( 09225f...2749c8 )
by Dylan
02:47
created

AutomatedLinkReportTask::getPageDOM()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 41
Code Lines 27

Duplication

Lines 14
Ratio 34.15 %

Importance

Changes 0
Metric Value
dl 14
loc 41
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 27
nc 18
nop 1
1
<?php
2
3
class AutomatedLinkReportTask extends Controller {
1 ignored issue
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...
4
5
    private   $GlobalSettings;
6
    private   $Links;
7
8
    private static $exclude_classes = array('RedirectorPage', 'VirtualPage');
1 ignored issue
show
Unused Code introduced by
The property $exclude_classes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
10
    public function index() {
11
        if (!Director::is_cli()) return 'Please run this controller in CLI';
12
13
        libxml_use_internal_errors(true);
14
        set_time_limit(600);
15
16
        $this->checkLinks();
17
18
        return array();
19
    }
20
21
    /**
22
     * Check each page on the site and add the data of the links
23
     * found in AutomatedLinkPageResult Objects
24
     *
25
     * @see AutomatedLinkPageResult
26
     * @return ArrayList
27
     */
28
    public function checkLinks() {
29
        $data = ArrayList::create();
30
31
        $run_in_realtime = Config::inst()->get('AutomatedLinkReport', 'run_in_realtime');
32
33
        // Enable this since we will need to render the pages for the report
34
        Config::inst()->update('SSViewer', 'theme_enabled', true);
35
36
        $this->GlobalSettings = GlobalAutoLinkSettings::get_current();
37
        $this->Links          = AutomatedLink::get()->sort('Priority');
38
        $includeInFields      = $this->GlobalSettings->IncludeInFields();
39
        if (!$this->GlobalSettings) {
40
            user_error('Run dev/build before starting to use SEOToolbox');
41
            return $data;
42
        }
43
44
        $exclude = Config::inst()->get($this->class, 'exclude_classes');
45
        $exclude = ($exclude) ? "'".implode("','", $exclude)."'" : '';
46
        foreach (SiteTree::get()->where("ClassName NOT IN($exclude)") as $page) {
47
            if (!$this->checkForPossibleLinks($page, $includeInFields)) continue;
48
            $page = $this->getLinkData($page, $includeInFields);
49
            if (!$page) continue;
50
51
            if (!$run_in_realtime) AutomatedLinkPageResult::add_or_update($page);
52
            $data->push($page);
53
        }
54
55
        if (!$run_in_realtime) AutomatedLinkPageResult::remove_old_data();
56
57
        return $data;
58
    }
59
60
    /**
61
     * Returns all the data on how the provided $page was
62
     * affected by automated links
63
     *
64
     * @param  SiteTree $page
65
     * @param  array $includeIn
66
     *
67
     * @return SiteTree|false $page
68
     */
69
    private function getLinkData(SiteTree $page, array $includeIn) {
70
        // Set a list of all fields that can have autolinks created in them
71
        $page->AutomateableFields = ArrayList::create();
72
73
        foreach ($this->getAllDatabaseFields($page->class) as $field => $type)
74
            if (in_array($field, $includeIn) &&
75
                !$page->AutomateableFields->find('DataField', $field) &&
76
                AutomatedLink::isFieldParsable($page, $field)
77
            ) $page->AutomateableFields->push(DataObject::create(array('DataField' => $field)));
78
79
        // Get data Pre-Automated Links creation
80
        $withLinks = $this->getPageDOM($page, true);
0 ignored issues
show
Unused Code introduced by
The call to AutomatedLinkReportTask::getPageDOM() has too many arguments starting with 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
        if (!$withLinks) return false;
82
83
        $links = $withLinks->getElementsByTagName('a');
84
85
        $page->TotalLinks           = $links->length;
86
        $page->OriginalLinkCount    = $page->TotalLinks;
1 ignored issue
show
Bug introduced by
The property OriginalLinkCount does not seem to exist. Did you mean original?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
87
        $page->LinkCount            = 0;
88
89
        // List all automated links that were created in this $page
90
        $linksUsed = array();
91
        foreach ($this->Links as $autolink)
92
            foreach ($links as $link) {
93
                if ($link->getAttribute('data-id') == $autolink->ID) {
94
                    $linksUsed[$autolink->ID] = $autolink->Phrase;
95
                    $page->OriginalLinkCount--;
1 ignored issue
show
Bug introduced by
The property OriginalLinkCount does not seem to exist. Did you mean original?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
96
                    $page->LinkCount++;
97
                }
98
            }
99
100
        $page->Links = implode(', ', $linksUsed);
101
102
        if ($page->LinkCount < 1) return false;
103
104
        return $page;
105
    }
106
107
    /**
108
     * Return all possible database fields for the
109
     * $class provided
110
     *
111
     * @param String $class
112
     * @return array
113
     */
114
    private function getAllDatabaseFields($class) {
115
        $fields = array();
116 View Code Duplication
        foreach (ClassInfo::ancestry($class, true) as $cls)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
            $fields = array_merge($fields, (array) DataObject::database_fields($cls));
118
119
        return $fields;
120
    }
121
122
    /**
123
     * Returns a rendered version of the page supplied
124
     * creating automated links according inside a DOMDocument
125
     * object or false if anything fails.
126
     *
127
     * @param SiteTree $page
128
     * @return DOMDocument
129
     */
130
    private function getPageDOM(SiteTree $page) {
131
        $controllerClass = $page->class.'_Controller';
132
        if (!class_exists($controllerClass))  $controller = $page->class.'Controller';
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
        if (!class_exists($controllerClass)) return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by AutomatedLinkReportTask::getPageDOM of type DOMDocument.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
134
135
        $controller = $controllerClass::create($page);
136
        $controller->invokeWithExtensions('addAutomatedLinks');
137
138
        // Set the fields with possible links into a single variable that
139
        // will be dumped in the link checker template
140
        $page->AutomateableText = '';
141
        foreach ($page->AutomateableFields as $field) {
142
            $field = $field->DataField;
143
            $page->AutomateableText .= $page->$field;
144
        }
145
146
        $content = mb_convert_encoding(
147
            $controller->renderWith('LinkCheckerTemplate'),
148
            'html-entities',
149
            GlobalAutoLinkSettings::$encoding
150
        );
151
152
        if (!$content) return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by AutomatedLinkReportTask::getPageDOM of type DOMDocument.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
153
154 View Code Duplication
        if( class_exists( 'HTML5_Parser' ) ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            $html5 = HTML5_Parser::parse( $content );
156
            if($html5 instanceof DOMNodeList){
157
                $dom = new DOMDocument();
158
                while($html5->length > 0) {
159
                    $dom->appendChild($html5->item(0));
160
                }
161
            }else{
162
                $dom = $html5;
163
            }
164
        } else{
165
            $dom = new DOMDocument();
166
            $dom->loadHTML( $content );
167
        }
168
169
        return $dom;
170
    }
171
172
    /**
173
     * Checks if the page could have the possibility of automated links
174
     *
175
     * @param SiteTree $page
176
     * @param array $includeIn
177
     *
178
     * @return Boolean
179
     */
180
    private function checkForPossibleLinks(SiteTree $page, array $includeIn) {
181
        foreach ($this->Links as $link)
182
            foreach ($includeIn as $possibleField)
183
                if (isset($page->$possibleField) && preg_match("/\b{$link->Phrase}\b/i", $page->$possibleField)) return true;
184
185
        return false;
186
    }
187
188
}
189