Skip to content

Commit 59564dc

Browse files
authored
Rector up to PHP 7.4 (librenms#18437)
* Rector up to PHP 7.4 Arrow functions are here * Update phpstan baseline for fixed issue * Style fix * Remove unused ??
1 parent e5b4a1b commit 59564dc

File tree

166 files changed

+823
-1454
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+823
-1454
lines changed

LibreNMS/Alert/RunAlerts.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,9 @@ public function runFollowUp()
394394
*/
395395
private function extractIdFieldsForFault($element)
396396
{
397-
return array_filter(array_keys($element), function ($key) {
397+
return array_filter(array_keys($element), fn ($key) =>
398398
// Exclude location_id as it is not relevant for the comparison
399-
return ($key === 'id' || strpos($key, '_id')) !== false && $key !== 'location_id';
400-
});
399+
($key === 'id' || strpos($key, '_id')) !== false && $key !== 'location_id');
401400
}
402401

403402
/**

LibreNMS/Alert/Transport/Browserpush.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ class Browserpush extends Transport
3737

3838
public function deliverAlert(array $alert_data): bool
3939
{
40-
$users = User::when($this->config['user'] ?? 0, function ($query, $user_id) {
41-
return $query->where('user_id', $user_id);
42-
})->get();
40+
$users = User::when($this->config['user'] ?? 0, fn ($query, $user_id) => $query->where('user_id', $user_id))->get();
4341

4442
Notification::send($users, new AlertNotification(
4543
$alert_data['alert_id'],

LibreNMS/Alert/Transport/Pagerduty.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public function deliverAlert(array $alert_data): bool
4242
};
4343

4444
$safe_message = strip_tags($alert_data['msg']) ?: 'Test';
45-
$message = array_filter(explode("\n", $safe_message), function ($value): bool {
46-
return strlen($value) > 0;
47-
});
45+
$message = array_filter(explode("\n", $safe_message), fn ($value): bool => strlen($value) > 0);
4846
$data = [
4947
'routing_key' => $this->config['service_key'],
5048
'event_action' => $event_action,

LibreNMS/Alerting/QueryBuilderFilter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ private function generateTableFilter()
123123
];
124124
} elseif ($type == 'enum') {// format enums as radios
125125
$values = explode(',', substr($column_type, 4));
126-
$values = array_map(function ($val) {
127-
return trim($val, "()' ");
128-
}, $values);
126+
$values = array_map(fn ($val) => trim($val, "()' "), $values);
129127

130128
$this->filter[$field] = [
131129
'id' => $field,

LibreNMS/Alerting/QueryBuilderParser.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,7 @@ public function getGlue($parent, $child)
327327
$shared_keys = array_filter(array_intersect(
328328
$this->schema->getColumns($parent),
329329
$this->schema->getColumns($child)
330-
), function ($table) {
331-
return Str::endsWith($table, '_id');
332-
});
330+
), fn ($table) => Str::endsWith($table, '_id'));
333331

334332
if (count($shared_keys) === 1) {
335333
$shared_key = reset($shared_keys);

LibreNMS/Cache/PermissionsCache.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,8 @@ public function getDeviceGroupPermissionsQuery()
282282
return DB::table('devices_group_perms')
283283
->select('devices_group_perms.user_id', 'device_group_device.device_id')
284284
->join('device_group_device', 'device_group_device.device_group_id', '=', 'devices_group_perms.device_group_id')
285-
->when(! LibrenmsConfig::get('permission.device_group.allow_dynamic'), function ($query) {
286-
return $query
287-
->join('device_groups', 'device_groups.id', '=', 'devices_group_perms.device_group_id')
288-
->where('device_groups.type', 'static');
289-
});
285+
->when(! LibrenmsConfig::get('permission.device_group.allow_dynamic'), fn ($query) => $query
286+
->join('device_groups', 'device_groups.id', '=', 'devices_group_perms.device_group_id')
287+
->where('device_groups.type', 'static'));
290288
}
291289
}

LibreNMS/Component.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ class Component
5151

5252
public function getComponentCount($device_id = null)
5353
{
54-
$counts = \App\Models\Component::query()->when($device_id, function ($query, $device_id) {
55-
return $query->where('device_id', $device_id);
56-
})->selectRaw('type, count(*) as count')->groupBy('type')->pluck('count', 'type');
54+
$counts = \App\Models\Component::query()->when($device_id, fn ($query, $device_id) => $query->where('device_id', $device_id))->selectRaw('type, count(*) as count')->groupBy('type')->pluck('count', 'type');
5755

5856
return $counts->isEmpty() ? false : $counts->all();
5957
}
@@ -115,12 +113,8 @@ public function getComponents($device_id = null, $options = [])
115113
}
116114

117115
// get and format results as expected by receivers
118-
return $query->get()->groupBy('device_id')->map(function ($group) {
119-
return $group->keyBy('id')->map(function ($component) {
120-
return $component->prefs->pluck('value', 'attribute')
121-
->merge($component->only(array_keys($this->reserved)));
122-
});
123-
})->toArray();
116+
return $query->get()->groupBy('device_id')->map(fn ($group) => $group->keyBy('id')->map(fn ($component) => $component->prefs->pluck('value', 'attribute')
117+
->merge($component->only(array_keys($this->reserved)))))->toArray();
124118
}
125119

126120
public function getComponentStatus($device = null)
@@ -233,9 +227,7 @@ public function setComponentPrefs($device_id, $updated)
233227
if ($component->isDirty()) {
234228
// Log the update to the Eventlog.
235229
$message = "Component $component->id has been modified: ";
236-
$message .= collect($component->getDirty())->map(function ($value, $key) {
237-
return "$key => $value";
238-
})->implode(',');
230+
$message .= collect($component->getDirty())->map(fn ($value, $key) => "$key => $value")->implode(',');
239231

240232
// If the Status has changed we need to add a log entry
241233
if ($component->isDirty('status')) {
@@ -248,9 +240,7 @@ public function setComponentPrefs($device_id, $updated)
248240
}
249241

250242
// update preferences
251-
$prefs = collect($updated[$component->id])->filter(function ($value, $attr) {
252-
return ! array_key_exists($attr, $this->reserved);
253-
});
243+
$prefs = collect($updated[$component->id])->filter(fn ($value, $attr) => ! array_key_exists($attr, $this->reserved));
254244

255245
$invalid = $component->prefs->keyBy('id');
256246

LibreNMS/DB/Schema.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public static function getUnexpectedMigrations()
7474
private static function getMigrationFiles()
7575
{
7676
$migrations = collect(glob(base_path('database/migrations/') . '*.php'))
77-
->map(function ($migration_file) {
78-
return basename($migration_file, '.php');
79-
});
77+
->map(fn ($migration_file) => basename($migration_file, '.php'));
8078

8179
return $migrations;
8280
}
@@ -235,9 +233,7 @@ private function findPathRecursive(array $tables, $target, $history = [])
235233
}
236234
}
237235
} else {
238-
$relations = array_keys(array_filter($relationships, function ($related) use ($table) {
239-
return in_array($table, $related);
240-
}));
236+
$relations = array_keys(array_filter($relationships, fn ($related) => in_array($table, $related)));
241237

242238
d_echo("Dead end at $table, searching for relationships " . json_encode($relations) . PHP_EOL);
243239
$recurse = $this->findPathRecursive($relations, $target, array_merge($history, $tables));
@@ -353,9 +349,7 @@ public static function dump($connection = null)
353349
}
354350

355351
$keys = DB::connection($connection)->select(DB::raw("SHOW INDEX FROM `$table`")->getValue(DB::connection($connection)->getQueryGrammar()));
356-
usort($keys, function ($a, $b) {
357-
return $a->Key_name <=> $b->Key_name;
358-
});
352+
usort($keys, fn ($a, $b) => $a->Key_name <=> $b->Key_name);
359353
foreach ($keys as $key) {
360354
$key_name = $key->Key_name;
361355
if (isset($output[$table]['Indexes'][$key_name])) {

LibreNMS/Data/Source/NetSnmpQuery.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,7 @@ private function mibDirectories(): string
506506
}
507507

508508
// remove trailing /, remove empty dirs, and remove duplicates
509-
$dirs = array_unique(array_filter(array_map(function ($dir) {
510-
return rtrim($dir, '/');
511-
}, $dirs)));
509+
$dirs = array_unique(array_filter(array_map(fn ($dir) => rtrim($dir, '/'), $dirs)));
512510

513511
return implode(':', $dirs);
514512
}

LibreNMS/Data/Source/SnmpResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function values(): array
192192
public function pluck(?string $oid = null): array
193193
{
194194
$output = [];
195-
$oid = $oid ?? '[a-zA-Z0-9:.-]+';
195+
$oid ??= '[a-zA-Z0-9:.-]+';
196196
$regex = "/^{$oid}[[.]([\d.[\]]+?)]?$/";
197197

198198
foreach ($this->values() as $key => $value) {

0 commit comments

Comments
 (0)