Skip to content

Commit a218614

Browse files
authored
Upgrade the minimum php version to 8.0 for validation. (#4516)
1 parent d50c54d commit a218614

23 files changed

+186
-469
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
},
2020
"require": {
21-
"php": ">=7.2",
21+
"php": ">=8.0",
2222
"egulias/email-validator": "^3.0",
2323
"hyperf/contract": "~3.0.0",
2424
"hyperf/database": "~3.0.0",

src/ClosureValidationRule.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,35 @@
1111
*/
1212
namespace Hyperf\Validation;
1313

14+
use Closure;
1415
use Hyperf\Validation\Contract\Rule;
1516
use Hyperf\Validation\Contract\Rule as RuleContract;
1617

1718
class ClosureValidationRule implements RuleContract
1819
{
19-
/**
20-
* The callback that validates the attribute.
21-
*
22-
* @var \Closure
23-
*/
24-
public $callback;
25-
2620
/**
2721
* Indicates if the validation callback failed.
28-
*
29-
* @var bool
3022
*/
31-
public $failed = false;
23+
public bool $failed = false;
3224

3325
/**
3426
* The validation error message.
35-
*
36-
* @var null|string
3727
*/
38-
public $message;
28+
public ?string $message = null;
3929

4030
/**
4131
* Create a new Closure based validation rule.
4232
*
43-
* @param \Closure $callback
33+
* @param Closure $callback the callback that validates the attribute
4434
*/
45-
public function __construct($callback)
35+
public function __construct(public Closure $callback)
4636
{
47-
$this->callback = $callback;
4837
}
4938

5039
/**
5140
* Determine if the validation rule passes.
52-
*
53-
* @param mixed $value
5441
*/
55-
public function passes(string $attribute, $value): bool
42+
public function passes(string $attribute, mixed $value): bool
5643
{
5744
$this->failed = false;
5845

src/Concerns/ValidatesAttributes.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Carbon\Carbon;
1515
use Carbon\Carbon as Date;
16-
use Countable;
1716
use DateTime;
1817
use DateTimeInterface;
1918
use DateTimeZone;
@@ -59,7 +58,7 @@ public function validateActiveUrl(string $attribute, $value): bool
5958
if ($url = parse_url($value, PHP_URL_HOST)) {
6059
try {
6160
return count(dns_get_record($url . '.', DNS_A | DNS_AAAA)) > 0;
62-
} catch (Exception $e) {
61+
} catch (Exception) {
6362
return false;
6463
}
6564
}
@@ -646,9 +645,7 @@ public function validateInArray(string $attribute, $value, array $parameters): b
646645

647646
$attributeData = ValidationData::extractDataFromPath($explicitPath, $this->data);
648647

649-
$otherValues = Arr::where(Arr::dot($attributeData), function ($value, $key) use ($parameters) {
650-
return Str::is($parameters[0], $key);
651-
});
648+
$otherValues = Arr::where(Arr::dot($attributeData), fn ($value, $key) => Str::is($parameters[0], $key));
652649

653650
return in_array($value, $otherValues);
654651
}
@@ -872,7 +869,7 @@ public function validateRequired(string $attribute, $value): bool
872869
if (is_string($value) && trim($value) === '') {
873870
return false;
874871
}
875-
if ((is_array($value) || $value instanceof Countable) && count($value) < 1) {
872+
if ((is_countable($value)) && count($value) < 1) {
876873
return false;
877874
}
878875
if ($value instanceof SplFileInfo) {
@@ -1057,7 +1054,7 @@ public function validateTimezone(string $attribute, $value): bool
10571054
{
10581055
try {
10591056
new DateTimeZone($value);
1060-
} catch (Throwable $e) {
1057+
} catch (Throwable) {
10611058
return false;
10621059
}
10631060

@@ -1177,9 +1174,8 @@ protected function getDateFormat(string $attribute)
11771174
* Get the date timestamp.
11781175
*
11791176
* @param mixed $value
1180-
* @return bool|int
11811177
*/
1182-
protected function getDateTimestamp($value)
1178+
protected function getDateTimestamp($value): bool|int
11831179
{
11841180
if ($value instanceof DateTimeInterface) {
11851181
return $value->getTimestamp();
@@ -1240,7 +1236,7 @@ protected function getDateTime(string $value)
12401236
}
12411237

12421238
return new DateTime($value);
1243-
} catch (Exception $e) {
1239+
} catch (Exception) {
12441240
}
12451241
}
12461242

@@ -1318,9 +1314,7 @@ protected function extractDistinctValues(string $attribute): array
13181314

13191315
$pattern = str_replace('\*', '[^.]+', preg_quote($attribute, '#'));
13201316

1321-
return Arr::where(Arr::dot($attributeData), function ($value, $key) use ($pattern) {
1322-
return (bool) preg_match('#^' . $pattern . '\z#u', $key);
1323-
});
1317+
return Arr::where(Arr::dot($attributeData), fn ($value, $key) => (bool) preg_match('#^' . $pattern . '\z#u', $key));
13241318
}
13251319

13261320
/**
@@ -1474,9 +1468,8 @@ protected function allFailingRequired(array $attributes): bool
14741468
* Get the size of an attribute.
14751469
*
14761470
* @param mixed $value
1477-
* @return float|int
14781471
*/
1479-
protected function getSize(string $attribute, $value)
1472+
protected function getSize(string $attribute, $value): float|int|string
14801473
{
14811474
$hasNumeric = $this->hasRule($attribute, $this->numericRules);
14821475

@@ -1506,20 +1499,14 @@ protected function getSize(string $attribute, $value)
15061499
*/
15071500
protected function compare($first, $second, string $operator): bool
15081501
{
1509-
switch ($operator) {
1510-
case '<':
1511-
return $first < $second;
1512-
case '>':
1513-
return $first > $second;
1514-
case '<=':
1515-
return $first <= $second;
1516-
case '>=':
1517-
return $first >= $second;
1518-
case '=':
1519-
return $first == $second;
1520-
default:
1521-
throw new InvalidArgumentException();
1522-
}
1502+
return match ($operator) {
1503+
'<' => $first < $second,
1504+
'>' => $first > $second,
1505+
'<=' => $first <= $second,
1506+
'>=' => $first >= $second,
1507+
'=' => $first == $second,
1508+
default => throw new InvalidArgumentException(),
1509+
};
15231510
}
15241511

15251512
/**

src/Contract/Rule.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ interface Rule
1515
{
1616
/**
1717
* Determine if the validation rule passes.
18-
*
19-
* @param mixed $value
2018
*/
21-
public function passes(string $attribute, $value): bool;
19+
public function passes(string $attribute, mixed $value): bool;
2220

2321
/**
2422
* Get the validation error message.
25-
*
26-
* @return array|string
2723
*/
28-
public function message();
24+
public function message(): array|string;
2925
}

src/Contract/ValidatorFactoryInterface.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@ public function make(array $data, array $rules, array $messages = [], array $cus
2222

2323
/**
2424
* Register a custom validator extension.
25-
*
26-
* @param \Closure|string $extension
2725
*/
28-
public function extend(string $rule, $extension, ?string $message = null);
26+
public function extend(string $rule, \Closure|string $extension, ?string $message = null);
2927

3028
/**
3129
* Register a custom implicit validator extension.
32-
*
33-
* @param \Closure|string $extension
3430
*/
35-
public function extendImplicit(string $rule, $extension, ?string $message = null);
31+
public function extendImplicit(string $rule, \Closure|string $extension, ?string $message = null);
3632

3733
/**
3834
* Register a custom implicit validator message replacer.
39-
*
40-
* @param \Closure|string $replacer
4135
*/
42-
public function replacer(string $rule, $replacer);
36+
public function replacer(string $rule, \Closure|string $replacer);
4337
}

src/DatabasePresenceVerifier.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818

1919
class DatabasePresenceVerifier implements PresenceVerifierInterface
2020
{
21-
/**
22-
* The database connection instance.
23-
*
24-
* @var \Hyperf\Database\ConnectionResolverInterface
25-
*/
26-
protected $db;
27-
2821
/**
2922
* The database connection to use.
3023
*
@@ -34,10 +27,11 @@ class DatabasePresenceVerifier implements PresenceVerifierInterface
3427

3528
/**
3629
* Create a new database presence verifier.
30+
*
31+
* @param ConnectionResolverInterface $db the database connection instance
3732
*/
38-
public function __construct(ConnectionResolverInterface $db)
33+
public function __construct(protected ConnectionResolverInterface $db)
3934
{
40-
$this->db = $db;
4135
}
4236

4337
/**

src/Event/ValidatorFactoryResolved.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@
1515

1616
class ValidatorFactoryResolved
1717
{
18-
/**
19-
* @var ValidatorFactoryInterface
20-
*/
21-
public $validatorFactory;
22-
23-
public function __construct(ValidatorFactoryInterface $validatorFactory)
18+
public function __construct(public ValidatorFactoryInterface $validatorFactory)
2419
{
25-
$this->validatorFactory = $validatorFactory;
2620
}
2721
}

src/Middleware/ValidationMiddleware.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,10 @@
2727

2828
class ValidationMiddleware implements MiddlewareInterface
2929
{
30-
/**
31-
* @var \Psr\Container\ContainerInterface
32-
*/
33-
private $container;
34-
35-
/**
36-
* @var array
37-
*/
38-
private $implements = [];
30+
private array $implements = [];
3931

40-
public function __construct(ContainerInterface $container)
32+
public function __construct(private ContainerInterface $container)
4133
{
42-
$this->container = $container;
4334
}
4435

4536
/**
@@ -98,9 +89,7 @@ public function isImplementedValidatesWhenResolved(string $classname): bool
9889
*/
9990
protected function handleUnauthorizedException(UnauthorizedException $exception): ResponseInterface
10091
{
101-
return Context::override(ResponseInterface::class, function (ResponseInterface $response) {
102-
return $response->withStatus(403);
103-
});
92+
return Context::override(ResponseInterface::class, fn (ResponseInterface $response) => $response->withStatus(403));
10493
}
10594

10695
protected function shouldHandle(Dispatched $dispatched): bool
@@ -110,12 +99,11 @@ protected function shouldHandle(Dispatched $dispatched): bool
11099

111100
/**
112101
* @see \Hyperf\HttpServer\CoreMiddleware::prepareHandler()
113-
* @param array|string $handler
114102
*/
115-
protected function prepareHandler($handler): array
103+
protected function prepareHandler(array|string $handler): array
116104
{
117105
if (is_string($handler)) {
118-
if (strpos($handler, '@') !== false) {
106+
if (str_contains($handler, '@')) {
119107
return explode('@', $handler);
120108
}
121109
$array = explode('::', $handler);

src/Request/FormRequest.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,15 @@ class FormRequest extends Request implements ValidatesWhenResolved
2626
{
2727
use ValidatesWhenResolvedTrait;
2828

29-
/**
30-
* The container instance.
31-
*
32-
* @var ContainerInterface
33-
*/
34-
protected $container;
35-
3629
/**
3730
* The key to be used for the view error bag.
38-
*
39-
* @var string
4031
*/
41-
protected $errorBag = 'default';
32+
protected string $errorBag = 'default';
4233

4334
/**
4435
* The scenes defined by developer.
45-
*
46-
* @var array
4736
*/
48-
protected $scenes = [];
37+
protected array $scenes = [];
4938

5039
/**
5140
* The input keys that should not be flashed on redirect.
@@ -54,15 +43,11 @@ class FormRequest extends Request implements ValidatesWhenResolved
5443
*/
5544
protected $dontFlash = ['password', 'password_confirmation'];
5645

57-
public function __construct(ContainerInterface $container)
46+
public function __construct(protected ContainerInterface $container)
5847
{
59-
$this->setContainer($container);
6048
}
6149

62-
/**
63-
* @return $this
64-
*/
65-
public function scene(string $scene)
50+
public function scene(string $scene): static
6651
{
6752
Context::set($this->getContextValidatorKey('scene'), $scene);
6853
return $this;
@@ -110,10 +95,8 @@ public function attributes(): array
11095

11196
/**
11297
* Set the container implementation.
113-
*
114-
* @return $this
11598
*/
116-
public function setContainer(ContainerInterface $container)
99+
public function setContainer(ContainerInterface $container): static
117100
{
118101
$this->container = $container;
119102

@@ -212,7 +195,7 @@ protected function getContextValidatorKey(string $key): string
212195
/**
213196
* Get scene rules.
214197
*/
215-
protected function getRules()
198+
protected function getRules(): array
216199
{
217200
$rules = call_user_func_array([$this, 'rules'], []);
218201
$scene = $this->getScene();

0 commit comments

Comments
 (0)