Skip to content

Commit

Permalink
Remove spread (...) operator in function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Aug 6, 2023
1 parent df9b357 commit 0e61806
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 83 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function notify(Request $request, Response $response): void

public function verify(Request $request, Response $response, UpdateAction $updateAction): void
{
if (!$request->hasQuery('email', 'token')) {
if (!$request->hasQuery(['email', 'token'])) {
$response->data(__('bad_request'))->send(400);
}

Expand All @@ -72,7 +72,7 @@ public function verify(Request $request, Response $response, UpdateAction $updat

$token->delete();

$user = $updateAction->handle(['email_verified' => Carbon::now()->toDateTimeString()], $request->queries('email'));
$user = $updateAction->handle(['email_verified' => Carbon::now()->toDateTimeString()], $request->email);

if (!$user) {
Alert::default(__('account_not_found'))->error();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function notify(Request $request, Response $response): void

public function reset(Request $request, Response $response): void
{
if (!$request->hasQuery('email', 'token')) {
if (!$request->hasQuery(['email', 'token'])) {
$response->data(__('bad_request'))->send(400);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public function authenticate(Request $request, Response $response, LoginValidato
{
$loginValidator->validate($request->inputs(), $response);

if (Auth::attempt($response, $request->only('email', 'password'), $request->has('remember'))) {
if (Auth::attempt($response, $request->only(['email', 'password']), $request->has('remember'))) {
$uri = !Session::has('intended') ? config('app.home') : Session::pull('intended');

Alert::toast(__('welcome', ['name' => Auth::get('name')]))->success();
$response->url($uri)->send();
}

Alert::default(__('login_failed'))->error();
$response->url('/login')->withInputs($request->only('email', 'password'))->withErrors([__('login_failed')])->send();
$response->url('/login')->withInputs($request->only(['email', 'password']))->withErrors([__('login_failed')])->send();
}
}
2 changes: 1 addition & 1 deletion app/Http/Middlewares/CsrfProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handle(Request $request): void
return;
}

if (!$request->filled('_csrf_token')) {
if (!$request->filled(['_csrf_token'])) {
throw new MissingCsrfTokenException();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Validators/Auth/RegisterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RegisterValidator extends Validator
public function __construct()
{
$this->addCustomRule('unique', function($field, array $input, array $params, $value) {
$data = (new Repository($params[0]))->select('*')->where($field, $value);
$data = (new Repository($params[0]))->select(['*'])->where($field, $value);
return !$data->exists();
}, 'This {field} is already registered');
}
Expand Down
2 changes: 1 addition & 1 deletion config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/**
* Routes paths configuration
* Routes paths configuration with base directory '/views'
*/

return [
Expand Down
2 changes: 1 addition & 1 deletion core/Console/Database/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$databases = $input->getArgument('database');

if (is_null($databases) || empty($databases)) {
if (empty($databases)) {
$db = config('app.env') !== 'test' ? config('database.name') :
config('database.name') . config('tests.database.suffix') ;

Expand Down
2 changes: 1 addition & 1 deletion core/Console/Database/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$databases = $input->getArgument('database');

if (is_null($databases) || empty($databases)) {
if (empty($databases)) {
$db = config('app.env') !== 'test' ? config('database.name') :
config('database.name') . config('tests.database.suffix') ;

Expand Down
2 changes: 1 addition & 1 deletion core/Console/Make/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$exception = $input->getArgument('exception');
$message = $input->getOption('message');

list(, $class) = Make::generateClass($exception, '');
list(, $class) = Make::generateClass($exception);

if (!Make::createException($exception, $message)) {
$output->writeln('<fg=yellow>Failed to create exception "' . $class . '"</fg>');
Expand Down
2 changes: 1 addition & 1 deletion core/Console/Make/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$helpers = $input->getArgument('helper');

foreach ($helpers as $helper) {
list(, $class) = Make::generateClass($helper, '');
list(, $class) = Make::generateClass($helper);

if (!Make::createHelper($helper)) {
$output->writeln('<fg=yellow>Failed to create helper "' . $class . '"</fg>');
Expand Down
2 changes: 1 addition & 1 deletion core/Console/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$host = $input->getOption('host') ?? '127.0.0.1';
$port = $input->getOption('port') ?? 8080;

$process = new Process(['php', '-S', "{$host}:{$port}"]);
$process = new Process(['php', '-S', "$host:$port"]);
$process->setTimeout(null);
$process->start();

Expand Down
13 changes: 8 additions & 5 deletions core/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public static function latest(string $column = 'id', $subquery = null): array|fa
return self::select('*')->subQuery($subquery)->latest($column)->getAll();
}

public static function select(string ...$columns): Repository
public static function select(array|string $columns): Repository
{
return (new Repository(static::$table))->select(...$columns);
return (new Repository(static::$table))->select($columns);
}

public static function where(string $column, $operator = null, $value = null): Repository
Expand Down Expand Up @@ -231,13 +231,16 @@ public function decrement(string $column, $value = null): void
$this->{$column} = $this->{$column} - $value;
}

public function toArray(string ...$attributes): array
public function toArray(array|string $attributes = null): array
{
$data = (array) $this;

if (is_null($this->id)) unset($data['id']);
if (is_null($this->id)) {
unset($data['id']);
}

if (!empty($attributes)) {
if (!is_null($attributes)) {
$attributes = parse_array($attributes);
$d = [];

foreach ($attributes as $attribute) {
Expand Down
8 changes: 5 additions & 3 deletions core/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ public static function deleteColumn(string $table, string $column): self
return new self();
}

public function select(string ...$columns): self
public function select(array|string $columns): self
{
$columns = parse_array($columns);
self::$query = 'SELECT ';

foreach ($columns as $column) {
self::$query .= "{$column}, ";
self::$query .= "$column, ";
}

self::$query = rtrim(self::$query, ', ');
Expand Down Expand Up @@ -469,8 +470,9 @@ public function orderBy(string $column, string $direction): self
return $this;
}

public function groupBy(string ...$columns): self
public function groupBy(array|string $columns): self
{
$columns = parse_array($columns);
self::$query .= ' GROUP BY ';

foreach ($columns as $column) {
Expand Down
16 changes: 8 additions & 8 deletions core/Database/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ class Repository

public function __construct(private readonly string $table) {}

public function select(string ...$columns): self
public function select(array|string $columns): self
{
$this->qb = QueryBuilder::table($this->table)->select(...$columns);
$this->qb = QueryBuilder::table($this->table)->select($columns);
return $this;
}

public function selectOne(string ...$columns): mixed
public function selectOne(array|string $columns): mixed
{
return $this->select(...$columns)->get();
return $this->select($columns)->get();
}

public function selectAll(string ...$columns): array|false
public function selectAll(array|string $columns): array|false
{
return $this->select(...$columns)->getAll();
return $this->select($columns)->getAll();
}

public function selectRaw(string $query, array $args = []): self
Expand Down Expand Up @@ -561,9 +561,9 @@ public function latest(string $column = 'id'): self
return $this->orderDesc($column);
}

public function groupBy(string ...$columns): self
public function groupBy(array|string $columns): self
{
$this->qb->groupBy(...$columns);
$this->qb->groupBy($columns);
return $this;
}

Expand Down
28 changes: 19 additions & 9 deletions core/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ public function remoteIP(): string
return $this->headers('REMOTE_ADDR', '');
}

public function has(string ...$items): bool
public function has(array|string $items): bool
{
$items = parse_array($items);
$result = false;

foreach ($items as $item) {
Expand All @@ -171,8 +172,9 @@ public function has(string ...$items): bool
return $result;
}

public function hasQuery(string ...$items): bool
public function hasQuery(array|string $items): bool
{
$items = parse_array($items);
$result = false;

foreach ($items as $item) {
Expand All @@ -182,8 +184,9 @@ public function hasQuery(string ...$items): bool
return $result;
}

public function hasInput(string ...$items): bool
public function hasInput(array|string $items): bool
{
$items = parse_array($items);
$result = false;

foreach ($items as $item) {
Expand All @@ -193,11 +196,14 @@ public function hasInput(string ...$items): bool
return $result;
}

public function filled(string ...$items): bool
public function filled(array|string $items): bool
{
$result = $this->has(...$items);
$items = parse_array($items);
$result = $this->has($items);

if (!$result) return false;
if (!$result) {
return false;
}

foreach ($items as $item) {
$result = !empty($this->{$item});
Expand Down Expand Up @@ -225,8 +231,9 @@ public function set(string $item, $value): void
$this->{$item} = $value;
}

public function only(string ...$items): array
public function only(array|string $items): array
{
$items = parse_array($items);
$result = [];

foreach ($items as $item) {
Expand All @@ -238,11 +245,14 @@ public function only(string ...$items): array
return $result;
}

public function except(string ...$items): array
public function except(array|string $items): array
{
$items = parse_array($items);
$result = [];

if (empty($this->all())) return $result;
if (empty($this->all())) {
return $result;
}

foreach ($items as $item) {
foreach ($this->all() as $key => $input) {
Expand Down
7 changes: 5 additions & 2 deletions core/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ public static function group($callback): self
return new static();
}

public function middleware(string ...$middlewares): self
public function middleware(array|string $middlewares): self
{
$middlewares = parse_array($middlewares);
static::$tmp_routes[static::$route]['middlewares'] = $middlewares;
return $this;
}

public function byMiddleware(string ...$middlewares): self
public function byMiddleware(array|string $middlewares): self
{
$middlewares = parse_array($middlewares);

foreach (static::$tmp_routes as $route => $options) {
if (isset($options['middlewares'])) {
static::$tmp_routes[$route]['middlewares'] = array_merge($middlewares, $options['middlewares']);
Expand Down
10 changes: 0 additions & 10 deletions core/Support/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ public static function default($message, bool $dismiss = true): self

return new self();
}

public static function popup($message): self
{
self::$alert = [
'message' => $message,
'display' => 'popup'
];

return new self();
}

public static function toast($message) : self
{
Expand Down
6 changes: 3 additions & 3 deletions core/Support/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public static function attempt(Response $response, array $credentials, bool $rem
$response
->back()
->with('auth_attempts_timeout', Carbon::now()->addMinutes(config('security.auth.unlock_timeout'))->toDateTimeString())
->send(302);
->send();
}

return false;
}

Session::forget('auth_attempts', 'auth_attempts_timeout');
Session::forget(['auth_attempts', 'auth_attempts_timeout']);
Session::create('user', $user);

if ($remember) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public static function get(?string $key = null): mixed

public static function forget(): void
{
Session::forget('user', 'history', 'csrf_token');
Session::forget(['user', 'history', 'csrf_token']);

if (self::remember()) {
Cookies::delete('user');
Expand Down
18 changes: 16 additions & 2 deletions core/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ function session_has(string $name): bool
}

if (!function_exists('session_forget')) {
function session_forget(string ...$names): void
function session_forget(array|string $names): void
{
Session::forget(...$names);
Session::forget($names);
}
}

Expand Down Expand Up @@ -461,6 +461,20 @@ function env(string $key, $default = null): mixed
}
}

if (!function_exists('parse_array')) {
/**
* Convert string to array
*/
function parse_array(array|string $value): array
{
if (is_array($value)) {
return $value;
}

return [$value];
}
}

/**
* Laravel helpers from \Illuminate\Support\helpers.php
*/
Expand Down
Loading

0 comments on commit 0e61806

Please sign in to comment.