SWLCustomTexts   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 73
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A initCustomTexts() 0 22 4
A getPropertyCustomText() 0 8 3
1
<?php
2
3
/**
4
 * Class to provide access to custom texts for emails
5
 *
6
 * @since 0.2
7
 *
8
 * @file SWL_CustomTexts.php
9
 * @ingroup SemanticWatchlist
10
 *
11
 * @author Nischay Nahata
12
 */
13
class SWLCustomTexts {
14
15
	/**
16
	 * Group for this CustomTexts
17
	 *
18
	 * @since 0.2
19
	 *
20
	 * @var SWLGroup
21
	 */
22
	private $group;
23
24
	/**
25
	 * Array holding custom texts to be sent in mails
26
	 *
27
	 * @since 0.2
28
	 *
29
	 * @var array or null
30
	 */
31
	private $customTexts = null;
32
33
	public function __construct( SWLGroup $group ) {
34
		$this->group = $group;
35
	}
36
37
	/**
38
	 * Sets an array of CustomTexts by reading from the db
39
	 * for this group.
40
	 *
41
	 * @since 0.2
42
	 */
43
	private function initCustomTexts() {
44
		if( !is_null( $this->customTexts ) ) {
45
			return;
46
		}
47
		$this->customTexts = array();
48
		$dbr = wfGetDB( DB_REPLICA );
49
		$row = $dbr->selectRow(
50
			'swl_groups',
51
			'group_custom_texts',
52
			array( 'group_name' => $this->group->getName() ),
53
			'SWL::initCustomTexts'
54
		);
55
		$set = explode( '|', $row->group_custom_texts );
56
57
		foreach( $set as $elem ) {
58
			$parts = explode( '~', $elem );
59
			if( !array_key_exists( $parts[0], $this->customTexts ) ) {
60
				$this->customTexts[$parts[0]] = array();
61
			}
62
			$this->customTexts[$parts[0]][$parts[1]] = $parts[2];
63
		}
64
	}
65
66
	/**
67
	 * Returns an array of CustomTexts set by the admin in WatchlistConditions
68
	 * for this group and property.
69
	 *
70
	 * @since 0.2
71
	 *
72
	 * @param SMWDIProperty $property
73
	 * @param String $newValue
74
	 *
75
	 * @return String or false
76
	 */
77
	public function getPropertyCustomText( SMWDIProperty $property, $newValue ) {
78
		$this->initCustomTexts();
79
		if( array_key_exists( $property->getLabel(), $this->customTexts ) && array_key_exists( $newValue, $this->customTexts[$property->getLabel()] ) ) {
80
			return $this->customTexts[$property->getLabel()][$newValue];
81
		} else {
82
			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 SWLCustomTexts::getPropertyCustomText 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...
83
		}
84
	}
85
}
86