Completed
Push — master ( 2a63d5...311968 )
by mw
05:46
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
	 * Get a human readable label for this printer.
16
	 *
17
	 * @return string
18
	 */
19
	public function getName() {
20
		return wfMessage( 'srf-printername-pagewidget' )->text();
21
	}
22
23
	/**
24
	 * @see SMWResultPrinter::getResultText
25
	 *
26
	 * @param SMWQueryResult $res
27
	 * @param $outputMode
28
	 *
29
	 * @return string
30
	 */
31
	protected function getResultText( SMWQueryResult $res, $outputMode ) {
32
33
		// Initialize
34
		static $statNr = 0;
35
36
		// Get results from SMWListResultPrinter
37
		$result = parent::getResultText( $res, $outputMode );
38
39
		// Count widgets
40
		$widgetID = 'pagewidget-' . ++$statNr;
41
42
		// Container items
43
		$result = Html::rawElement( 'div', [
44
			'id' => $widgetID,
45
			'class' => 'pagewidget-container',
46
			'data-embedonly' => $this->params['embedonly'],
47
			'style' => 'display:none;'
48
			], $result
49
		);
50
51
		// Placeholder
52
		$processing = SRFUtils::htmlProcessingElement( $this->isHTML );
53
54
		// RL module
55
		SMWOutputs::requireResource( 'ext.srf.pagewidget.carousel' );
56
57
		// Beautify class selector
58
		$class = $this->params['class'] ? ' ' . $this->params['class'] : '';
59
60
		// Wrap results
61
		return Html::rawElement( 'div', [
62
			'class' => 'srf-pagewidget' . $class,
63
			] , $processing . $result
64
		);
65
	}
66
67
	/**
68
	 * @see SMWResultPrinter::getParamDefinitions
69
	 *
70
	 * @since 1.8
71
	 *
72
	 * @param $definitions array of IParamDefinition
73
	 *
74
	 * @return array of IParamDefinition|array
75
	 */
76
	public function getParamDefinitions( array $definitions ) {
77
		$params = parent::getParamDefinitions( $definitions );
78
79
		$params['embedformat'] = [
80
			'message' => 'smw-paramdesc-embedformat',
81
			'default' => 'ul',
82
			'values' => [ 'ul' ],
83
		];
84
85
		$params['class'] = [
86
			'message' => 'srf-paramdesc-class',
87
			'default' => '',
88
		];
89
90
		$params['widget'] = [
91
			'message' => 'srf-paramdesc-widget',
92
			'default' => 'carousel',
93
			'values' =>  [ 'carousel' ],
94
		];
95
96
		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...
97
	}
98
}
99