Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Const to enum #53961

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3a251cf
const to enum
shaedrich Dec 18, 2024
5bf6f69
Fix syntax error
shaedrich Dec 18, 2024
dbf5145
Fix StyleCI
shaedrich Dec 18, 2024
86733fd
More linting fixes
shaedrich Dec 18, 2024
7ccc387
Fix Typo
shaedrich Dec 18, 2024
2ebb039
Fix typo
shaedrich Dec 18, 2024
af63fdd
Add missing input
shaedrich Dec 18, 2024
053c72a
stringify enums
shaedrich Dec 18, 2024
7d04309
Fix typo
shaedrich Dec 18, 2024
eaa84c0
enum_value
shaedrich Dec 18, 2024
8712ba9
Fix test
shaedrich Dec 18, 2024
01468a4
Another enum_value
shaedrich Dec 18, 2024
a7fd3e9
FIx StyleCI
shaedrich Dec 18, 2024
52ed299
Fix syntax error
shaedrich Dec 18, 2024
a248b4e
Add missing import
shaedrich Dec 18, 2024
27da74a
Fix StyleCI
shaedrich Dec 18, 2024
201a5e2
Only wrap in enum if it is none
shaedrich Dec 18, 2024
a8eca1b
Remove worker-related enum
shaedrich Dec 18, 2024
935efcc
Change casing for enum cases
shaedrich Dec 18, 2024
31cc1b3
Remove unused function
shaedrich Dec 18, 2024
7f0bda7
Revert to promoted property
shaedrich Dec 18, 2024
f49854a
Remove enum_value to the proper location
shaedrich Dec 18, 2024
4c17dcd
Cron expression is not an enum
shaedrich Dec 18, 2024
f44b034
Also make ScheduleOn cases PascalCase
shaedrich Dec 19, 2024
e065656
More casing
shaedrich Dec 19, 2024
a0a2ee9
Remove manually setting argument that has been reverted to a promited…
shaedrich Dec 19, 2024
41a889a
Deprecation messages and correct PHPDoc types
shaedrich Dec 19, 2024
1b19e79
Improve type-hint for days
shaedrich Dec 19, 2024
8ce6e6c
More PHPDocs
shaedrich Dec 19, 2024
f384afd
Fix StyleCI
shaedrich Dec 19, 2024
c66a581
More StyleCI
shaedrich Dec 19, 2024
0e26932
Remove accidentally added spacing
shaedrich Dec 19, 2024
76cc1d6
StyleCI leftovers
shaedrich Dec 19, 2024
ddfacc2
Months are 1-indexed
shaedrich Dec 19, 2024
2ddbab1
Add type-hints to respective facade
shaedrich Dec 19, 2024
1df5098
Add range type-hint for seconds
shaedrich Dec 19, 2024
4418d4f
Add array type-hint
shaedrich Dec 19, 2024
f08f992
Fix typo
shaedrich Dec 19, 2024
d4f76ad
formatting
taylorotwell Dec 19, 2024
eed57b8
Update PasswordResetResult.php
taylorotwell Dec 19, 2024
d86d3ad
still use strings for passwords
taylorotwell Dec 19, 2024
3c1ba5f
cant depend on auth component
taylorotwell Dec 19, 2024
680036d
dont use enums
taylorotwell Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Illuminate/Auth/Passwords/PasswordBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Auth\Events\PasswordResetLinkSent;
use Illuminate\Auth\Passwords\PasswordResetResult;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract;
use Illuminate\Contracts\Auth\UserProvider;
Expand Down Expand Up @@ -64,17 +65,17 @@ public function sendResetLink(#[\SensitiveParameter] array $credentials, ?Closur
$user = $this->getUser($credentials);

if (is_null($user)) {
return static::INVALID_USER;
return PasswordResetResult::InvalidUser;
}

if ($this->tokens->recentlyCreatedToken($user)) {
return static::RESET_THROTTLED;
return PasswordResetResult::Throttled;
}

$token = $this->tokens->create($user);

if ($callback) {
return $callback($user, $token) ?? static::RESET_LINK_SENT;
return $callback($user, $token) ?? PasswordResetResult::ResetLinkSent;
}

// Once we have the reset token, we are ready to send the message out to this
Expand All @@ -84,7 +85,7 @@ public function sendResetLink(#[\SensitiveParameter] array $credentials, ?Closur

$this->events?->dispatch(new PasswordResetLinkSent($user));

return static::RESET_LINK_SENT;
return PasswordResetResult::ResetLinkSent;
}

/**
Expand Down Expand Up @@ -114,7 +115,7 @@ public function reset(#[\SensitiveParameter] array $credentials, Closure $callba

$this->tokens->delete($user);

return static::PASSWORD_RESET;
return PasswordResetResult::PasswordReset;
}

/**
Expand All @@ -126,11 +127,11 @@ public function reset(#[\SensitiveParameter] array $credentials, Closure $callba
protected function validateReset(#[\SensitiveParameter] array $credentials)
{
if (is_null($user = $this->getUser($credentials))) {
return static::INVALID_USER;
return PasswordResetResult::InvalidUser;
}

if (! $this->tokens->exists($user, $credentials['token'])) {
return static::INVALID_TOKEN;
return PasswordResetResult::InvalidToken;
}

return $user;
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/Auth/Passwords/PasswordResetResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Auth\Passwords;

class PasswordResetResult
{
/**
* Result indicating a successfully sent password reset email.
*/
const ResetLinkSent = 'passwords.sent';

/**
* Result representing a successfully reset password.
*/
const PasswordReset = 'passwords.reset';

/**
* Result indicating the user is invalid.
*/
const InvalidUser = 'passwords.user';

/**
* Result indicating the token is invalid.
*/
const InvalidToken = 'passwords.token';

/**
* Result indicating the password reset attempt has been throttled.
*/
const Throttled = 'passwords.throttled';
}
14 changes: 14 additions & 0 deletions src/Illuminate/Console/Scheduling/Enums/Day.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Illuminate\Console\Scheduling\Enums;

enum Day: int
{
case Sunday = 0;
case Monday = 1;
case Tuesday = 2;
case Wednesday = 3;
case Thursday = 4;
case Friday = 5;
case Saturday = 6;
}
57 changes: 31 additions & 26 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Illuminate\Console\Scheduling;

use Illuminate\Console\Scheduling\Enums\Day;
use Illuminate\Support\Carbon;
use InvalidArgumentException;

use function Illuminate\Support\enum_value;

trait ManagesFrequencies
{
/**
Expand Down Expand Up @@ -143,7 +146,7 @@ public function everyThirtySeconds()
/**
* Schedule the event to run multiple times per minute.
*
* @param int $seconds
* @param int<0, 59> $seconds
* @return $this
*/
protected function repeatEvery($seconds)
Expand Down Expand Up @@ -250,7 +253,7 @@ public function hourly()
/**
* Schedule the event to run hourly at a given offset in the hour.
*
* @param array|string|int $offset
* @param array|string|int<0, 24>|int<0, 24>[] $offset
* @return $this
*/
public function hourlyAt($offset)
Expand Down Expand Up @@ -353,8 +356,8 @@ public function dailyAt($time)
/**
* Schedule the event to run twice daily.
*
* @param int $first
* @param int $second
* @param int $first<0, 24>
* @param int $second<0, 24>
* @return $this
*/
public function twiceDaily($first = 1, $second = 13)
Expand All @@ -365,9 +368,9 @@ public function twiceDaily($first = 1, $second = 13)
/**
* Schedule the event to run twice daily at a given offset.
*
* @param int $first
* @param int $second
* @param int $offset
* @param int $first<0, 24>
* @param int $second<0, 24>
* @param int $offset<0, 59>
* @return $this
*/
public function twiceDailyAt($first = 1, $second = 13, $offset = 0)
Expand All @@ -380,8 +383,8 @@ public function twiceDailyAt($first = 1, $second = 13, $offset = 0)
/**
* Schedule the event to run at the given minutes and hours.
*
* @param array|string|int $minutes
* @param array|string|int $hours
* @param array|string|int<0, 59> $minutes
* @param array|string|int<0, 24> $hours
* @return $this
*/
protected function hourBasedSchedule($minutes, $hours)
Expand All @@ -401,7 +404,7 @@ protected function hourBasedSchedule($minutes, $hours)
*/
public function weekdays()
{
return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY);
return $this->days(Day::Monday->value.'-'.Day::Friday->value);
}

/**
Expand All @@ -411,7 +414,7 @@ public function weekdays()
*/
public function weekends()
{
return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY);
return $this->days(Day::Saturday->value.','.Day::Sunday->value);
}

/**
Expand All @@ -421,7 +424,7 @@ public function weekends()
*/
public function mondays()
{
return $this->days(Schedule::MONDAY);
return $this->days(Day::Monday);
}

/**
Expand All @@ -431,7 +434,7 @@ public function mondays()
*/
public function tuesdays()
{
return $this->days(Schedule::TUESDAY);
return $this->days(Day::Tuesday);
}

/**
Expand All @@ -441,7 +444,7 @@ public function tuesdays()
*/
public function wednesdays()
{
return $this->days(Schedule::WEDNESDAY);
return $this->days(Day::Wednesday);
}

/**
Expand All @@ -451,7 +454,7 @@ public function wednesdays()
*/
public function thursdays()
{
return $this->days(Schedule::THURSDAY);
return $this->days(Day::Thursday);
}

/**
Expand All @@ -461,7 +464,7 @@ public function thursdays()
*/
public function fridays()
{
return $this->days(Schedule::FRIDAY);
return $this->days(Day::Friday);
}

/**
Expand All @@ -471,7 +474,7 @@ public function fridays()
*/
public function saturdays()
{
return $this->days(Schedule::SATURDAY);
return $this->days(Day::Saturday);
}

/**
Expand All @@ -481,7 +484,7 @@ public function saturdays()
*/
public function sundays()
{
return $this->days(Schedule::SUNDAY);
return $this->days(Day::Sunday);
}

/**
Expand All @@ -499,7 +502,7 @@ public function weekly()
/**
* Schedule the event to run weekly on a given day and time.
*
* @param array|mixed $dayOfWeek
* @param Day[]|Day|array|int|int[]|mixed $dayOfWeek
* @param string $time
* @return $this
*/
Expand All @@ -525,7 +528,7 @@ public function monthly()
/**
* Schedule the event to run monthly on a given day and time.
*
* @param int $dayOfMonth
* @param int<0, 31> $dayOfMonth
* @param string $time
* @return $this
*/
Expand All @@ -539,8 +542,8 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0')
/**
* Schedule the event to run twice monthly at a given time.
*
* @param int $first
* @param int $second
* @param int $first<0, 31>
* @param int $second<0, 31>
* @param string $time
* @return $this
*/
Expand Down Expand Up @@ -610,8 +613,8 @@ public function yearly()
/**
* Schedule the event to run yearly on a given month, day, and time.
*
* @param int $month
* @param int|string $dayOfMonth
* @param int<1, 12> $month
* @param int<0, 31>|string $dayOfMonth
* @param string $time
* @return $this
*/
Expand All @@ -626,13 +629,15 @@ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')
/**
* Set the days of the week the command should run on.
*
* @param array|mixed $days
* @param Day[]|Day|array|int[]|int|string[]|string|mixed $days
* @return $this
*/
public function days($days)
{
$days = is_array($days) ? $days : func_get_args();

$days = array_map(enum_value(...), $days);

return $this->spliceIntoPosition(5, implode(',', $days));
}

Expand Down Expand Up @@ -660,7 +665,7 @@ protected function spliceIntoPosition($position, $value)
{
$segments = preg_split("/\s+/", $this->expression);

$segments[$position - 1] = $value;
$segments[$position - 1] = enum_value($value);

return $this->cron(implode(' ', $segments));
}
Expand Down
6 changes: 0 additions & 6 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@ class Schedule
}

const SUNDAY = 0;

const MONDAY = 1;

const TUESDAY = 2;

const WEDNESDAY = 3;

const THURSDAY = 4;

const FRIDAY = 5;

const SATURDAY = 6;

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function registerTimeoutHandler($job, WorkerOptions $options)
));
}

$this->kill(static::EXIT_ERROR, $options);
$this->kill(self::EXIT_ERROR, $options);
}, true);

pcntl_alarm(
Expand Down Expand Up @@ -304,12 +304,12 @@ protected function pauseWorker(WorkerOptions $options, $lastRestart)
protected function stopIfNecessary(WorkerOptions $options, $lastRestart, $startTime = 0, $jobsProcessed = 0, $job = null)
{
return match (true) {
$this->shouldQuit => static::EXIT_SUCCESS,
$this->memoryExceeded($options->memory) => static::EXIT_MEMORY_LIMIT,
$this->queueShouldRestart($lastRestart) => static::EXIT_SUCCESS,
$options->stopWhenEmpty && is_null($job) => static::EXIT_SUCCESS,
$options->maxTime && hrtime(true) / 1e9 - $startTime >= $options->maxTime => static::EXIT_SUCCESS,
$options->maxJobs && $jobsProcessed >= $options->maxJobs => static::EXIT_SUCCESS,
$this->shouldQuit => self::EXIT_SUCCESS,
$this->memoryExceeded($options->memory) => self::EXIT_MEMORY_LIMIT,
$this->queueShouldRestart($lastRestart) => self::EXIT_SUCCESS,
$options->stopWhenEmpty && is_null($job) => self::EXIT_SUCCESS,
$options->maxTime && hrtime(true) / 1e9 - $startTime >= $options->maxTime => self::EXIT_SUCCESS,
$options->maxJobs && $jobsProcessed >= $options->maxJobs => self::EXIT_SUCCESS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also be a leftover? Or is the change from static to self intentionally left as a part of this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While reviewing this file, I also felt compelled to say this change was unnecessary.

But after some thinking, and considering your previous remark regarding when enums should be used versus when constants should be used, I thought that, even if the class isn't final, there is no point on reading these constants from subclasses, as they are basically hard-coded exit values from processes, and should not change.

Using self:: instead of static::, improves -- IMO -- intention declaration and correctness in this case.

default => null
};
}
Expand Down
Loading