Skip to content

Commit 1528d3d

Browse files
authored
Merge pull request #9187 from samsonasik/refactor-enable-closure-return
refactor: enable ClosureReturnTypeRector
2 parents 092d85b + 8ecbf0d commit 1528d3d

File tree

21 files changed

+32
-30
lines changed

21 files changed

+32
-30
lines changed

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
5454
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
5555
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;
56+
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
5657
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
5758
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
5859
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
@@ -217,6 +218,7 @@
217218
AddFunctionVoidReturnTypeWhereNoReturnRector::class,
218219
AddMethodCallBasedStrictParamTypeRector::class,
219220
TypedPropertyFromAssignsRector::class,
221+
ClosureReturnTypeRector::class,
220222
])
221223
->withConfiguredRule(StringClassNameToClassConstantRector::class, [
222224
// keep '\\' prefix string on string '\Foo\Bar'

system/Database/OCI8/Builder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ protected function _replace(string $table, array $keys, array $values): string
109109
{
110110
$fieldNames = array_map(static fn ($columnName) => trim($columnName, '"'), $keys);
111111

112-
$uniqueIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames) {
112+
$uniqueIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames): bool {
113113
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
114114

115115
return ($index->type === 'PRIMARY') && $hasAllFields;
116116
});
117-
$replaceableFields = array_filter($keys, static function ($columnName) use ($uniqueIndexes) {
117+
$replaceableFields = array_filter($keys, static function ($columnName) use ($uniqueIndexes): bool {
118118
foreach ($uniqueIndexes as $index) {
119119
if (in_array(trim($columnName, '"'), $index->fields, true)) {
120120
return false;
@@ -344,7 +344,7 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
344344
if (empty($constraints)) {
345345
$fieldNames = array_map(static fn ($columnName) => trim($columnName, '"'), $keys);
346346

347-
$uniqueIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames) {
347+
$uniqueIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames): bool {
348348
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
349349

350350
return ($index->type === 'PRIMARY' || $index->type === 'UNIQUE') && $hasAllFields;

system/Database/OCI8/PreparedQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function parameterize(string $sql): string
113113
// Track our current value
114114
$count = 0;
115115

116-
return preg_replace_callback('/\?/', static function ($matches) use (&$count) {
116+
return preg_replace_callback('/\?/', static function ($matches) use (&$count): string {
117117
return ':' . ($count++);
118118
}, $sql);
119119
}

system/Database/Postgre/Builder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
368368
$sql .= 'WHERE ' . implode(
369369
' AND ',
370370
array_map(
371-
static function ($key, $value) use ($table, $alias, $that) {
371+
static function ($key, $value) use ($table, $alias, $that): string|RawSql {
372372
if ($value instanceof RawSql && is_string($key)) {
373373
return $table . '.' . $key . ' = ' . $value;
374374
}
@@ -463,7 +463,7 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
463463
$constraints = $this->QBOptions['constraints'] ?? [];
464464

465465
if (empty($constraints)) {
466-
$allIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames) {
466+
$allIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames): bool {
467467
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
468468

469469
return ($index->type === 'UNIQUE' || $index->type === 'PRIMARY') && $hasAllFields;
@@ -575,7 +575,7 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
575575
$sql .= 'WHERE ' . implode(
576576
' AND ',
577577
array_map(
578-
static function ($key, $value) use ($table, $alias, $that) {
578+
static function ($key, $value) use ($table, $alias, $that): RawSql|string {
579579
if ($value instanceof RawSql) {
580580
return $value;
581581
}

system/Database/Postgre/PreparedQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function parameterize(string $sql): string
119119
// Track our current value
120120
$count = 0;
121121

122-
return preg_replace_callback('/\?/', static function () use (&$count) {
122+
return preg_replace_callback('/\?/', static function () use (&$count): string {
123123
$count++;
124124

125125
return "\${$count}";

system/Database/SQLSRV/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,15 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
699699
if (empty($constraints)) {
700700
$tableIndexes = $this->db->getIndexData($table);
701701

702-
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames) {
702+
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames): bool {
703703
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
704704

705705
return $index->type === 'PRIMARY' && $hasAllFields;
706706
});
707707

708708
// if no primary found then look for unique - since indexes have no order
709709
if ($uniqueIndexes === []) {
710-
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames) {
710+
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames): bool {
711711
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
712712

713713
return $index->type === 'UNIQUE' && $hasAllFields;

system/Database/SQLite3/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
145145
if (empty($constraints)) {
146146
$fieldNames = array_map(static fn ($columnName) => trim($columnName, '`'), $keys);
147147

148-
$allIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames) {
148+
$allIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames): bool {
149149
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
150150

151151
return ($index->type === 'PRIMARY' || $index->type === 'UNIQUE') && $hasAllFields;

system/Debug/Toolbar/Collectors/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function formatTimelineData(): array
148148
public function display(): array
149149
{
150150
$data = [];
151-
$data['queries'] = array_map(static function (array $query) {
151+
$data['queries'] = array_map(static function (array $query): array {
152152
$isDuplicate = $query['duplicate'] === true;
153153

154154
$firstNonSystemLine = '';

system/Events/Events.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function initialize()
8484
$files = service('locator')->search('Config/Events.php');
8585
}
8686

87-
$files = array_filter(array_map(static function (string $file) {
87+
$files = array_filter(array_map(static function (string $file): false|string {
8888
if (is_file($file)) {
8989
return realpath($file) ?: $file;
9090
}

system/HTTP/ContentSecurityPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ protected function generateNonces(ResponseInterface $response)
713713
$pattern = '/(' . preg_quote($this->styleNonceTag, '/')
714714
. '|' . preg_quote($this->scriptNonceTag, '/') . ')/';
715715

716-
$body = preg_replace_callback($pattern, function ($match) {
716+
$body = preg_replace_callback($pattern, function ($match): string {
717717
$nonce = $match[0] === $this->styleNonceTag ? $this->getStyleNonce() : $this->getScriptNonce();
718718

719719
return "nonce=\"{$nonce}\"";

system/HTTP/Negotiate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public function parseHeader(string $header): array
233233
}
234234

235235
// Sort to get the highest results first
236-
usort($results, static function ($a, $b) {
236+
usort($results, static function ($a, $b): int {
237237
if ($a['q'] === $b['q']) {
238238
$aAst = substr_count($a['value'], '*');
239239
$bAst = substr_count($b['value'], '*');

system/Helpers/Array/ArrayHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public static function recursiveCount(array $array, int $counter = 0): int
307307
*/
308308
public static function sortValuesByNatural(array &$array, $sortByIndex = null): bool
309309
{
310-
return usort($array, static function ($currentValue, $nextValue) use ($sortByIndex) {
310+
return usort($array, static function ($currentValue, $nextValue) use ($sortByIndex): int {
311311
if ($sortByIndex !== null) {
312312
return strnatcmp((string) $currentValue[$sortByIndex], (string) $nextValue[$sortByIndex]);
313313
}

system/Router/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ protected function checkRoutes(string $uri): bool
433433
// Is this route supposed to redirect to another?
434434
if ($this->collection->isRedirect($routeKey)) {
435435
// replacing matched route groups with references: post/([0-9]+) -> post/$1
436-
$redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function () {
436+
$redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function (): string {
437437
static $i = 1;
438438

439439
return '$' . $i++;

system/View/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ protected function replaceSingle($pattern, $content, $template, bool $escape = f
534534
$content = (string) $content;
535535

536536
// Replace the content in the template
537-
return preg_replace_callback($pattern, function ($matches) use ($content, $escape) {
537+
return preg_replace_callback($pattern, function ($matches) use ($content, $escape): string {
538538
// Check for {! !} syntax to not escape this one.
539539
if (
540540
str_starts_with($matches[0], $this->leftDelimiter . '!')

tests/system/Commands/Utilities/NamespacesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function tearDown(): void
4242
*/
4343
protected function getBuffer()
4444
{
45-
return preg_replace_callback('/(\|\s*[^|]+\s*\|\s*)(.*?)(\s*\|\s*[^|]+\s*\|)/', static function (array $matches) {
45+
return preg_replace_callback('/(\|\s*[^|]+\s*\|\s*)(.*?)(\s*\|\s*[^|]+\s*\|)/', static function (array $matches): string {
4646
$matches[2] = str_replace(DIRECTORY_SEPARATOR, '/', $matches[2]);
4747

4848
return $matches[1] . $matches[2] . $matches[3];

tests/system/DataConverter/DataConverterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ public function testReconstructObjectWithClosure(): void
570570
'created_at' => 'datetime',
571571
'updated_at' => 'datetime',
572572
];
573-
$reconstructor = static function ($array) {
573+
$reconstructor = static function ($array): User {
574574
$user = new User();
575575
$user->fill($array);
576576

@@ -667,7 +667,7 @@ public function testExtractWithClosure(): void
667667
'created_at' => 'datetime',
668668
'updated_at' => 'datetime',
669669
];
670-
$extractor = static function ($obj) {
670+
$extractor = static function ($obj): array {
671671
$array['id'] = $obj->id;
672672
$array['name'] = $obj->name;
673673
$array['created_at'] = $obj->created_at;

tests/system/Debug/TimerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function testRecordFunctionNoReturn(): void
144144
public function testRecordFunctionWithReturn(): void
145145
{
146146
$timer = new Timer();
147-
$returnValue = $timer->record('longjohn', static function () {
147+
$returnValue = $timer->record('longjohn', static function (): string {
148148
usleep(100000);
149149

150150
return 'test';

tests/system/Events/EventsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function testCancelEvent(): void
121121

122122
// This should cancel the flow of events, and leave
123123
// $result = 1.
124-
Events::on('foo', static function ($arg) use (&$result) {
124+
Events::on('foo', static function ($arg) use (&$result): bool {
125125
$result = 1;
126126

127127
return false;
@@ -138,14 +138,14 @@ public function testPriority(): void
138138
{
139139
$result = 0;
140140

141-
Events::on('foo', static function () use (&$result) {
141+
Events::on('foo', static function () use (&$result): bool {
142142
$result = 1;
143143

144144
return false;
145145
}, EVENT_PRIORITY_NORMAL);
146146
// Since this has a higher priority, it will
147147
// run first.
148-
Events::on('foo', static function () use (&$result) {
148+
Events::on('foo', static function () use (&$result): bool {
149149
$result = 2;
150150

151151
return false;

tests/system/Test/FeatureTestTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function testCallValidationTwice(): void
152152
[
153153
'POST',
154154
'section/create',
155-
static function () {
155+
static function (): string {
156156
$validation = Services::validation();
157157
$validation->setRule('title', 'title', 'required|min_length[3]');
158158

tests/system/Validation/ValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function testClosureRuleWithParamError(): void
311311
$this->validation->setRules([
312312
'foo' => [
313313
'required',
314-
static function ($value, $data, &$error, $field) {
314+
static function ($value, $data, &$error, $field): bool {
315315
if ($value !== 'abc') {
316316
$error = 'The ' . $field . ' value is not "abc"';
317317

tests/system/View/ParserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public function testParserPluginClosure(): void
806806

807807
public function testParserPluginParams(): void
808808
{
809-
$this->parser->addPlugin('growth', static function ($str, array $params) {
809+
$this->parser->addPlugin('growth', static function ($str, array $params): string {
810810
$step = $params['step'] ?? 1;
811811
$count = $params['count'] ?? 2;
812812

@@ -853,7 +853,7 @@ public function testParserSingleTagWithSingleParams(): void
853853

854854
public function testParserSingleTagWithQuotedParams(): void
855855
{
856-
$this->parser->addPlugin('count', static function (array $params = []) {
856+
$this->parser->addPlugin('count', static function (array $params = []): string {
857857
$out = '';
858858

859859
foreach ($params as $index => $param) {
@@ -870,7 +870,7 @@ public function testParserSingleTagWithQuotedParams(): void
870870

871871
public function testParserSingleTagWithNamedParams(): void
872872
{
873-
$this->parser->addPlugin('read_params', static function (array $params = []) {
873+
$this->parser->addPlugin('read_params', static function (array $params = []): string {
874874
$out = '';
875875

876876
foreach ($params as $index => $param) {

0 commit comments

Comments
 (0)