Skip to content

Commit

Permalink
Merge pull request #4724 from samsonasik/add-remove-error-suppress
Browse files Browse the repository at this point in the history
[Rector] Add custom Rector Rule: RemoveErrorSuppressInTryCatchStmtsRector rector rule
  • Loading branch information
samsonasik authored May 22, 2021
2 parents 6da0e5b + 7212c53 commit b3e9057
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 suppression operator `@` inside try...catch blocks', [
new CodeSample(
<<<'CODE_SAMPLE'
try {
@rmdir($dirname);
} catch (Exception $e) {}
CODE_SAMPLE,
<<<'CODE_SAMPLE'
try {
rmdir($dirname);
} catch (Exception $e) {}
CODE_SAMPLE
),
]);
}

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

/**
* @param ErrorSuppress $node
*/
public function refactor(Node $node): ?Node
{
$tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class);

// not in try catch
if (! $tryCatch instanceof TryCatch)
{
return null;
}

$inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, static function (Node $n) use ($node) : bool {
return $n === $node;
});

// not in stmts, means it in catch or finally
if (! $inStmts)
{
return null;
}

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

0 comments on commit b3e9057

Please sign in to comment.