Yet more static analysis

This commit is contained in:
Lance Pioch 2024-03-17 13:46:01 -04:00
parent e9ea5b1cae
commit 7c0a46deaa
14 changed files with 35 additions and 35 deletions

View File

@ -2,11 +2,10 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\User; use App\Models\User;
use App\Models\Model;
use Illuminate\Support\Collection;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\QueryBuilder;
@ -129,22 +128,25 @@ class UserController extends Controller
/** /**
* Get a JSON response of users on the system. * Get a JSON response of users on the system.
*/ */
public function json(Request $request): Model|Collection public function json(Request $request): JsonResponse
{ {
$users = QueryBuilder::for(User::query())->allowedFilters(['email'])->paginate(25); $userPaginator = QueryBuilder::for(User::query())->allowedFilters(['email'])->paginate(25);
/** @var User[] $users */
$users = $userPaginator->items();
// Handle single user requests. // Handle single user requests.
if ($request->query('user_id')) { if ($request->query('user_id')) {
$user = User::query()->findOrFail($request->input('user_id')); $user = User::query()->findOrFail($request->input('user_id'));
$user['md5'] = md5(strtolower($user->email)); $user['md5'] = md5(strtolower($user->email));
return $user; return response()->json($user);
} }
return $users->map(function (User $item) { return response()->json(collect($users)->map(function (User $user) {
$item['md5'] = md5(strtolower($item->email)); $user['md5'] = md5(strtolower($user->email));
return $item; return $user;
}); }));
} }
} }

View File

@ -30,7 +30,7 @@ class NodeDeploymentController extends ApplicationApiController
$nodes = $this->viableNodesService $nodes = $this->viableNodesService
->setMemory($data['memory']) ->setMemory($data['memory'])
->setDisk($data['disk']) ->setDisk($data['disk'])
->handle($request->query('per_page'), $request->query('page')); ->handle((int) $request->query('per_page'), (int) $request->query('page'));
return $this->fractal->collection($nodes) return $this->fractal->collection($nodes)
->transformWith($this->getTransformer(NodeTransformer::class)) ->transformWith($this->getTransformer(NodeTransformer::class))

View File

@ -20,7 +20,7 @@ class ActivityLogController extends ClientApiController
->allowedSorts(['timestamp']) ->allowedSorts(['timestamp'])
->with('actor') ->with('actor')
->whereNotIn('activity_logs.event', ActivityLog::DISABLED_EVENTS) ->whereNotIn('activity_logs.event', ActivityLog::DISABLED_EVENTS)
->paginate(min($request->query('per_page', 25), 100)) ->paginate(min($request->query('per_page', '25'), 100))
->appends($request->query()); ->appends($request->query());
return $this->fractal->collection($activity) return $this->fractal->collection($activity)

View File

@ -61,7 +61,7 @@ class ClientController extends ClientApiController
$builder = $builder->whereIn('servers.id', $user->accessibleServers()->pluck('id')->all()); $builder = $builder->whereIn('servers.id', $user->accessibleServers()->pluck('id')->all());
} }
$servers = $builder->paginate(min($request->query('per_page', 50), 100))->appends($request->query()); $servers = $builder->paginate(min($request->query('per_page', '50'), 100))->appends($request->query());
return $this->fractal->transformWith($transformer)->collection($servers)->toArray(); return $this->fractal->transformWith($transformer)->collection($servers)->toArray();
} }

View File

@ -31,7 +31,7 @@ class ActivityLogController extends ClientApiController
->when(config('activity.hide_admin_activity'), function (Builder $builder) use ($server) { ->when(config('activity.hide_admin_activity'), function (Builder $builder) use ($server) {
// We could do this with a query and a lot of joins, but that gets pretty // We could do this with a query and a lot of joins, but that gets pretty
// painful so for now we'll execute a simpler query. // painful so for now we'll execute a simpler query.
$subusers = $server->subusers()->pluck('user_id')->merge($server->owner_id); $subusers = $server->subusers()->pluck('user_id')->merge([$server->owner_id]);
$builder->select('activity_logs.*') $builder->select('activity_logs.*')
->leftJoin('users', function (JoinClause $join) { ->leftJoin('users', function (JoinClause $join) {
@ -44,7 +44,7 @@ class ActivityLogController extends ClientApiController
->orWhereIn('users.id', $subusers); ->orWhereIn('users.id', $subusers);
}); });
}) })
->paginate(min($request->query('per_page', 25), 100)) ->paginate(min($request->query('per_page', '25'), 100))
->appends($request->query()); ->appends($request->query());
return $this->fractal->collection($activity) return $this->fractal->collection($activity)

View File

@ -50,7 +50,7 @@ class BackupController extends ClientApiController
return $this->fractal->collection($server->backups()->paginate($limit)) return $this->fractal->collection($server->backups()->paginate($limit))
->transformWith($this->getTransformer(BackupTransformer::class)) ->transformWith($this->getTransformer(BackupTransformer::class))
->addMeta([ ->addMeta([
'backup_count' => $server->getNonFailedBackups()->count(), 'backup_count' => $server->backups()->nonFailed()->count(),
]) ])
->toArray(); ->toArray();
} }

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -73,4 +74,12 @@ class Backup extends Model
{ {
return $this->belongsTo(Server::class); return $this->belongsTo(Server::class);
} }
/**
* Returns a query filtering only non-failed backups for a specific server.
*/
public function scopeNonFailed(Builder $query): void
{
$query->whereNull('completed_at')->orWhere('is_successful', true);
}
} }

View File

@ -400,14 +400,4 @@ class Server extends Model
->withTrashed() ->withTrashed()
->get(); ->get();
} }
/**
* Returns a query filtering only non-failed backups for a specific server.
*/
public function getNonFailedBackups(): HasMany
{
return $this->backups()->where(
fn ($query) => $query->whereNull('completed_at')->orWhere('is_successful', true)
);
}
} }

View File

@ -86,7 +86,7 @@ class InitiateBackupService
// Check if the server has reached or exceeded its backup limit. // Check if the server has reached or exceeded its backup limit.
// completed_at == null will cover any ongoing backups, while is_successful == true will cover any completed backups. // completed_at == null will cover any ongoing backups, while is_successful == true will cover any completed backups.
$successful = $server->getNonFailedBackups(); $successful = $server->backups()->nonFailed();
if (!$server->backup_limit || $successful->count() >= $server->backup_limit) { if (!$server->backup_limit || $successful->count() >= $server->backup_limit) {
// Do not allow the user to continue if this server is already at its limit and can't override. // Do not allow the user to continue if this server is already at its limit and can't override.
if (!$override || $server->backup_limit <= 0) { if (!$override || $server->backup_limit <= 0) {

View File

@ -34,7 +34,7 @@ class DatabasePasswordService
$this->connection->transaction(function () use ($database, $password) { $this->connection->transaction(function () use ($database, $password) {
$this->dynamic->set('dynamic', $database->database_host_id); $this->dynamic->set('dynamic', $database->database_host_id);
$database->update($database->id, [ $database->update([
'password' => $this->encrypter->encrypt($password), 'password' => $this->encrypter->encrypt($password),
]); ]);

View File

@ -45,11 +45,10 @@ class EggExporterService
'entrypoint' => $egg->copy_script_entry, 'entrypoint' => $egg->copy_script_entry,
], ],
], ],
'variables' => $egg->variables->transform(function (EggVariable $item) { 'variables' => $egg->variables->map(function (EggVariable $eggVariable) {
return Collection::make($item->toArray()) return Collection::make($eggVariable->toArray())
->except(['id', 'egg_id', 'created_at', 'updated_at']) ->except(['id', 'egg_id', 'created_at', 'updated_at'])
->merge(['field_type' => 'text']) ->merge(['field_type' => 'text']);
->toArray();
}), }),
]; ];

View File

@ -36,7 +36,7 @@ class EggUpdateImporterService
EggVariable::unguarded(function () use ($egg, $variable) { EggVariable::unguarded(function () use ($egg, $variable) {
$egg->variables()->updateOrCreate([ $egg->variables()->updateOrCreate([
'env_variable' => $variable['env_variable'], 'env_variable' => $variable['env_variable'],
], Collection::make($variable)->except('egg_id', 'env_variable')->toArray()); ], Collection::make($variable)->except(['egg_id', 'env_variable'])->toArray());
}); });
} }

View File

@ -22,7 +22,7 @@ class ActivityLogTransformer extends BaseClientTransformer
// This is not for security, it is only to provide a unique identifier to // This is not for security, it is only to provide a unique identifier to
// the front-end for each entry to improve rendering performance since there // the front-end for each entry to improve rendering performance since there
// is nothing else sufficiently unique to key off at this point. // is nothing else sufficiently unique to key off at this point.
'id' => sha1($model->id), 'id' => sha1((string) $model->id),
'batch' => $model->batch, 'batch' => $model->batch,
'event' => $model->event, 'event' => $model->event,
'is_api' => !is_null($model->api_key_id), 'is_api' => !is_null($model->api_key_id),
@ -73,7 +73,7 @@ class ActivityLogTransformer extends BaseClientTransformer
$keys = $properties->keys()->filter(fn ($key) => Str::endsWith($key, '_count'))->values(); $keys = $properties->keys()->filter(fn ($key) => Str::endsWith($key, '_count'))->values();
if ($keys->containsOneItem()) { if ($keys->containsOneItem()) {
$properties = $properties->merge(['count' => $properties->get($keys[0])])->except($keys[0]); $properties = $properties->merge(['count' => $properties->get($keys[0])])->except([$keys[0]]);
} }
return (object) $properties->toArray(); return (object) $properties->toArray();

View File

@ -7,7 +7,7 @@ parameters:
- app/ - app/
# Level 9 is the highest level # Level 9 is the highest level
level: 4 level: 5
ignoreErrors: ignoreErrors:
# Prologue\Alerts defines its methods from its configuration file dynamically # Prologue\Alerts defines its methods from its configuration file dynamically