Completed
Push — master ( 6dc7d8...407c40 )
by Karsten
15:45
created

formats/widget/SRF_PageWidget.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Extends the SMWEmbeddedResultPrinter with a JavaScript carousel widget
5
 *
6
 * @since 1.8
7
 *
8
 * @author mwjames
9
 *
10
 * @ingroup SemanticResultFormats
11
 * @file
12
 */
13
class SRFPageWidget extends SMWEmbeddedResultPrinter {
14
15
	/**
16
	 * Get a human readable label for this printer.
17
	 *
18
	 * @return string
19
	 */
20
	public function getName() {
21
		return wfMessage( 'srf-printername-pagewidget' )->text();
22
	}
23
24
	/**
25
	 * @see SMWResultPrinter::getResultText
26
	 *
27
	 * @param SMWQueryResult $res
28
	 * @param $outputMode
29
	 *
30
	 * @return string
31
	 */
32
	protected function getResultText( SMWQueryResult $res, $outputMode ) {
33
34
		// Initialize
35
		static $statNr = 0;
36
37
		// Get results from SMWListResultPrinter
38
		$result = parent::getResultText( $res, $outputMode );
39
40
		// Count widgets
41
		$widgetID = 'pagewidget-' . ++$statNr;
42
43
		// Container items
44
		$result = Html::rawElement(
45
			'div',
46
			[
47
				'id' => $widgetID,
48
				'class' => 'pagewidget-container',
49
				'data-embedonly' => $this->params['embedonly'],
50
				'style' => 'display:none;'
51
			],
52
			$result
53
		);
54
55
		// Placeholder
56
		$processing = SRFUtils::htmlProcessingElement( $this->isHTML );
57
58
		// RL module
59
		SMWOutputs::requireResource( 'ext.srf.pagewidget.carousel' );
60
61
		// Beautify class selector
62
		$class = $this->params['class'] ? ' ' . $this->params['class'] : '';
63
64
		// Wrap results
65
		return Html::rawElement(
66
			'div',
67
			[
68
				'class' => 'srf-pagewidget' . $class,
69
			],
70
			$processing . $result
71
		);
72
	}
73
74
	/**
75
	 * @see SMWResultPrinter::getParamDefinitions
76
	 *
77
	 * @since 1.8
78
	 *
79
	 * @param $definitions array of IParamDefinition
80
	 *
81
	 * @return array of IParamDefinition|array
82
	 */
83
	public function getParamDefinitions( array $definitions ) {
84
		$params = parent::getParamDefinitions( $definitions );
85
86
		$params['embedformat'] = [
87
			'message' => 'smw-paramdesc-embedformat',
88
			'default' => 'ul',
89
			'values' => [ 'ul' ],
90
		];
91
92
		$params['class'] = [
93
			'message' => 'srf-paramdesc-class',
94
			'default' => '',
95
		];
96
97
		$params['widget'] = [
98
			'message' => 'srf-paramdesc-widget',
99
			'default' => 'carousel',
100
			'values' => [ 'carousel' ],
101
		];
102
103
		return $params;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $params; (array<*,array>) is incompatible with the return type of the parent method SMW\EmbeddedResultPrinter::getParamDefinitions of type array[].

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...
104
	}
105
}
106