Skip to content

Commit c009bae

Browse files
authored
Merge pull request #19593 from grokability/moar-phpstan
Fixed phpstan “will always evaluate to true” errors
2 parents 746b775 + c59cdbe commit c009bae

21 files changed

Lines changed: 149 additions & 211 deletions

app/Console/Commands/ValidateAssets.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\Asset;
66
use Illuminate\Console\Command;
7-
use Illuminate\Support\MessageBag;
87

98
class ValidateAssets extends Command
109
{
@@ -69,18 +68,7 @@ public function handle()
6968

7069
private function formatValidationErrors(Asset $asset): string
7170
{
72-
$errors = $asset->getErrors();
73-
$messages = [];
74-
75-
if ($errors instanceof MessageBag) {
76-
$messages = $errors->all();
77-
} elseif (is_array($errors)) {
78-
$messages = $errors;
79-
} else {
80-
$messages = [(string) $errors];
81-
}
82-
83-
$prefixedMessages = collect($messages)
71+
$prefixedMessages = collect($asset->getErrors()->all())
8472
->map(fn ($message) => trim((string) $message))
8573
->filter()
8674
->map(fn (string $message) => str_starts_with($message, '') ? $message : ''.$message)

app/Exceptions/Handler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function reportOrRethrow(Throwable $e): void
8181
// escape past the friendly user-facing error.
8282
if (
8383
config('app.debug')
84-
&& !app()->environment('production')
84+
&& ! app()->environment('production')
8585
&& $e instanceof \Error
8686
) {
8787
throw $e;
@@ -182,7 +182,7 @@ public function render($request, Throwable $e)
182182
// This is traaaaash but it handles models that are not found while using route model binding :(
183183
// The only alternative is to set that at *each* route, which is crazypants
184184
if ($e instanceof ModelNotFoundException) {
185-
$ids = method_exists($e, 'getIds') ? $e->getIds() : [];
185+
$ids = $e->getIds();
186186

187187
if (in_array('bulkedit', $ids, true)) {
188188
$error_array = session()->get('bulk_asset_errors');
@@ -219,7 +219,7 @@ public function render($request, Throwable $e)
219219
// Normalize the space-separated derived name to underscore
220220
// so compound class names (AssetModel -> "asset model" -> "asset_model")
221221
// resolve to keys that actually exist in general.php.
222-
$translationKey = 'general.' . str_replace(' ', '_', $model_name);
222+
$translationKey = 'general.'.str_replace(' ', '_', $model_name);
223223
$translatedName = Lang::has($translationKey) ? trans($translationKey) : $model_name;
224224

225225
return redirect()

app/Http/Controllers/Assets/BulkAssetsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public function update(Request $request): RedirectResponse
266266
// is Referer-derived and would need its own sanitize step.
267267
$bulk_back_url = Helper::sameOriginUrl($request->session()->pull('bulk_back_url')) ?? route('hardware.index');
268268

269-
$custom_field_columns = CustomField::all()->pluck('db_column')->toArray();
269+
$custom_field_columns = CustomField::pluck('db_column')->toArray();
270270

271271
// find custom field input attributes that start with 'null_'
272272
$null_custom_fields_inputs = array_filter($request->all(), function ($key) {

app/Http/Transformers/ActionlogsTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function transformActionlog(Actionlog $actionlog, $settings = null)
217217
: null,
218218
'note' => ($actionlog->note) ? Helper::parseEscapedMarkedownInline($actionlog->note) : null,
219219
'signature_file' => (($actionlog->accept_signature) && Storage::exists('private_uploads/signatures/'.$actionlog->accept_signature)) ? route('log.signature.view', ['filename' => $actionlog->accept_signature]) : null,
220-
'log_meta' => ((isset($clean_meta)) && (is_array($clean_meta))) ? $clean_meta : null,
220+
'log_meta' => $clean_meta ?? null,
221221
'remote_ip' => e($actionlog->remote_ip) ?? null,
222222
'user_agent' => e($actionlog->user_agent) ?? null,
223223
'action_source' => ($actionlog->action_source) ?? null,

app/Http/Transformers/UploadedFilesTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function transformFile(Actionlog $file)
2727
$snipeModel = $file->item_type;
2828
$item = null;
2929

30-
if (is_string($snipeModel) && class_exists($snipeModel)) {
30+
if (class_exists($snipeModel)) {
3131
$itemQuery = $snipeModel::query();
3232

3333
if (in_array(SoftDeletes::class, class_uses_recursive($snipeModel), true)) {

app/Livewire/LdapSettings.php

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,22 +1447,78 @@ public function updated(string $property): void
14471447
return;
14481448
}
14491449

1450+
// Every string-typed prop that participates in the LDAP
1451+
// handshake or a downstream test surface, grouped by wizard
1452+
// step. Shared between the trim-on-assignment and the
1453+
// clearTestResult() blocks below because both need the same
1454+
// "user typed into a field the test depends on" signal.
1455+
$connectionStringProps = [
1456+
// Step 1: Connection
1457+
'ldap_server',
1458+
'ldap_client_tls_key',
1459+
'ldap_client_tls_cert',
1460+
// Step 2: Authenticate + Scope
1461+
'ldap_uname',
1462+
'ldap_pword',
1463+
'ldap_basedn',
1464+
'ldap_filter',
1465+
'ldap_auth_filter_query',
1466+
// Step 3: Attribute Mapping
1467+
'ldap_username_field',
1468+
'ldap_fname_field',
1469+
'ldap_lname_field',
1470+
'ldap_display_name',
1471+
'ldap_email',
1472+
'ldap_emp_num',
1473+
'ldap_phone_field',
1474+
'ldap_mobile',
1475+
'ldap_jobtitle',
1476+
'ldap_manager',
1477+
'ldap_dept',
1478+
'ldap_address',
1479+
'ldap_city',
1480+
'ldap_state',
1481+
'ldap_zip',
1482+
'ldap_country',
1483+
'ldap_location',
1484+
'ldap_active_flag',
1485+
// Step 4: Sync + Defaults
1486+
'custom_forgot_pass_url',
1487+
// Not persisted; step-4 Look Up preview input
1488+
'test_sample_username',
1489+
];
1490+
14501491
// Trim string values on assignment so pasted-with-whitespace
14511492
// inputs get normalized both in the visible field and in the
14521493
// saved config. Without this a leading space on ldap_server
14531494
// silently fails starts_with:ldap://, and a trailing newline in
14541495
// a bind username produces a mysterious LDAP-side rejection at
1455-
// auth time. Textareas (TLS key/cert) tolerate the trim because
1456-
// PEM parsers accept both terminating-newline and no-terminating-
1457-
// newline forms.
1458-
if (in_array($property, ['ldap_server', 'ad_domain', 'ldap_client_tls_key', 'ldap_client_tls_cert', 'ldap_uname', 'ldap_pword', 'ldap_basedn', 'ldap_filter', 'ldap_auth_filter_query', 'ldap_username_field', 'ldap_fname_field', 'ldap_lname_field', 'ldap_display_name', 'ldap_email', 'ldap_emp_num', 'ldap_phone_field', 'ldap_mobile', 'ldap_jobtitle', 'ldap_manager', 'ldap_dept', 'ldap_address', 'ldap_city', 'ldap_state', 'ldap_zip', 'ldap_country', 'ldap_location', 'ldap_active_flag', 'custom_forgot_pass_url', 'test_sample_username'], true) && is_string($this->{$property})) {
1496+
// auth time. Textareas (TLS key / cert) tolerate the trim
1497+
// because PEM parsers accept both terminating-newline and
1498+
// no-terminating-newline forms. ad_domain gets trimmed too but
1499+
// does NOT invalidate the test below; it's not part of the
1500+
// ldap_bind() handshake, only post-connection scoping.
1501+
if (in_array($property, [...$connectionStringProps, 'ad_domain'], true)) {
14591502
$this->{$property} = trim($this->{$property});
14601503
}
14611504

14621505
// Any edit to a connection-shape field invalidates the prior
14631506
// network test result (a stale "connected" indicator sitting
14641507
// under a since-edited server URL would be actively misleading).
1465-
if (in_array($property, ['ldap_server', 'ldap_tls', 'ldap_server_cert_ignore', 'ldap_client_tls_key', 'ldap_client_tls_cert', 'ldap_uname', 'ldap_pword', 'ldap_basedn', 'ldap_filter', 'ldap_auth_filter_query', 'ldap_username_field', 'ldap_fname_field', 'ldap_lname_field', 'ldap_display_name', 'ldap_email', 'ldap_emp_num', 'ldap_phone_field', 'ldap_mobile', 'ldap_jobtitle', 'ldap_manager', 'ldap_dept', 'ldap_address', 'ldap_city', 'ldap_state', 'ldap_zip', 'ldap_country', 'ldap_location', 'ldap_active_flag', 'ldap_invert_active_flag', 'ldap_enabled', 'ldap_pw_sync', 'ldap_default_group', 'custom_forgot_pass_url', 'test_sample_username'], true)) {
1508+
// Every connection-participating string prop plus the non-string
1509+
// toggles / IDs that also affect the handshake or downstream
1510+
// test surface.
1511+
$testInvalidatingProps = [
1512+
...$connectionStringProps,
1513+
'ldap_tls',
1514+
'ldap_server_cert_ignore',
1515+
'ldap_invert_active_flag',
1516+
'ldap_enabled',
1517+
'ldap_pw_sync',
1518+
'ldap_default_group',
1519+
];
1520+
1521+
if (in_array($property, $testInvalidatingProps, true)) {
14661522
$this->clearTestResult();
14671523
// Also clear the step-4 preview table since it references the
14681524
// old values.
@@ -1473,11 +1529,31 @@ public function updated(string $property): void
14731529
// Clear this field's inline validation error so re-editing the
14741530
// field un-greys the Save button (which keys off canAdvance,
14751531
// which checks the error bag).
1476-
if (in_array($property, ['ldap_server', 'ldap_client_tls_key', 'ldap_client_tls_cert', 'ad_domain', 'ldap_uname', 'ldap_pword', 'ldap_basedn', 'ldap_filter', 'ldap_auth_filter_query', 'ldap_username_field', 'ldap_fname_field', 'custom_forgot_pass_url', 'test_sample_username'], true)) {
1532+
if (in_array($property, [
1533+
'ldap_server',
1534+
'ldap_client_tls_key',
1535+
'ldap_client_tls_cert',
1536+
'ad_domain',
1537+
'ldap_uname',
1538+
'ldap_pword',
1539+
'ldap_basedn',
1540+
'ldap_filter',
1541+
'ldap_auth_filter_query',
1542+
'ldap_username_field',
1543+
'ldap_fname_field',
1544+
'custom_forgot_pass_url',
1545+
'test_sample_username'
1546+
], true)) {
14771547
$this->resetValidation($property);
14781548
}
14791549

1480-
if (! in_array($property, ['currentStep', 'highestStepReached', 'dirty', 'testStatus', 'testMessage'], true)) {
1550+
if (!in_array($property, [
1551+
'currentStep',
1552+
'highestStepReached',
1553+
'dirty',
1554+
'testStatus',
1555+
'testMessage'
1556+
], true)) {
14811557
$this->dirty = true;
14821558
}
14831559
}

app/Models/Accessory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Presenters\Presentable;
1515
use Illuminate\Database\Eloquent\Factories\HasFactory;
1616
use Illuminate\Database\Eloquent\Relations\BelongsTo;
17+
use Illuminate\Database\Eloquent\Relations\HasMany;
1718
use Illuminate\Database\Eloquent\Relations\Relation;
1819
use Illuminate\Database\Eloquent\SoftDeletes;
1920
use Illuminate\Database\Query\Builder;
@@ -273,7 +274,7 @@ public function assetlog()
273274
* @since v5.0.0
274275
* @see checkedout()
275276
*/
276-
public function lastCheckout()
277+
public function lastCheckout(): HasMany
277278
{
278279
return $this->assetlog()->where('action_type', '=', 'checkout')->take(1);
279280
}

app/Models/AccessoryCheckout.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Builder;
88
use Illuminate\Database\Eloquent\Factories\HasFactory;
99
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Database\Eloquent\Relations\HasMany;
1011
use Illuminate\Database\Eloquent\Relations\Relation;
1112

1213
/**
@@ -44,7 +45,7 @@ public function accessory()
4445
return $this->belongsTo(Accessory::class);
4546
}
4647

47-
public function accessories()
48+
public function accessories(): HasMany
4849
{
4950
return $this->hasMany(Accessory::class, 'id', 'accessory_id');
5051
}

app/Models/Company.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Presenters\CompanyPresenter;
1111
use App\Presenters\Presentable;
1212
use Illuminate\Database\Eloquent\Factories\HasFactory;
13+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1314
use Illuminate\Database\Eloquent\SoftDeletes;
1415
use Illuminate\Support\Facades\Auth;
1516
use Illuminate\Support\Facades\DB;
@@ -499,7 +500,7 @@ public function users()
499500
* on the index page. Hierarchy is metadata about a row the user already sees,
500501
* not an access decision, so unscoping here is semantically correct too.
501502
*/
502-
public function parent()
503+
public function parent(): BelongsTo
503504
{
504505
return $this->belongsTo(self::class, 'parent_id')->withoutGlobalScopes();
505506
}

app/Models/License.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Carbon\Carbon;
1616
use Illuminate\Database\Eloquent\Casts\Attribute;
1717
use Illuminate\Database\Eloquent\Factories\HasFactory;
18+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1819
use Illuminate\Database\Eloquent\Relations\Relation;
1920
use Illuminate\Database\Eloquent\SoftDeletes;
2021
use Illuminate\Database\Query\Builder;
@@ -498,6 +499,12 @@ public function manufacturer()
498499
return $this->belongsTo(Manufacturer::class, 'manufacturer_id')->withTrashed();
499500
}
500501

502+
503+
public function depreciation(): BelongsTo
504+
{
505+
return $this->belongsTo(Depreciation::class, 'depreciation_id');
506+
}
507+
501508
/**
502509
* Determine whether the user should be emailed on checkin/checkout
503510
*

0 commit comments

Comments
 (0)