PageWithImage   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%
Metric Value
wmc 12
lcom 0
cbo 5
dl 0
loc 75
ccs 0
cts 36
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getOGImage() 0 3 1
A getCMSFields() 0 10 1
A getPortletTitle() 0 3 1
A getPortletImage() 0 3 1
A getPortletCaption() 0 3 1
A getTwitterTitle() 0 3 1
A getTwitterImage() 0 3 1
A getTwitterDescription() 0 3 1
A getImagesForSeo() 0 8 2
A getLinksForSeo() 0 7 2
1
<?php
2
/**
3
* Defines the SupportingProjectPage page type - initial code created by ss generator
4
*/
5
class PageWithImage extends Page implements RenderableAsPortlet, RenderableAsTwitterCard, SeoInformationProvider {
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...
6
7
	// Brief summary to show on facebook and twitter when linking
8
	private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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
		'ImageAttribution' => 'Varchar(255)',
10
		'BriefIntroduction' => 'HTMLText'
11
	);
12
13
14
	static $has_one = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $has_one.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
		'MainImage' => 'Image'
16
	);
17
18
19
	// for rendering thumbnail when linked in facebook
20
	function getOGImage() {
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getOGImage.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
21
		return $this->MainImage();
22
	}
23
24
25
	function getCMSFields() {
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getCMSFields.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
26
		$fields = parent::getCMSFields();
27
		$imagename = _t('PageWithImage.IMAGE', 'Image');
28
		$fields->addFieldToTab( 'Root.'.$imagename, $uf = new UploadField('MainImage', _t('PageWithImage.MAIN_IMAGE', 'Main Image')));
29
		$dirname = strtolower($this->ClassName).'s';
30
		$uf->setFolderName($dirname);
31
		$fields->addFieldToTab('Root.'.$imagename, new TextField('ImageAttribution', 'Image Credit'));
32
		$fields->addFieldToTab('Root', new TextAreaField('BriefIntroduction', 'Brief description of the page for Twitter and Facebook purposes'), 'Content');
33
		return $fields;
34
	}
35
36
37
	public function getPortletTitle() {
38
		return $this->Title;
39
	}
40
41
	public function getPortletImage() {
42
		return $this->MainImage();
43
	}
44
45
	public function getPortletCaption() {
46
		return $this->BriefIntroduction;
47
	}
48
49
	public function getTwitterTitle() {
50
		return $this->Title;
51
	}
52
53
	public function getTwitterImage() {
54
		return $this->MainImage();
55
	}
56
57
	public function getTwitterDescription() {
58
		return Convert::html2raw($this->BriefIntroduction);
59
	}
60
61
	public function getImagesForSeo() {
62
		$result = new ArrayList();
63
		$mainimage = $this->owner->MainImage();
64
		if ($mainimage) {
65
			$result->push($mainimage);
66
		}
67
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (ArrayList) is incompatible with the return type declared by the interface SeoInformationProvider::getImagesForSeo of type DataList.

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...
68
	}
69
70
71
	public function getLinksForSeo() {
72
		$result = false;
73
		if ($this->hasExtension('LinksExtension')) {
74
			$result = $this->Links();
75
		}
76
		return $result;
77
	}
78
79
}
80
81
class PageWithImage_Controller extends Page_Controller {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
82
83
}
84