Skip to content

Commit 6ba40da

Browse files
authored
Merge pull request #5007 from samsonasik/apply-rector-dead-code-set-list
[Rector] Apply Rector dead code set list
2 parents 381748a + 377eddb commit 6ba40da

File tree

31 files changed

+104
-125
lines changed

31 files changed

+104
-125
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"phpstan/phpstan": "^0.12.91",
2424
"phpunit/phpunit": "^9.1",
2525
"predis/predis": "^1.1",
26-
"rector/rector": "0.11.47",
26+
"rector/rector": "0.11.48",
2727
"symplify/package-builder": "^9.3"
2828
},
2929
"suggest": {

rector.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
2828
use Rector\Core\Configuration\Option;
2929
use Rector\Core\ValueObject\PhpVersion;
30-
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
31-
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
32-
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
33-
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
34-
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
35-
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
36-
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
30+
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
31+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
32+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
33+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
34+
use Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector;
35+
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
36+
use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector;
37+
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
3738
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
3839
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
3940
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
@@ -51,6 +52,7 @@
5152
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;
5253

5354
return static function (ContainerConfigurator $containerConfigurator): void {
55+
$containerConfigurator->import(SetList::DEAD_CODE);
5456
$containerConfigurator->import(SetList::PHP_73);
5557

5658
$parameters = $containerConfigurator->parameters();
@@ -72,6 +74,43 @@
7274
__DIR__ . '/tests/_support',
7375
JsonThrowOnErrorRector::class,
7476
StringifyStrNeedlesRector::class,
77+
78+
// requires php 8
79+
RemoveUnusedPromotedPropertyRector::class,
80+
81+
// currently buggy on call inside assign, wait for next release
82+
RemoveParentCallWithoutParentRector::class,
83+
84+
// private method called via getPrivateMethodInvoker
85+
RemoveUnusedPrivateMethodRector::class => [
86+
__DIR__ . '/system/Entity/Entity.php',
87+
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
88+
],
89+
90+
// call on purpose for nothing happen check
91+
RemoveEmptyMethodCallRector::class => [
92+
__DIR__ . '/tests',
93+
],
94+
95+
// currently buggy on class implements ArrayAccess, wait for next release
96+
RemoveDeadStmtRector::class => [
97+
__DIR__ . '/tests/system/Cookie/CookieTest.php',
98+
],
99+
100+
// check on constant compare
101+
UnwrapFutureCompatibleIfPhpVersionRector::class => [
102+
__DIR__ . '/system/CodeIgniter.php',
103+
],
104+
105+
// check context ResponseTrait
106+
RemoveUselessReturnTagRector::class => [
107+
__DIR__ . '/system/HTTP/MessageTrait.php',
108+
],
109+
110+
// casted to Entity via EntityTest->getCastEntity()
111+
RecastingRemovalRector::class => [
112+
__DIR__ . '/tests/system/Entity/EntityTest.php',
113+
],
75114
]);
76115

77116
// auto import fully qualified class names
@@ -92,27 +131,20 @@
92131
$services->set(SimplifyStrposLowerRector::class);
93132
$services->set(CombineIfRector::class);
94133
$services->set(SimplifyIfReturnBoolRector::class);
95-
$services->set(RemoveDuplicatedCaseInSwitchRector::class);
96134
$services->set(InlineIfToExplicitIfRector::class);
97135
$services->set(PreparedValueToEarlyReturnRector::class);
98136
$services->set(ShortenElseIfRector::class);
99-
$services->set(RemoveUnusedForeachKeyRector::class);
100137
$services->set(SimplifyIfElseToTernaryRector::class);
101138
$services->set(UnusedForeachValueToArrayKeysRector::class);
102-
$services->set(RemoveConcatAutocastRector::class);
103139
$services->set(ChangeArrayPushToArrayAssignRector::class);
104140
$services->set(UnnecessaryTernaryExpressionRector::class);
105-
$services->set(RemoveUnusedPrivatePropertyRector::class);
106141
$services->set(RemoveErrorSuppressInTryCatchStmtsRector::class);
107142
$services->set(TernaryToNullCoalescingRector::class);
108143
$services->set(ListToArrayDestructRector::class);
109144
$services->set(RemoveVarTagFromClassConstantRector::class);
110145
$services->set(AddPregQuoteDelimiterRector::class);
111146
$services->set(SimplifyRegexPatternRector::class);
112147
$services->set(RemoveExtraParametersRector::class);
113-
$services->set(RemoveUnusedVariableAssignRector::class);
114148
$services->set(FuncGetArgsToVariadicParamRector::class);
115149
$services->set(MakeInheritedMethodVisibilitySameAsParentRector::class);
116-
$services->set(RemoveDuplicatedArrayKeyRector::class);
117-
$services->set(RemoveDoubleAssignRector::class);
118150
};

system/Database/Forge.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -996,20 +996,18 @@ protected function _processForeignKeys(string $table): string
996996
'SET DEFAULT',
997997
];
998998

999-
if ($this->foreignKeys !== []) {
1000-
foreach ($this->foreignKeys as $field => $fkey) {
1001-
$nameIndex = $table . '_' . $field . '_foreign';
999+
foreach ($this->foreignKeys as $field => $fkey) {
1000+
$nameIndex = $table . '_' . $field . '_foreign';
10021001

1003-
$sql .= ",\n\tCONSTRAINT " . $this->db->escapeIdentifiers($nameIndex)
1004-
. ' FOREIGN KEY(' . $this->db->escapeIdentifiers($field) . ') REFERENCES ' . $this->db->escapeIdentifiers($this->db->DBPrefix . $fkey['table']) . ' (' . $this->db->escapeIdentifiers($fkey['field']) . ')';
1002+
$sql .= ",\n\tCONSTRAINT " . $this->db->escapeIdentifiers($nameIndex)
1003+
. ' FOREIGN KEY(' . $this->db->escapeIdentifiers($field) . ') REFERENCES ' . $this->db->escapeIdentifiers($this->db->DBPrefix . $fkey['table']) . ' (' . $this->db->escapeIdentifiers($fkey['field']) . ')';
10051004

1006-
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
1007-
$sql .= ' ON DELETE ' . $fkey['onDelete'];
1008-
}
1005+
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
1006+
$sql .= ' ON DELETE ' . $fkey['onDelete'];
1007+
}
10091008

1010-
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
1011-
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
1012-
}
1009+
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
1010+
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
10131011
}
10141012
}
10151013

system/Database/MySQLi/Result.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function getFieldData(): array
8989
$retVal[$i]->type = $data->type;
9090
$retVal[$i]->type_name = in_array($data->type, [1, 247], true) ? 'char' : ($dataTypes[$data->type] ?? null);
9191
$retVal[$i]->max_length = $data->max_length;
92-
$retVal[$i]->primary_key = (int) ($data->flags & 2);
92+
$retVal[$i]->primary_key = $data->flags & 2;
9393
$retVal[$i]->length = $data->length;
9494
$retVal[$i]->default = $data->def;
9595
}

system/Database/SQLSRV/Connection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,23 +354,23 @@ protected function _fieldData(string $table): array
354354
*/
355355
protected function _transBegin(): bool
356356
{
357-
return (bool) sqlsrv_begin_transaction($this->connID);
357+
return sqlsrv_begin_transaction($this->connID);
358358
}
359359

360360
/**
361361
* Commit Transaction
362362
*/
363363
protected function _transCommit(): bool
364364
{
365-
return (bool) sqlsrv_commit($this->connID);
365+
return sqlsrv_commit($this->connID);
366366
}
367367

368368
/**
369369
* Rollback Transaction
370370
*/
371371
protected function _transRollback(): bool
372372
{
373-
return (bool) sqlsrv_rollback($this->connID);
373+
return sqlsrv_rollback($this->connID);
374374
}
375375

376376
/**

system/Database/SQLSRV/Forge.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,19 @@ protected function _processForeignKeys(string $table): string
224224

225225
$allowActions = ['CASCADE', 'SET NULL', 'NO ACTION', 'RESTRICT', 'SET DEFAULT'];
226226

227-
if ($this->foreignKeys !== []) {
228-
foreach ($this->foreignKeys as $field => $fkey) {
229-
$nameIndex = $table . '_' . $field . '_foreign';
227+
foreach ($this->foreignKeys as $field => $fkey) {
228+
$nameIndex = $table . '_' . $field . '_foreign';
230229

231-
$sql .= ",\n\t CONSTRAINT " . $this->db->escapeIdentifiers($nameIndex)
232-
. ' FOREIGN KEY (' . $this->db->escapeIdentifiers($field) . ') '
233-
. ' REFERENCES ' . $this->db->escapeIdentifiers($this->db->getPrefix() . $fkey['table']) . ' (' . $this->db->escapeIdentifiers($fkey['field']) . ')';
230+
$sql .= ",\n\t CONSTRAINT " . $this->db->escapeIdentifiers($nameIndex)
231+
. ' FOREIGN KEY (' . $this->db->escapeIdentifiers($field) . ') '
232+
. ' REFERENCES ' . $this->db->escapeIdentifiers($this->db->getPrefix() . $fkey['table']) . ' (' . $this->db->escapeIdentifiers($fkey['field']) . ')';
234233

235-
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
236-
$sql .= ' ON DELETE ' . $fkey['onDelete'];
237-
}
234+
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
235+
$sql .= ' ON DELETE ' . $fkey['onDelete'];
236+
}
238237

239-
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
240-
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
241-
}
238+
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
239+
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
242240
}
243241
}
244242

system/Debug/Toolbar.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,14 @@ public function __construct(ToolbarConfig $config)
6666
/**
6767
* Returns all the data required by Debug Bar
6868
*
69-
* @param float $startTime App start time
69+
* @param float $startTime App start time
70+
* @param IncomingRequest $request
71+
* @param Response $response
7072
*
7173
* @return string JSON encoded data
7274
*/
7375
public function run(float $startTime, float $totalTime, RequestInterface $request, ResponseInterface $response): string
7476
{
75-
/**
76-
* @var IncomingRequest $request
77-
* @var Response $response
78-
*/
79-
8077
// Data items used within the view.
8178
$data['url'] = current_url();
8279
$data['method'] = $request->getMethod(true);

system/Debug/Toolbar/Collectors/Files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Files extends BaseCollector
4545
*/
4646
public function getTitleDetails(): string
4747
{
48-
return '( ' . (int) count(get_included_files()) . ' )';
48+
return '( ' . count(get_included_files()) . ' )';
4949
}
5050

5151
/**

system/Events/Events.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public static function initialize()
7171
return;
7272
}
7373

74-
/**
75-
* @var Modules
76-
*/
74+
/** @var Modules $config */
7775
$config = config('Modules');
7876
$events = APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'Events.php';
7977
$files = [];

system/Filters/Filters.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ public function initialize(?string $uri = null)
254254
* Restores instance to its pre-initialized state.
255255
* Most useful for testing so the service can be
256256
* re-initialized to a different path.
257-
*
258-
* @return $this
259257
*/
260258
public function reset(): self
261259
{
@@ -427,8 +425,6 @@ protected function processMethods()
427425

428426
if (array_key_exists($method, $this->config->methods)) {
429427
$this->filters['before'] = array_merge($this->filters['before'], $this->config->methods[$method]);
430-
431-
return;
432428
}
433429
}
434430

system/HTTP/URI.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,9 +997,6 @@ public function resolveRelativeURI(string $uri)
997997
* Section 5.2
998998
*
999999
* @see http://tools.ietf.org/html/rfc3986#section-5.2.3
1000-
*
1001-
* @param URI $base
1002-
* @param URI $reference
10031000
*/
10041001
protected function mergePaths(self $base, self $reference): string
10051002
{

system/Helpers/html_helper.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,8 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a
304304

305305
$video .= ">\n";
306306

307-
if (! empty($tracks)) {
308-
foreach ($tracks as $track) {
309-
$video .= _space_indent() . $track . "\n";
310-
}
307+
foreach ($tracks as $track) {
308+
$video .= _space_indent() . $track . "\n";
311309
}
312310

313311
if (! empty($unsupportedMessage)) {
@@ -352,10 +350,8 @@ function audio($src, string $unsupportedMessage = '', string $attributes = '', a
352350

353351
$audio .= '>';
354352

355-
if (! empty($tracks)) {
356-
foreach ($tracks as $track) {
357-
$audio .= "\n" . _space_indent() . $track;
358-
}
353+
foreach ($tracks as $track) {
354+
$audio .= "\n" . _space_indent() . $track;
359355
}
360356

361357
if (! empty($unsupportedMessage)) {
@@ -388,10 +384,8 @@ function _media(string $name, array $types = [], string $unsupportedMessage = ''
388384
$media .= _space_indent() . $option . "\n";
389385
}
390386

391-
if (! empty($tracks)) {
392-
foreach ($tracks as $track) {
393-
$media .= _space_indent() . $track . "\n";
394-
}
387+
foreach ($tracks as $track) {
388+
$media .= _space_indent() . $track . "\n";
395389
}
396390

397391
if (! empty($unsupportedMessage)) {

system/Helpers/text_helper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,8 @@ function word_wrap(string $str, int $charlim = 76): string
395395
}
396396

397397
// Put our markers back
398-
if (! empty($unwrap)) {
399-
foreach ($unwrap as $key => $val) {
400-
$output = str_replace('{{unwrapped' . $key . '}}', $val, $output);
401-
}
398+
foreach ($unwrap as $key => $val) {
399+
$output = str_replace('{{unwrapped' . $key . '}}', $val, $output);
402400
}
403401

404402
// remove any trailing newline

system/Log/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public function log($level, $message, array $context = []): bool
282282
}
283283

284284
/**
285-
* @var HandlerInterface
285+
* @var HandlerInterface $handler
286286
*/
287287
$handler = $this->handlers[$className];
288288

system/Pager/Pager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,6 @@ public function getDetails(string $group = 'default'): array
363363

364364
/**
365365
* Sets only allowed queries on pagination links.
366-
*
367-
* @return Pager
368366
*/
369367
public function only(array $queries): self
370368
{

system/Pager/PagerRenderer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function links(): array
239239
$uri = $this->segment === 0 ? $uri->addQuery($this->pageSelector, $i) : $uri->setSegment($this->segment, $i);
240240
$links[] = [
241241
'uri' => URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment()),
242-
'title' => (int) $i,
242+
'title' => $i,
243243
'active' => ($i === $this->current),
244244
];
245245
}
@@ -260,8 +260,8 @@ protected function updatePages(?int $count = null)
260260
return;
261261
}
262262

263-
$this->first = $this->current - $count > 0 ? (int) ($this->current - $count) : 1;
264-
$this->last = $this->current + $count <= $this->pageCount ? (int) ($this->current + $count) : (int) $this->pageCount;
263+
$this->first = $this->current - $count > 0 ? $this->current - $count : 1;
264+
$this->last = $this->current + $count <= $this->pageCount ? $this->current + $count : (int) $this->pageCount;
265265
}
266266

267267
/**

system/Router/RouteCollectionInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ public function setTranslateURIDashes(bool $value);
9292
* defined routes.
9393
*
9494
* If FALSE, will stop searching and do NO automatic routing.
95-
*
96-
* @return RouteCollectionInterface
9795
*/
9896
public function setAutoRoute(bool $value): self;
9997

@@ -105,8 +103,6 @@ public function setAutoRoute(bool $value): self;
105103
* This setting is passed to the Router class and handled there.
106104
*
107105
* @param callable|null $callable
108-
*
109-
* @return RouteCollectionInterface
110106
*/
111107
public function set404Override($callable = null): self;
112108

0 commit comments

Comments
 (0)