Skip to content

Commit

Permalink
Merge pull request #5007 from samsonasik/apply-rector-dead-code-set-list
Browse files Browse the repository at this point in the history
[Rector] Apply Rector dead code set list
  • Loading branch information
samsonasik authored Aug 17, 2021
2 parents 381748a + 377eddb commit 6ba40da
Show file tree
Hide file tree
Showing 31 changed files with 104 additions and 125 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"phpstan/phpstan": "^0.12.91",
"phpunit/phpunit": "^9.1",
"predis/predis": "^1.1",
"rector/rector": "0.11.47",
"rector/rector": "0.11.48",
"symplify/package-builder": "^9.3"
},
"suggest": {
Expand Down
60 changes: 46 additions & 14 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector;
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector;
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
Expand All @@ -51,6 +52,7 @@
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(SetList::DEAD_CODE);
$containerConfigurator->import(SetList::PHP_73);

$parameters = $containerConfigurator->parameters();
Expand All @@ -72,6 +74,43 @@
__DIR__ . '/tests/_support',
JsonThrowOnErrorRector::class,
StringifyStrNeedlesRector::class,

// requires php 8
RemoveUnusedPromotedPropertyRector::class,

// currently buggy on call inside assign, wait for next release
RemoveParentCallWithoutParentRector::class,

// private method called via getPrivateMethodInvoker
RemoveUnusedPrivateMethodRector::class => [
__DIR__ . '/system/Entity/Entity.php',
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
],

// call on purpose for nothing happen check
RemoveEmptyMethodCallRector::class => [
__DIR__ . '/tests',
],

// currently buggy on class implements ArrayAccess, wait for next release
RemoveDeadStmtRector::class => [
__DIR__ . '/tests/system/Cookie/CookieTest.php',
],

// check on constant compare
UnwrapFutureCompatibleIfPhpVersionRector::class => [
__DIR__ . '/system/CodeIgniter.php',
],

// check context ResponseTrait
RemoveUselessReturnTagRector::class => [
__DIR__ . '/system/HTTP/MessageTrait.php',
],

// casted to Entity via EntityTest->getCastEntity()
RecastingRemovalRector::class => [
__DIR__ . '/tests/system/Entity/EntityTest.php',
],
]);

// auto import fully qualified class names
Expand All @@ -92,27 +131,20 @@
$services->set(SimplifyStrposLowerRector::class);
$services->set(CombineIfRector::class);
$services->set(SimplifyIfReturnBoolRector::class);
$services->set(RemoveDuplicatedCaseInSwitchRector::class);
$services->set(InlineIfToExplicitIfRector::class);
$services->set(PreparedValueToEarlyReturnRector::class);
$services->set(ShortenElseIfRector::class);
$services->set(RemoveUnusedForeachKeyRector::class);
$services->set(SimplifyIfElseToTernaryRector::class);
$services->set(UnusedForeachValueToArrayKeysRector::class);
$services->set(RemoveConcatAutocastRector::class);
$services->set(ChangeArrayPushToArrayAssignRector::class);
$services->set(UnnecessaryTernaryExpressionRector::class);
$services->set(RemoveUnusedPrivatePropertyRector::class);
$services->set(RemoveErrorSuppressInTryCatchStmtsRector::class);
$services->set(TernaryToNullCoalescingRector::class);
$services->set(ListToArrayDestructRector::class);
$services->set(RemoveVarTagFromClassConstantRector::class);
$services->set(AddPregQuoteDelimiterRector::class);
$services->set(SimplifyRegexPatternRector::class);
$services->set(RemoveExtraParametersRector::class);
$services->set(RemoveUnusedVariableAssignRector::class);
$services->set(FuncGetArgsToVariadicParamRector::class);
$services->set(MakeInheritedMethodVisibilitySameAsParentRector::class);
$services->set(RemoveDuplicatedArrayKeyRector::class);
$services->set(RemoveDoubleAssignRector::class);
};
20 changes: 9 additions & 11 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,20 +996,18 @@ protected function _processForeignKeys(string $table): string
'SET DEFAULT',
];

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

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

if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
$sql .= ' ON DELETE ' . $fkey['onDelete'];
}
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
$sql .= ' ON DELETE ' . $fkey['onDelete'];
}

if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
}
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
}
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getFieldData(): array
$retVal[$i]->type = $data->type;
$retVal[$i]->type_name = in_array($data->type, [1, 247], true) ? 'char' : ($dataTypes[$data->type] ?? null);
$retVal[$i]->max_length = $data->max_length;
$retVal[$i]->primary_key = (int) ($data->flags & 2);
$retVal[$i]->primary_key = $data->flags & 2;
$retVal[$i]->length = $data->length;
$retVal[$i]->default = $data->def;
}
Expand Down
6 changes: 3 additions & 3 deletions system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,23 @@ protected function _fieldData(string $table): array
*/
protected function _transBegin(): bool
{
return (bool) sqlsrv_begin_transaction($this->connID);
return sqlsrv_begin_transaction($this->connID);
}

/**
* Commit Transaction
*/
protected function _transCommit(): bool
{
return (bool) sqlsrv_commit($this->connID);
return sqlsrv_commit($this->connID);
}

/**
* Rollback Transaction
*/
protected function _transRollback(): bool
{
return (bool) sqlsrv_rollback($this->connID);
return sqlsrv_rollback($this->connID);
}

/**
Expand Down
22 changes: 10 additions & 12 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,19 @@ protected function _processForeignKeys(string $table): string

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

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

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

if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
$sql .= ' ON DELETE ' . $fkey['onDelete'];
}
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) {
$sql .= ' ON DELETE ' . $fkey['onDelete'];
}

if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
}
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true)) {
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
}
}

Expand Down
9 changes: 3 additions & 6 deletions system/Debug/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,14 @@ public function __construct(ToolbarConfig $config)
/**
* Returns all the data required by Debug Bar
*
* @param float $startTime App start time
* @param float $startTime App start time
* @param IncomingRequest $request
* @param Response $response
*
* @return string JSON encoded data
*/
public function run(float $startTime, float $totalTime, RequestInterface $request, ResponseInterface $response): string
{
/**
* @var IncomingRequest $request
* @var Response $response
*/

// Data items used within the view.
$data['url'] = current_url();
$data['method'] = $request->getMethod(true);
Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Toolbar/Collectors/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Files extends BaseCollector
*/
public function getTitleDetails(): string
{
return '( ' . (int) count(get_included_files()) . ' )';
return '( ' . count(get_included_files()) . ' )';
}

/**
Expand Down
4 changes: 1 addition & 3 deletions system/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public static function initialize()
return;
}

/**
* @var Modules
*/
/** @var Modules $config */
$config = config('Modules');
$events = APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'Events.php';
$files = [];
Expand Down
4 changes: 0 additions & 4 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ public function initialize(?string $uri = null)
* Restores instance to its pre-initialized state.
* Most useful for testing so the service can be
* re-initialized to a different path.
*
* @return $this
*/
public function reset(): self
{
Expand Down Expand Up @@ -427,8 +425,6 @@ protected function processMethods()

if (array_key_exists($method, $this->config->methods)) {
$this->filters['before'] = array_merge($this->filters['before'], $this->config->methods[$method]);

return;
}
}

Expand Down
3 changes: 0 additions & 3 deletions system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,6 @@ public function resolveRelativeURI(string $uri)
* Section 5.2
*
* @see http://tools.ietf.org/html/rfc3986#section-5.2.3
*
* @param URI $base
* @param URI $reference
*/
protected function mergePaths(self $base, self $reference): string
{
Expand Down
18 changes: 6 additions & 12 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,8 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a

$video .= ">\n";

if (! empty($tracks)) {
foreach ($tracks as $track) {
$video .= _space_indent() . $track . "\n";
}
foreach ($tracks as $track) {
$video .= _space_indent() . $track . "\n";
}

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

$audio .= '>';

if (! empty($tracks)) {
foreach ($tracks as $track) {
$audio .= "\n" . _space_indent() . $track;
}
foreach ($tracks as $track) {
$audio .= "\n" . _space_indent() . $track;
}

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

if (! empty($tracks)) {
foreach ($tracks as $track) {
$media .= _space_indent() . $track . "\n";
}
foreach ($tracks as $track) {
$media .= _space_indent() . $track . "\n";
}

if (! empty($unsupportedMessage)) {
Expand Down
6 changes: 2 additions & 4 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,8 @@ function word_wrap(string $str, int $charlim = 76): string
}

// Put our markers back
if (! empty($unwrap)) {
foreach ($unwrap as $key => $val) {
$output = str_replace('{{unwrapped' . $key . '}}', $val, $output);
}
foreach ($unwrap as $key => $val) {
$output = str_replace('{{unwrapped' . $key . '}}', $val, $output);
}

// remove any trailing newline
Expand Down
2 changes: 1 addition & 1 deletion system/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public function log($level, $message, array $context = []): bool
}

/**
* @var HandlerInterface
* @var HandlerInterface $handler
*/
$handler = $this->handlers[$className];

Expand Down
2 changes: 0 additions & 2 deletions system/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ public function getDetails(string $group = 'default'): array

/**
* Sets only allowed queries on pagination links.
*
* @return Pager
*/
public function only(array $queries): self
{
Expand Down
6 changes: 3 additions & 3 deletions system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function links(): array
$uri = $this->segment === 0 ? $uri->addQuery($this->pageSelector, $i) : $uri->setSegment($this->segment, $i);
$links[] = [
'uri' => URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment()),
'title' => (int) $i,
'title' => $i,
'active' => ($i === $this->current),
];
}
Expand All @@ -260,8 +260,8 @@ protected function updatePages(?int $count = null)
return;
}

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

/**
Expand Down
4 changes: 0 additions & 4 deletions system/Router/RouteCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public function setTranslateURIDashes(bool $value);
* defined routes.
*
* If FALSE, will stop searching and do NO automatic routing.
*
* @return RouteCollectionInterface
*/
public function setAutoRoute(bool $value): self;

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

Expand Down
Loading

0 comments on commit 6ba40da

Please sign in to comment.