Byline   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 116
ccs 39
cts 39
cp 1
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 2
	 */
54
55 2
	public function init(array $optionArray = []) : self
56 2
	{
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 2
	 * @return string
70
	 */
71 2
72 2
	public function render(int $date = null, string $author = null) : string
73 2
	{
74
		$output = Module\Hook::trigger('bylineStart');
75
		$dater = new Dater();
76
		$dater->init($date);
77 2
78
		/* html element */
79 2
80 2
		$element = new Html\Element();
81
		$boxElement = $element
82 2
			->copy()
83
			->init('div',
84
			[
85 2
				'class' => $this->_optionArray['className']['box']
86 2
			]);
87
		$textElement = $element
88
			->copy()
89
			->init('span');
90 2
91
		/* collect output */
92 1
93
		if ($author)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
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 1
		{
95 1
			$boxElement->html(
96 1
				$textElement
97
					->copy()
98 1
					->addClass($this->_optionArray['className']['text']['by'])
99 1
					->text($this->_language->get('posted_by')) .
0 ignored issues
show
Bug introduced by Henry Ruhs
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 1
				$textElement
101
					->copy()
102
					->addClass($this->_optionArray['className']['text']['author'])
103 2
					->text($author)
104
			);
105 1
		}
106
		if ($author && $date)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
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 Henry Ruhs
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 1
		{
108 1
			$boxElement->append(
109 1
				$textElement
110
					->copy()
111
					->addClass($this->_optionArray['className']['text']['on'])
112 2
					->text($this->_language->get('on'))
0 ignored issues
show
Bug introduced by Henry Ruhs
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 2
		}
115
		if ($date)
0 ignored issues
show
Bug Best Practice introduced by Henry Ruhs
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 2
		{
117 2
			$boxElement->append(
118 2
				$textElement
119
					->copy()
120 2
					->addClass($this->_optionArray['className']['text']['date'])
121 2
					->text($dater->formatDate()) .
122 2
				$textElement
123
					->copy()
124 2
					->addClass($this->_optionArray['className']['text']['at'])
125 2
					->text($this->_language->get('at')) .
0 ignored issues
show
Bug introduced by Henry Ruhs
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 2
				$textElement
127
					->copy()
128
					->addClass($this->_optionArray['className']['text']['time'])
129 2
					->text($dater->formatTime())
130 2
			);
131
		}
132
		$output .= $boxElement . Module\Hook::trigger('bylineEnd');
133
		return $output;
134
	}
135
}
136