Skip to content

Commit d4fa654

Browse files
authored
Merge pull request #178 from iMattPro/admin-perms
Code inspection fixes
2 parents 781ce8c + 8cc4bf2 commit d4fa654

13 files changed

+33
-45
lines changed

ad/manager.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function get_ad($ad_id)
6060
$data = $this->db->sql_fetchrow($result);
6161
$this->db->sql_freeresult($result);
6262

63-
return $data !== false ? $data : array();
63+
return $data !== false ? $data : [];
6464
}
6565

6666
/**
@@ -183,8 +183,8 @@ public function increment_ad_clicks($ad_id)
183183
/**
184184
* Insert new advertisement to the database
185185
*
186-
* @param array $data New ad data
187-
* @return int New advertisement ID
186+
* @param array $data New ad data
187+
* @return int New advertisement ID
188188
*/
189189
public function insert_ad($data)
190190
{
@@ -195,7 +195,7 @@ public function insert_ad($data)
195195
// add a row to ads table
196196
$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
197197
$this->db->sql_query($sql);
198-
$ad_id = $this->db->sql_nextid();
198+
$ad_id = (int) $this->db->sql_nextid();
199199

200200
$this->insert_ad_group_data($ad_id, $ad_groups);
201201

@@ -212,7 +212,7 @@ public function insert_ad($data)
212212
public function update_ad($ad_id, $data)
213213
{
214214
// extract ad groups here because it gets filtered in intersect_ad_data()
215-
$ad_groups = isset($data['ad_groups']) ? $data['ad_groups'] : array();
215+
$ad_groups = $data['ad_groups'] ?? [];
216216
$data = $this->intersect_ad_data($data);
217217

218218
$sql = 'UPDATE ' . $this->ads_table . '
@@ -269,7 +269,7 @@ public function remove_ad_owner(array $user_ids)
269269
*/
270270
public function get_ad_locations($ad_id)
271271
{
272-
$ad_locations = array();
272+
$ad_locations = [];
273273

274274
$sql = 'SELECT location_id
275275
FROM ' . $this->ad_locations_table . '
@@ -293,13 +293,13 @@ public function get_ad_locations($ad_id)
293293
*/
294294
public function insert_ad_locations($ad_id, $ad_locations)
295295
{
296-
$sql_ary = array();
296+
$sql_ary = [];
297297
foreach ($ad_locations as $ad_location)
298298
{
299-
$sql_ary[] = array(
299+
$sql_ary[] = [
300300
'ad_id' => $ad_id,
301301
'location_id' => $ad_location,
302-
);
302+
];
303303
}
304304
$this->db->sql_multi_insert($this->ad_locations_table, $sql_ary);
305305
}
@@ -325,7 +325,7 @@ public function delete_ad_locations($ad_id)
325325
*/
326326
public function load_memberships($user_id)
327327
{
328-
$memberships = array();
328+
$memberships = [];
329329
$sql = 'SELECT group_id
330330
FROM ' . USER_GROUP_TABLE . '
331331
WHERE user_id = ' . (int) $user_id . '
@@ -371,7 +371,7 @@ public function load_groups($ad_id)
371371
*/
372372
protected function intersect_ad_data($data)
373373
{
374-
return array_intersect_key($data, array(
374+
return array_intersect_key($data, [
375375
'ad_name' => '',
376376
'ad_note' => '',
377377
'ad_code' => '',
@@ -384,7 +384,7 @@ protected function intersect_ad_data($data)
384384
'ad_owner' => '',
385385
'ad_content_only' => '',
386386
'ad_centering' => '',
387-
));
387+
]);
388388
}
389389

390390
/**
@@ -428,13 +428,13 @@ protected function sql_random()
428428
*/
429429
protected function insert_ad_group_data($ad_id, $ad_groups)
430430
{
431-
$sql_ary = array();
431+
$sql_ary = [];
432432
foreach ($ad_groups as $group)
433433
{
434-
$sql_ary[] = array(
434+
$sql_ary[] = [
435435
'ad_id' => $ad_id,
436436
'group_id' => $group,
437-
);
437+
];
438438
}
439439
$this->db->sql_multi_insert($this->ad_group_table, $sql_ary);
440440
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
}
2525
],
2626
"require": {
27-
"php": ">=5.4",
27+
"php": ">=7.1.3",
2828
"composer/installers": "~1.0"
2929
},
3030
"require-dev": {

controller/helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader,
8282
public function assign_data($data, $errors)
8383
{
8484
$this->assign_locations($data['ad_locations']);
85-
$this->assign_groups((isset($data['ad_id']) ? $data['ad_id'] : 0), (isset($data['ad_groups']) ? $data['ad_groups'] : array()));
85+
$this->assign_groups(($data['ad_id'] ?? 0), ($data['ad_groups'] ?? array()));
8686

8787
$errors = array_map(array($this->language, 'lang'), $errors);
8888
$this->template->assign_vars(array(

ext.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
class ext extends \phpbb\extension\base
1414
{
15-
const DATE_FORMAT = 'Y-m-d';
16-
const MAX_NAME_LENGTH = 255;
17-
const DEFAULT_PRIORITY = 5;
18-
const AD_BLOCK_MODES = [0, 1, 2];
15+
public const DATE_FORMAT = 'Y-m-d';
16+
public const MAX_NAME_LENGTH = 255;
17+
public const DEFAULT_PRIORITY = 5;
18+
public const AD_BLOCK_MODES = [0, 1, 2];
1919

2020
/**
2121
* {@inheritdoc}

tests/ad/delete_ad_locations_test.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ public function test_delete_ad_locations_no_ad()
3333
{
3434
$manager = $this->get_manager();
3535

36-
$sql = 'SELECT COUNT(ad_id) as total_ad_locations
37-
FROM phpbb_ad_locations';
36+
$sql = 'SELECT COUNT(ad_id) as total_ad_locations FROM phpbb_ad_locations';
37+
3838
$result = $this->db->sql_query($sql);
3939
$total_ad_locations = $this->db->sql_fetchfield('total_ad_locations');
4040
$this->db->sql_freeresult($result);
4141

4242
$manager->delete_ad_locations(0);
4343

44-
$sql = 'SELECT COUNT(ad_id) as total_ad_locations
45-
FROM phpbb_ad_locations';
4644
$result = $this->db->sql_query($sql);
4745
self::assertEquals($this->db->sql_fetchfield('total_ad_locations'), $total_ad_locations);
4846
$this->db->sql_freeresult($result);

tests/analyser/analyser_base.php

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class analyser_base extends \phpbb_test_case
2424
/** @var \phpbb\language\language */
2525
protected $lang;
2626

27-
/**
28-
* {@inheritDoc}
29-
*/
3027
protected static function setup_extensions()
3128
{
3229
return array('phpbb/ads');

tests/banner/banner_base.php

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class banner_base extends \phpbb_test_case
2424
/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\files\filespec */
2525
protected $file;
2626

27-
/**
28-
* {@inheritDoc}
29-
*/
3027
protected static function setup_extensions()
3128
{
3229
return array('phpbb/ads');

tests/controller/admin_controller_test.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ public function test_action_edit_no_submit($ad_id)
672672
}
673673
else
674674
{
675-
$ad_locations = !$ad_id ? false : array(
675+
$ad_locations = array(
676676
'above_footer',
677677
'above_header',
678678
);
@@ -955,7 +955,7 @@ public function test_ad_enable($ad_id, $enable, $is_ajax, $err_msg)
955955

956956
$this->manager->expects(self::once())
957957
->method('update_ad')
958-
->willReturn($ad_id ? true : false);
958+
->willReturn((bool) $ad_id);
959959

960960
$this->request->expects(self::once())
961961
->method('is_ajax')
@@ -1015,8 +1015,8 @@ public function test_action_delete($ad_id, $ad_owner, $error, $confirm)
10151015
$this->request
10161016
->expects(self::exactly($confirm ? 2 : 4))
10171017
->method('variable')
1018-
->withConsecutive(...[['action', ''], ['id', 0], ['i', ''], ['mode', '']])
1019-
->willReturnOnConsecutiveCalls(...['delete', $ad_id, '', '']);
1018+
->withConsecutive(['action', ''], ['id', 0], ['i', ''], ['mode', ''])
1019+
->willReturnOnConsecutiveCalls('delete', $ad_id, '', '');
10201020

10211021
if (!$confirm)
10221022
{
@@ -1041,7 +1041,7 @@ public function test_action_delete($ad_id, $ad_owner, $error, $confirm)
10411041
->willReturn(array('id' => $ad_id, 'ad_owner' => $ad_owner, 'ad_name' => ''));
10421042
$this->manager->expects(self::once())
10431043
->method('delete_ad')
1044-
->willReturn($ad_id ? true : false);
1044+
->willReturn((bool) $ad_id);
10451045
$this->manager->expects(($ad_owner ? self::once() : self::never()))
10461046
->method('get_ads_by_owner')
10471047
->with($ad_owner)

tests/controller/admin_input_test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function get_form_data_data()
142142
*/
143143
public function test_get_form_data($valid_form, $data, $ad_owner_expected, $errors)
144144
{
145-
list($ad_name, $ad_note, $ad_code, $ad_enabled, $ad_locations, $ad_start_date, $ad_end_date, $ad_priority, $ad_content_only, $ad_views_limit, $ad_clicks_limit, $ad_owner, $ad_groups, $ad_centering) = $data;
145+
[$ad_name, $ad_note, $ad_code, $ad_enabled, $ad_locations, $ad_start_date, $ad_end_date, $ad_priority, $ad_content_only, $ad_views_limit, $ad_clicks_limit, $ad_owner, $ad_groups, $ad_centering] = $data;
146146

147147
self::$valid_form = $valid_form;
148148
$input_controller = $this->get_input_controller();

tests/controller/helper_test.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function test_assign_locations($ad_locations)
308308
'LOCATION_ID' => 'top_of_page_1',
309309
'LOCATION_DESC' => 'Location #1 desc',
310310
'LOCATION_NAME' => 'Location #1',
311-
'S_SELECTED' => $ad_locations ? in_array('top_of_page_1', $ad_locations) : false,
311+
'S_SELECTED' => $ad_locations && in_array('top_of_page_1', $ad_locations),
312312
]],
313313
['ad_locations', [
314314
'CATEGORY_NAME' => 'CAT_BOTTOM_OF_PAGE',
@@ -317,7 +317,7 @@ public function test_assign_locations($ad_locations)
317317
'LOCATION_ID' => 'bottom_of_page_1',
318318
'LOCATION_DESC' => 'Location #2 desc',
319319
'LOCATION_NAME' => 'Location #2',
320-
'S_SELECTED' => $ad_locations ? in_array('bottom_of_page_1', $ad_locations) : false,
320+
'S_SELECTED' => $ad_locations && in_array('bottom_of_page_1', $ad_locations),
321321
]]
322322
);
323323

tests/controller/visual_demo_test.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public function test_controller($action, $is_ajax, $status_code, $cookie_time)
131131
if (!$is_ajax)
132132
{
133133
// Throws E_WARNING in PHP 8.0+ and E_USER_WARNING in earlier versions
134-
$exceptionName = version_compare(PHP_VERSION, '8.0', '<') ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Warning::class;
135-
$errno = version_compare(PHP_VERSION, '8.0', '<') ? E_USER_WARNING : E_WARNING;
134+
$exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Warning::class;
135+
$errno = PHP_VERSION_ID < 80000 ? E_USER_WARNING : E_WARNING;
136136
$this->expectException($exceptionName);
137137
$this->expectExceptionCode($errno);
138138
}

tests/event/visual_demo_test.php

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function test_visual_demo($in_visual_demo)
3737
$this->user->page['page_name'] = 'viewtopic';
3838

3939
$this->request
40-
->expects(self::any())
4140
->method('is_set')
4241
->withConsecutive(
4342
[$this->config['cookie_name'] . '_phpbb_ads_visual_demo', \phpbb\request\request_interface::COOKIE],

tests/location/location_base.php

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class location_base extends \phpbb_test_case
2424
/** @var \phpbb\request\request|\PHPUnit\Framework\MockObject\MockObject */
2525
protected $request;
2626

27-
/**
28-
* {@inheritDoc}
29-
*/
3027
protected static function setup_extensions()
3128
{
3229
return array('phpbb/ads');

0 commit comments

Comments
 (0)