Skip to content

A free open source IT asset/license management system

Fixed phpstan “will always evaluate to true” errors

snipec59cdbedba1e
diff --git a/app/Console/Commands/ValidateAssets.php b/app/Console/Commands/ValidateAssets.php
index 7dc11a8121..34480cca23 100644
--- a/app/Console/Commands/ValidateAssets.php
+++ b/app/Console/Commands/ValidateAssets.php
@@ -4,7 +4,6 @@ namespace App\Console\Commands;
use App\Models\Asset;
use Illuminate\Console\Command;
-use Illuminate\Support\MessageBag;
class ValidateAssets extends Command
{
@@ -69,18 +68,7 @@ class ValidateAssets extends Command
private function formatValidationErrors(Asset $asset): string
{
- $errors = $asset->getErrors();
- $messages = [];
-
- if ($errors instanceof MessageBag) {
- $messages = $errors->all();
- } elseif (is_array($errors)) {
- $messages = $errors;
- } else {
- $messages = [(string) $errors];
- }
-
- $prefixedMessages = collect($messages)
+ $prefixedMessages = collect($asset->getErrors()->all())
->map(fn ($message) => trim((string) $message))
->filter()
->map(fn (string $message) => str_starts_with($message, '✘') ? $message : '✘ '.$message)
diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index e2b7bb7a86..fb8fea60b1 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -81,7 +81,7 @@ class Handler extends ExceptionHandler
// escape past the friendly user-facing error.
if (
config('app.debug')
- && !app()->environment('production')
+ && ! app()->environment('production')
&& $e instanceof \Error
) {
throw $e;
@@ -182,7 +182,7 @@ class Handler extends ExceptionHandler
// This is traaaaash but it handles models that are not found while using route model binding :(
// The only alternative is to set that at *each* route, which is crazypants
if ($e instanceof ModelNotFoundException) {
- $ids = method_exists($e, 'getIds') ? $e->getIds() : [];
+ $ids = $e->getIds();
if (in_array('bulkedit', $ids, true)) {
$error_array = session()->get('bulk_asset_errors');
@@ -219,7 +219,7 @@ class Handler extends ExceptionHandler
// Normalize the space-separated derived name to underscore
// so compound class names (AssetModel -> "asset model" -> "asset_model")
// resolve to keys that actually exist in general.php.
- $translationKey = 'general.' . str_replace(' ', '_', $model_name);
+ $translationKey = 'general.'.str_replace(' ', '_', $model_name);
$translatedName = Lang::has($translationKey) ? trans($translationKey) : $model_name;
return redirect()
diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php
index 966ed73185..08ba9b71dc 100644
--- a/app/Http/Transformers/ActionlogsTransformer.php
+++ b/app/Http/Transformers/ActionlogsTransformer.php
@@ -217,7 +217,7 @@ class ActionlogsTransformer
: null,
'note' => ($actionlog->note) ? Helper::parseEscapedMarkedownInline($actionlog->note) : null,
'signature_file' => (($actionlog->accept_signature) && Storage::exists('private_uploads/signatures/'.$actionlog->accept_signature)) ? route('log.signature.view', ['filename' => $actionlog->accept_signature]) : null,
- 'log_meta' => ((isset($clean_meta)) && (is_array($clean_meta))) ? $clean_meta : null,
+ 'log_meta' => $clean_meta ?? null,
'remote_ip' => e($actionlog->remote_ip) ?? null,
'user_agent' => e($actionlog->user_agent) ?? null,
'action_source' => ($actionlog->action_source) ?? null,
diff --git a/app/Http/Transformers/UploadedFilesTransformer.php b/app/Http/Transformers/UploadedFilesTransformer.php
index b153a23291..b50048dc3f 100644
--- a/app/Http/Transformers/UploadedFilesTransformer.php
+++ b/app/Http/Transformers/UploadedFilesTransformer.php
@@ -27,7 +27,7 @@ class UploadedFilesTransformer
$snipeModel = $file->item_type;
$item = null;
- if (is_string($snipeModel) && class_exists($snipeModel)) {
+ if (class_exists($snipeModel)) {
$itemQuery = $snipeModel::query();
if (in_array(SoftDeletes::class, class_uses_recursive($snipeModel), true)) {
diff --git a/app/Livewire/LdapSettings.php b/app/Livewire/LdapSettings.php
index 51d7de9707..622647d4ed 100644
--- a/app/Livewire/LdapSettings.php
+++ b/app/Livewire/LdapSettings.php
@@ -1447,22 +1447,78 @@ class LdapSettings extends Component
return;
}
+ // Every string-typed prop that participates in the LDAP
+ // handshake or a downstream test surface, grouped by wizard
+ // step. Shared between the trim-on-assignment and the
+ // clearTestResult() blocks below because both need the same
+ // "user typed into a field the test depends on" signal.
+ $connectionStringProps = [
+ // Step 1: Connection
+ 'ldap_server',
+ 'ldap_client_tls_key',
+ 'ldap_client_tls_cert',
+ // Step 2: Authenticate + Scope
+ 'ldap_uname',
+ 'ldap_pword',
+ 'ldap_basedn',
+ 'ldap_filter',
+ 'ldap_auth_filter_query',
+ // Step 3: Attribute Mapping
+ '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',
+ // Step 4: Sync + Defaults
+ 'custom_forgot_pass_url',
+ // Not persisted; step-4 Look Up preview input
+ 'test_sample_username',
+ ];
+
// Trim string values on assignment so pasted-with-whitespace
// inputs get normalized both in the visible field and in the
// saved config. Without this a leading space on ldap_server
// silently fails starts_with:ldap://, and a trailing newline in
// a bind username produces a mysterious LDAP-side rejection at
- // auth time. Textareas (TLS key/cert) tolerate the trim because
- // PEM parsers accept both terminating-newline and no-terminating-
- // newline forms.
- 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})) {
+ // auth time. Textareas (TLS key / cert) tolerate the trim
+ // because PEM parsers accept both terminating-newline and
+ // no-terminating-newline forms. ad_domain gets trimmed too but
+ // does NOT invalidate the test below; it's not part of the
+ // ldap_bind() handshake, only post-connection scoping.
+ if (in_array($property, [...$connectionStringProps, 'ad_domain'], true)) {
$this->{$property} = trim($this->{$property});
}
// Any edit to a connection-shape field invalidates the prior
// network test result (a stale "connected" indicator sitting
// under a since-edited server URL would be actively misleading).
- 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)) {
+ // Every connection-participating string prop plus the non-string
+ // toggles / IDs that also affect the handshake or downstream
+ // test surface.
+ $testInvalidatingProps = [
+ ...$connectionStringProps,
+ 'ldap_tls',
+ 'ldap_server_cert_ignore',
+ 'ldap_invert_active_flag',
+ 'ldap_enabled',
+ 'ldap_pw_sync',
+ 'ldap_default_group',
+ ];
+
+ if (in_array($property, $testInvalidatingProps, true)) {
$this->clearTestResult();
// Also clear the step-4 preview table since it references the
// old values.
@@ -1473,11 +1529,31 @@ class LdapSettings extends Component
// Clear this field's inline validation error so re-editing the
// field un-greys the Save button (which keys off canAdvance,
// which checks the error bag).
- 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)) {
+ 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)) {
$this->resetValidation($property);
}
- if (! in_array($property, ['currentStep', 'highestStepReached', 'dirty', 'testStatus', 'testMessage'], true)) {
+ if (!in_array($property, [
+ 'currentStep',
+ 'highestStepReached',
+ 'dirty',
+ 'testStatus',
+ 'testMessage'
+ ], true)) {
$this->dirty = true;
}
}
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 6799948137..d751068422 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -570,18 +570,6 @@ parameters:
count: 1
path: app/Console/Commands/ToggleCustomfieldEncryption.php
- -
- message: '#^Call to function is_array\(\) with \*NEVER\* will always evaluate to true\.$#'
- identifier: function.alreadyNarrowedType
- count: 1
- path: app/Console/Commands/ValidateAssets.php
-
- -
- message: '#^Instanceof between Illuminate\\Support\\MessageBag and Illuminate\\Support\\MessageBag will always evaluate to true\.$#'
- identifier: instanceof.alwaysTrue
- count: 1
- path: app/Console/Commands/ValidateAssets.php
-
-
message: '#^Using nullsafe property access "\?\-\>model_number" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
identifier: nullsafe.neverNull
@@ -648,12 +636,6 @@ parameters:
count: 2
path: app/Exceptions/Handler.php
- -
- message: '#^Call to function method_exists\(\) with Illuminate\\Database\\Eloquent\\ModelNotFoundException and ''getIds'' will always evaluate to true\.$#'
- identifier: function.alreadyNarrowedType
- count: 1
- path: app/Exceptions/Handler.php
-
-
message: '#^Instanceof between Exception and Carbon\\Exceptions\\InvalidFormatException will always evaluate to false\.$#'
identifier: instanceof.alwaysFalse
@@ -3702,12 +3684,6 @@ parameters:
count: 1
path: app/Http/Transformers/ActionlogsTransformer.php
- -
- message: '#^Call to function is_array\(\) with array will always evaluate to true\.$#'
- identifier: function.alreadyNarrowedType
- count: 1
- path: app/Http/Transformers/ActionlogsTransformer.php
-
-
message: '#^Expression on left side of \?\? is not nullable\.$#'
identifier: nullCoalesce.expr
@@ -4056,12 +4032,6 @@ parameters:
count: 1
path: app/Http/Transformers/SuppliersTransformer.php
- -
- message: '#^Call to function is_string\(\) with string will always evaluate to true\.$#'
- identifier: function.alreadyNarrowedType
- count: 1
- path: app/Http/Transformers/UploadedFilesTransformer.php
-
-
message: '#^Expression on left side of \?\? is not nullable\.$#'
identifier: nullCoalesce.expr
@@ -4794,12 +4764,6 @@ parameters:
count: 1
path: app/Livewire/Importer.php
- -
- message: '#^Call to function is_string\(\) with string will always evaluate to true\.$#'
- identifier: function.alreadyNarrowedType
- count: 1
- path: app/Livewire/LdapSettings.php
-
-
message: '#^Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$name\.$#'
identifier: property.notFound