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

[Rector] Add custom Rector Rule: RemoveErrorSuppressInTryCatchStmtsRector rector rule #4724

Merged
merged 4 commits into from May 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions rector.php
Expand Up @@ -27,6 +27,7 @@
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;

return static function (ContainerConfigurator $containerConfigurator): void {
Expand Down Expand Up @@ -84,4 +85,5 @@
$services->set(ChangeArrayPushToArrayAssignRector::class);
$services->set(UnnecessaryTernaryExpressionRector::class);
$services->set(RemoveUnusedPrivatePropertyRector::class);
$services->set(RemoveErrorSuppressInTryCatchStmtsRector::class);
};
18 changes: 10 additions & 8 deletions system/Helpers/filesystem_helper.php
Expand Up @@ -81,9 +81,9 @@ function directory_map(string $sourceDir, int $directoryDepth = 0, bool $hidden
* Recursively copies the files and directories of the origin directory
* into the target directory, i.e. "mirror" its contents.
*
* @param string $originDir
* @param string $targetDir
* @param bool $overwrite Whether individual files overwrite on collision
* @param string $originDir
* @param string $targetDir
* @param boolean $overwrite Whether individual files overwrite on collision
*
* @return void
*
Expand All @@ -103,7 +103,9 @@ function directory_mirror(string $originDir, string $targetDir, bool $overwrite

$dirLen = strlen($originDir);

/** @var SplFileInfo $file */
/**
* @var SplFileInfo $file
*/
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
Expand Down Expand Up @@ -210,12 +212,12 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
$isDir = $object->isDir();
if ($isDir && $delDir)
{
@rmdir($object->getPathname());
rmdir($object->getPathname());
continue;
}
if (! $isDir)
{
@unlink($object->getPathname());
unlink($object->getPathname());
}
}
}
Expand Down Expand Up @@ -315,7 +317,7 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r

try
{
$fp = @opendir($sourceDir); {
$fp = opendir($sourceDir); {
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
Expand Down Expand Up @@ -507,7 +509,7 @@ function octal_permissions(int $perms): string
* @param string $file1
* @param string $file2
*
* @return bool Same or not
* @return boolean Same or not
*/
function same_file(string $file1, string $file2): bool
{
Expand Down
68 changes: 68 additions & 0 deletions utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Utils\Rector;

use PhpParser\Node;
use PhpParser\Node\Expr\ErrorSuppress;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class RemoveErrorSuppressInTryCatchStmtsRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove error suppress @', [
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
new CodeSample(
<<<'CODE_SAMPLE'
try {
@rmdir($dirname);
} catch (Exception $e) {}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
try {
rmdir($dirname);
} catch (Exception $e) {}
CODE_SAMPLE
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ErrorSuppress::class];
}

/**
* @param ErrorSuppress $node
*/
public function refactor(Node $node): ?Node
{
// not in try catch
$tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class);
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
if (! $tryCatch instanceof TryCatch)
{
return null;
}

// not in stmts, means it in catch or finally
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
$inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, function (Node $n) use ($node) : bool {
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
return $n === $node;
});

if (! $inStmts)
{
return null;
}

// in try { ... } stmts
return $node->expr;
}
}