Completed
Push — master ( d0c173...e44a53 )
by Henry
09:36
created

modules/Archive/Archive.php (1 issue)

Check for mismatching type of a variable.

Bug Minor

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
namespace Redaxscript\Modules\Archive;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Db;
6
use Redaxscript\Html;
7
use Redaxscript\Model;
8
use Redaxscript\Module;
9
use function strtotime;
10
11
/**
12
 * generate a archive tree
13
 *
14
 * @since 2.2.0
15
 *
16
 * @package Redaxscript
17
 * @category Modules
18
 * @author Henry Ruhs
19
 */
20
21
class Archive extends Module\Module
22
{
23
	/**
24
	 * array of the module
25
	 *
26
	 * @var array
27
	 */
28
29
	protected static $_moduleArray =
30
	[
31
		'name' => 'Archive',
32
		'alias' => 'Archive',
33
		'author' => 'Redaxmedia',
34
		'description' => 'Generate a archive tree',
35
		'version' => '4.0.0'
36
	];
37
38
	/**
39
	 * array of the option
40
	 *
41
	 * @var array
42
	 */
43
44
	protected $_optionArray =
45
	[
46
		'className' =>
47
		[
48
			'title' => 'rs-title-content-sub rs-title-archive',
49
			'list' => 'rs-list-default rs-list-archive'
50
		]
51
	];
52
53
	/**
54
	 * render
55
	 *
56
	 * @since 2.2.0
57
	 *
58
	 * @return string
59
	 */
60
61
	public function render() : string
62
	{
63
		$output = null;
64
		$outputItem = null;
65
		$error = null;
66
		$articleModel = new Model\Article();
67
		$dater = new Dater();
68
69
		/* html element */
70
71
		$element = new Html\Element();
72
		$titleElement = $element
73
			->copy()
74
			->init('h3',
75
			[
76
				'class' => $this->_optionArray['className']['title']
77
			]);
78
		$listElement = $element
79
			->copy()
80
			->init('ul',
81
			[
82
				'class' => $this->_optionArray['className']['list']
83
			]);
84
		$itemElement = $element->copy()->init('li');
85
		$linkElement = $element->copy()->init('a');
86
87
		/* query articles */
88
89
		$monthArray = $this->_getMonthArrayByLanguage($this->_registry->get('language'));
0 ignored issues
show
It seems like $this->_registry->get('language') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Modules\Arch...tMonthArrayByLanguage() does only seem to accept null|string, 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...
90
91
		/* process articles */
92
93
		if (!$monthArray)
94
		{
95
			$error = $this->_language->get('article_no') . $this->_language->get('point');
96
		}
97
		else
98
		{
99
			foreach ($monthArray as $key => $articles)
100
			{
101
				foreach ($articles as $value)
102
				{
103
					$outputItem .= $itemElement
104
						->copy()
105
						->html($linkElement
106
							->copy()
107
							->attr('href', $this->_registry->get('parameterRoute') . $articleModel->getRouteById($value->id))
108
							->text($value->title)
109
						);
110
				}
111
112
				/* collect output */
113
114
				$dater->init(strtotime($key));
115
				$month = $dater->getDateTime()->format('n') - 1;
116
				$output .= $titleElement->text($this->_language->get('_month', $month) . ' ' . $dater->getDateTime()->format('Y'));
117
				$output .= $listElement->html($outputItem);
118
				$outputItem = null;
119
			}
120
		}
121
122
		/* handle error */
123
124
		if ($error)
125
		{
126
			$output = $listElement->html(
127
				$itemElement->html($error)
128
			);
129
		}
130
		return $output;
131
	}
132
133
	/**
134
	 * get month array by language
135
	 *
136
	 * @since 3.3.0
137
	 *
138
	 * @param string $language
139
	 *
140
	 * @return array
141
	 */
142
143
	protected function _getMonthArrayByLanguage(string $language = null) : array
144
	{
145
		$monthArray = [];
146
		$dater = new Dater();
147
		$articles = Db::forTablePrefix('articles')
148
			->whereLanguageIs($language)
149
			->whereNull('access')
150
			->where('status', 1)
151
			->orderByDesc('date')
152
			->findMany();
153
154
		/* process article */
155
156
		foreach ($articles as $value)
157
		{
158
			$dater->init($value->date);
159
			$dateKey = $dater->getDateTime()->format('Y-m');
160
			$monthArray[$dateKey][] = $value;
161
		}
162
		return $monthArray;
163
	}
164
}
165