Skip to content

Commit 2b11e27

Browse files
Fix phpstan errors, and some refactoring
1 parent 025b739 commit 2b11e27

File tree

17 files changed

+162
-111
lines changed

17 files changed

+162
-111
lines changed

src/administrator/components/com_weblinks/script.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,20 @@ public function install($parent)
113113
// Set the location in the tree
114114
$category->setLocation(1, 'last-child');
115115

116-
// Check to make sure our data is valid
117-
if (!$category->check()) {
118-
Factory::getApplication()->enqueueMessage(Text::sprintf('COM_WEBLINKS_ERROR_INSTALL_CATEGORY', $category->getError()));
116+
try {
117+
// Check to make sure our data is valid (throws exception on failure)
118+
$category->check();
119+
} catch (\Exception $e) {
120+
Factory::getApplication()->enqueueMessage(Text::sprintf('COM_WEBLINKS_ERROR_INSTALL_CATEGORY', $e->getMessage()), 'error');
119121

120122
return;
121123
}
122124

123-
// Now store the category
124-
if (!$category->store(true)) {
125-
Factory::getApplication()->enqueueMessage(Text::sprintf('COM_WEBLINKS_ERROR_INSTALL_CATEGORY', $category->getError()));
125+
try {
126+
// Now store the category (throws exception on failure)
127+
$category->store(true);
128+
} catch (\Exception $e) {
129+
Factory::getApplication()->enqueueMessage(Text::sprintf('COM_WEBLINKS_ERROR_INSTALL_CATEGORY', $e->getMessage()), 'error');
126130

127131
return;
128132
}

src/administrator/components/com_weblinks/src/Service/HTML/Icon.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Joomla\CMS\Layout\LayoutHelper;
2121
use Joomla\CMS\Router\Route;
2222
use Joomla\CMS\Uri\Uri;
23+
use Joomla\CMS\User\UserFactoryInterface;
2324
use Joomla\Component\Weblinks\Site\Helper\RouteHelper;
2425
use Joomla\Registry\Registry;
2526

@@ -115,7 +116,8 @@ public function edit($weblink, $params, $attribs = [], $legacy = false)
115116
&& $weblink->checked_out
116117
&& $weblink->checked_out !== $user->get('id')
117118
) {
118-
$checkoutUser = Factory::getUser($weblink->checked_out);
119+
$userFactory = Factory::getContainer()->get(UserFactoryInterface::class);
120+
$checkoutUser = $userFactory->loadUserById($weblink->checked_out);
119121
$date = HTMLHelper::_('date', $weblink->checked_out_time);
120122
$tooltip = Text::sprintf('COM_WEBLINKS_CHECKED_OUT_BY', $checkoutUser->name)
121123
. ' <br> ' . $date;

src/administrator/components/com_weblinks/src/Table/WeblinkTable.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ public function store($updateNulls = true)
126126
$table->load(['language' => $this->language, 'alias' => $this->alias, 'catid' => (int) $this->catid])
127127
&& ($table->id != $this->id || $this->id == 0)
128128
) {
129-
$this->setError(Text::_('COM_WEBLINKS_ERROR_UNIQUE_ALIAS'));
130-
return false;
129+
throw new \Exception(Text::_('COM_WEBLINKS_ERROR_UNIQUE_ALIAS'));
131130
}
132131

133132
// Convert IDN urls to punycode
@@ -145,14 +144,12 @@ public function store($updateNulls = true)
145144
public function check()
146145
{
147146
if (InputFilter::checkAttribute(['href', $this->url])) {
148-
$this->setError(Text::_('COM_WEBLINKS_ERR_TABLES_PROVIDE_URL'));
149-
return false;
147+
throw new \Exception(Text::_('COM_WEBLINKS_ERR_TABLES_PROVIDE_URL'));
150148
}
151149

152150
// Check for valid name
153151
if (trim($this->title) === '') {
154-
$this->setError(Text::_('COM_WEBLINKS_ERR_TABLES_TITLE'));
155-
return false;
152+
throw new \Exception(Text::_('COM_WEBLINKS_ERR_TABLES_TITLE'));
156153
}
157154

158155
// Check for existing name
@@ -169,8 +166,7 @@ public function check()
169166
$db->setQuery($query);
170167
$xid = (int) $db->loadResult();
171168
if ($xid && $xid != (int) $this->id) {
172-
$this->setError(Text::_('COM_WEBLINKS_ERR_TABLES_NAME'));
173-
return false;
169+
throw new \Exception(Text::_('COM_WEBLINKS_ERR_TABLES_NAME'));
174170
}
175171

176172
if (empty($this->alias)) {
@@ -184,8 +180,7 @@ public function check()
184180

185181
// Check the publish down date is not earlier than publish up.
186182
if ((int) $this->publish_down > 0 && $this->publish_down < $this->publish_up) {
187-
$this->setError(Text::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));
188-
return false;
183+
throw new \Exception(Text::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));
189184
}
190185

191186
/*

src/administrator/components/com_weblinks/src/View/Weblink/HtmlView.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
2424
use Joomla\CMS\Toolbar\ToolbarHelper;
2525
use Joomla\Component\Weblinks\Administrator\Model\WeblinkModel;
26+
use Joomla\Registry\Registry;
2627

2728
/**
2829
* View to edit a weblink.
@@ -48,7 +49,7 @@ class HtmlView extends BaseHtmlView
4849
/**
4950
* The model state
5051
*
51-
* @var \Joomla\CMS\Object\CMSObject
52+
* @var Registry
5253
*/
5354
protected $state;
5455

@@ -67,11 +68,6 @@ public function display($tpl = null)
6768
$this->item = $model->getItem();
6869
$this->form = $model->getForm();
6970

70-
// Check for errors.
71-
if (\count($errors = $model->getErrors())) {
72-
throw new GenericDataException(implode("\n", $errors), 500);
73-
}
74-
7571
// If we are forcing a language in modal (used for associations).
7672
if ($this->getLayout() === 'modal' && $forcedLanguage = Factory::getApplication()->getInput()->get('forcedLanguage', '', 'cmd')) {
7773
// Set the language field to the forcedLanguage and disable changing it.
@@ -90,6 +86,18 @@ public function display($tpl = null)
9086
parent::display($tpl);
9187
}
9288

89+
/**
90+
* Get the item ID.
91+
*
92+
* @return int
93+
*
94+
* @since __DEPLOY_VERSION__
95+
*/
96+
public function getItemId(): int
97+
{
98+
return (int) $this->item->id;
99+
}
100+
93101
/**
94102
* Add the page title and toolbar.
95103
*

src/administrator/components/com_weblinks/src/View/Weblinks/HtmlView.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
use Joomla\CMS\Language\Text;
2121
use Joomla\CMS\MVC\View\GenericDataException;
2222
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
23-
use Joomla\CMS\Toolbar\Toolbar;
23+
use Joomla\CMS\Pagination\Pagination;
2424
use Joomla\CMS\Toolbar\ToolbarHelper;
2525
use Joomla\Component\Weblinks\Administrator\Model\WeblinksModel;
26+
use Joomla\Registry\Registry;
2627

2728
/**
2829
* View class for a list of weblinks.
@@ -48,7 +49,7 @@ class HtmlView extends BaseHtmlView
4849
/**
4950
* The model state
5051
*
51-
* @var \Joomla\CMS\Object\CMSObject
52+
* @var Registry
5253
*/
5354
protected $state;
5455

@@ -92,12 +93,7 @@ public function display($tpl = null)
9293
$this->filterForm = $model->getFilterForm();
9394
$this->activeFilters = $model->getActiveFilters();
9495

95-
// Check for errors.
96-
if (\count($errors = $model->getErrors())) {
97-
throw new GenericDataException(implode("\n", $errors), 500);
98-
}
99-
100-
if (!\count($this->items) && $this->isEmptyState = $this->get('IsEmptyState')) {
96+
if (!\count($this->items) && $this->isEmptyState = $model->getIsEmptyState()) {
10197
$this->setLayout('emptystate');
10298
}
10399

@@ -123,6 +119,42 @@ public function display($tpl = null)
123119
parent::display($tpl);
124120
}
125121

122+
/**
123+
* Get the items.
124+
*
125+
* @return array
126+
*
127+
* @since __DEPLOY_VERSION__
128+
*/
129+
public function getItems(): array
130+
{
131+
return $this->items;
132+
}
133+
134+
/**
135+
* Get the pagination object.
136+
*
137+
* @return Pagination
138+
*
139+
* @since __DEPLOY_VERSION__
140+
*/
141+
public function getPagination(): Pagination
142+
{
143+
return $this->pagination;
144+
}
145+
146+
/**
147+
* Get the model state.
148+
*
149+
* @return Registry
150+
*
151+
* @since __DEPLOY_VERSION__
152+
*/
153+
public function getState(): Registry
154+
{
155+
return $this->state;
156+
}
157+
126158
/**
127159
* Add the page title and toolbar.
128160
*
@@ -136,7 +168,7 @@ protected function addToolbar()
136168
$user = $this->getCurrentUser();
137169

138170
// Get the toolbar object instance
139-
$toolbar = Toolbar::getInstance('toolbar');
171+
$toolbar = $this->getDocument()->getToolbar();
140172

141173
ToolbarHelper::title(Text::_('COM_WEBLINKS_MANAGER_WEBLINKS'), 'link weblinks');
142174

src/administrator/components/com_weblinks/tmpl/weblink/modal.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616

1717
/** @var \Joomla\Component\Weblinks\Administrator\View\Weblink\HtmlView $this */
1818

19-
HTMLHelper::_('bootstrap.tooltip', '.hasTooltip', array('placement' => 'bottom'));
19+
HTMLHelper::_('bootstrap.tooltip', '.hasTooltip', ['placement' => 'bottom']);
20+
2021
// @deprecated 4.0 the function parameter, the inline js and the buttons are not needed since 3.7.0.
21-
$function = Factory::getApplication()->getInput()->getCmd('function', 'jEditWeblink_' . (int) $this->item->id);
22+
$function = Factory::getApplication()->getInput()->getCmd('function', 'jEditWeblink_' . $this->getItemId());
23+
2224
// Function to update input title when changed
23-
$this->getDocument()->addScriptDeclaration('
25+
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
26+
$script = <<<JS
2427
function jEditWeblinkModal() {
2528
if (window.parent && document.formvalidator.isValid(document.getElementById("weblink-form"))) {
26-
return window.parent.' . $this->escape($function) . '(document.getElementById("jform_title").value);
29+
return window.parent.{$this->escape($function)}(document.getElementById("jform_title").value);
2730
}
2831
}
29-
');
32+
JS;
33+
$wa->addInlineScript($script);
3034
?>
3135
<button id="applyBtn" type="button" class="hidden" onclick="Joomla.submitbutton('weblink.apply'); jEditWeblinkModal();"></button>
3236
<button id="saveBtn" type="button" class="hidden" onclick="Joomla.submitbutton('weblink.save'); jEditWeblinkModal();"></button>

src/administrator/components/com_weblinks/tmpl/weblinks/default.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
HTMLHelper::_('behavior.multiselect');
2525
$user = Factory::getApplication()->getIdentity();
2626
$userId = $user->get('id');
27-
$listOrder = $this->escape($this->state->get('list.ordering'));
28-
$listDirn = $this->escape($this->state->get('list.direction'));
27+
$listOrder = $this->escape($this->getState()->get('list.ordering'));
28+
$listDirn = $this->escape($this->getState()->get('list.direction'));
2929
$saveOrder = $listOrder == 'a.ordering';
3030
$assoc = Associations::isEnabled();
31-
if ($saveOrder && !empty($this->items)) {
31+
if ($saveOrder && !empty($this->getItems())) {
3232
$saveOrderingUrl = 'index.php?option=com_weblinks&task=weblinks.saveOrderAjax&tmpl=component';
3333
HTMLHelper::_('draggablelist.draggable');
3434
}
@@ -41,7 +41,7 @@
4141
// Search tools bar
4242
echo LayoutHelper::render('joomla.searchtools.default', ['view' => $this]);
4343
?>
44-
<?php if (empty($this->items)) :
44+
<?php if (empty($this->getItems())) :
4545
?>
4646
<div class="alert alert-info">
4747
<span class="icon-info-circle" aria-hidden="true"></span><span class="visually-hidden"><?php echo Text::_('INFO'); ?></span>
@@ -98,7 +98,7 @@
9898
<tbody <?php if ($saveOrder) :
9999
?> class="js-draggable" data-url="<?php echo $saveOrderingUrl; ?>" data-direction="<?php echo strtolower($listDirn); ?>" data-nested="true"<?php
100100
endif; ?>>
101-
<?php foreach ($this->items as $i => $item) :
101+
<?php foreach ($this->getItems() as $i => $item) :
102102
?>
103103
<?php $item->cat_link = Route::_('index.php?option=com_categories&extension=com_weblinks&task=edit&type=other&cid[]=' . $item->catid); ?>
104104
<?php $canCreate = $user->authorise('core.create', 'com_weblinks.category.' . $item->catid); ?>
@@ -191,7 +191,7 @@
191191
</table>
192192

193193
<?php // Load the pagination. ?>
194-
<?php echo $this->pagination->getListFooter(); ?>
194+
<?php echo $this->getPagination()->getListFooter(); ?>
195195

196196
<?php // Load the batch processing form. ?>
197197
<?php if (

src/components/com_weblinks/src/Model/CategoriesModel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public function getItems()
116116
$options['access'] = $this->getState('filter.access');
117117
$options['published'] = $this->getState('filter.published');
118118
$options['countItems'] = $params->get('show_cat_num_links', 1) || !$params->get('show_empty_categories_cat', 0);
119-
$categories = Categories::getInstance('Weblinks', $options);
119+
$component = Factory::getApplication()->bootComponent('com_weblinks');
120+
$categories = $component->getCategory($options);
120121
$this->_parent = $categories->get($this->getState('filter.parentId', 'root'));
121122
if (\is_object($this->_parent)) {
122123
$this->_items = $this->_parent->getChildren();

src/components/com_weblinks/src/Model/CategoryModel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected function getListQuery()
202202

203203
// Filter by language
204204
if ($this->getState('filter.language')) {
205-
$query->whereIn($db->quoteName('a.language'), [Factory::getLanguage()->getTag(), '*'], ParameterType::STRING);
205+
$query->whereIn($db->quoteName('a.language'), [Factory::getApplication()->getLanguage()->getTag(), '*'], ParameterType::STRING);
206206
}
207207

208208
// Filter by search in title
@@ -308,7 +308,8 @@ public function getCategory()
308308
$options['countItems'] = $params->get('show_cat_num_links_cat', 1)
309309
|| $params->get('show_empty_categories', 0);
310310

311-
$categories = Categories::getInstance('Weblinks', $options);
311+
$component = Factory::getApplication()->bootComponent('com_weblinks');
312+
$categories = $component->getCategory($options);
312313
$this->_item = $categories->get($this->getState('category.id', 'root'));
313314

314315
if (\is_object($this->_item)) {

src/components/com_weblinks/src/Model/WeblinkModel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function getItem($pk = null)
113113

114114
// Filter by language
115115
if ($this->getState('filter.language')) {
116-
$query->whereIn($db->quoteName('a.language'), [Factory::getLanguage()->getTag(), '*'], ParameterType::STRING);
116+
$query->whereIn($db->quoteName('a.language'), [Factory::getApplication()->getLanguage()->getTag(), '*'], ParameterType::STRING);
117117
}
118118

119119
// Join over the categories to get parent category titles
@@ -174,6 +174,7 @@ public function getItem($pk = null)
174174

175175
$this->_item[$pk] = $data;
176176
} catch (\Exception $e) {
177+
// @phpstan-ignore-next-line ItemModel::setError is designed to handle caught exceptions
177178
$this->setError($e);
178179
$this->_item[$pk] = false;
179180
}

0 commit comments

Comments
 (0)