Completed
Push — master ( 14c945...6811c1 )
by Henry
05:37
created

Byline   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 116
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
B render() 0 63 5
1
<?php
2
namespace Redaxscript\View\Helper;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use Redaxscript\View\ViewAbstract;
8
use function array_replace_recursive;
9
10
/**
11
 * helper class to create the byline
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category View
17
 * @author Henry Ruhs
18
 */
19
20
class Byline extends ViewAbstract
21
{
22
	/**
23
	 * options of the byline
24
	 *
25
	 * @var array
26
	 */
27
28
	protected $_optionArray =
29
	[
30
		'className' =>
31
		[
32
			'box' => 'rs-box-byline',
33
			'text' =>
34
			[
35
				'by' => 'rs-text-by',
36
				'author' => 'rs-text-author',
37
				'on' => 'rs-text-on',
38
				'date' => 'rs-text-date',
39
				'at' => 'rs-text-at',
40
				'time' => 'rs-text-time',
41
			]
42
		]
43
	];
44
45
	/**
46
	 * init the class
47
	 *
48
	 * @since 4.0.0
49
	 *
50
	 * @param array $optionArray options of the pagination
51
	 *
52
	 * @return self
53
	 */
54
55
	public function init(array $optionArray = []) : self
56
	{
57
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
58
		return $this;
59
	}
60
61
	/**
62
	 * render the view
63
	 *
64
	 * @since 4.0.0
65
	 *
66
	 * @param int $date timestamp of the date
67
	 * @param string $author name of the author
68
	 *
69
	 * @return string
70
	 */
71
72
	public function render(int $date = null, string $author = null) : string
73
	{
74
		$output = Module\Hook::trigger('bylineStart');
75
		$dater = new Dater();
76
		$dater->init($date);
77
78
		/* html element */
79
80
		$element = new Html\Element();
81
		$boxElement = $element
82
			->copy()
83
			->init('div',
84
			[
85
				'class' => $this->_optionArray['className']['box']
86
			]);
87
		$textElement = $element
88
			->copy()
89
			->init('span');
90
91
		/* collect output */
92
93
		if ($author)
0 ignored issues
show
Bug Best Practice introduced by
The expression $author of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
94
		{
95
			$boxElement->html(
96
				$textElement
97
					->copy()
98
					->addClass($this->_optionArray['className']['text']['by'])
99
					->text($this->_language->get('posted_by')) .
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('posted_by') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
100
				$textElement
101
					->copy()
102
					->addClass($this->_optionArray['className']['text']['author'])
103
					->text($author)
104
			);
105
		}
106
		if ($author && $date)
0 ignored issues
show
Bug Best Practice introduced by
The expression $author of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $date of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
107
		{
108
			$boxElement->append(
109
				$textElement
110
					->copy()
111
					->addClass($this->_optionArray['className']['text']['on'])
112
					->text($this->_language->get('on'))
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('on') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
113
			);
114
		}
115
		if ($date)
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
116
		{
117
			$boxElement->append(
118
				$textElement
119
					->copy()
120
					->addClass($this->_optionArray['className']['text']['date'])
121
					->text($dater->formatDate()) .
122
				$textElement
123
					->copy()
124
					->addClass($this->_optionArray['className']['text']['at'])
125
					->text($this->_language->get('at')) .
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('at') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
126
				$textElement
127
					->copy()
128
					->addClass($this->_optionArray['className']['text']['time'])
129
					->text($dater->formatTime())
130
			);
131
		}
132
		$output .= $boxElement . Module\Hook::trigger('bylineEnd');
133
		return $output;
134
	}
135
}
136