Completed
Pull Request — master (#1665)
by Damian
02:09
created

CMSPagesController::ViewState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\CMS\Controllers;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\View\ArrayData;
9
use stdClass;
10
11
class CMSPagesController extends CMSMain {
12
13
	private static $url_segment = 'pages';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
15
	private static $url_rule = '/$Action/$ID/$OtherID';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
16
17
	private static $url_priority = 40;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
19
	private static $menu_title = 'Pages';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
20
21
	private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
22
23
	public function LinkPreview() {
24
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method SilverStripe\CMS\Controllers\CMSMain::LinkPreview of type string.

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...
25
	}
26
27
	public function isCurrentPage(DataObject $record) {
28
		return false;
29
	}
30
31
	public function Breadcrumbs($unlinked = false) {
32
		$items = parent::Breadcrumbs($unlinked);
33
34
		//special case for building the breadcrumbs when calling the listchildren Pages ListView action
35
		if($parentID = $this->getRequest()->getVar('ParentID')) {
36
			$page = SiteTree::get()->byID($parentID);
37
38
			//build a reversed list of the parent tree
39
			$pages = array();
40
			while($page) {
41
				array_unshift($pages, $page); //add to start of array so that array is in reverse order
42
				$page = $page->Parent;
43
			}
44
45
			//turns the title and link of the breadcrumbs into template-friendly variables
46
			$params = array_filter(array(
47
				'view' => $this->getRequest()->getVar('view'),
48
				'q' => $this->getRequest()->getVar('q')
49
			));
50
			foreach($pages as $page) {
51
				$params['ParentID'] = $page->ID;
52
				$item = new stdClass();
53
				$item->Title = $page->Title;
54
				$item->Link = Controller::join_links($this->Link(), '?' . http_build_query($params));
55
				$items->push(new ArrayData($item));
56
			}
57
		}
58
59
		return $items;
60
61
	}
62
}
63