Skip to content

Commit b3e9057

Browse files
authored
Merge pull request #4724 from samsonasik/add-remove-error-suppress
[Rector] Add custom Rector Rule: RemoveErrorSuppressInTryCatchStmtsRector rector rule
2 parents 6da0e5b + 7212c53 commit b3e9057

3 files changed

Lines changed: 80 additions & 8 deletions

File tree

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Rector\Set\ValueObject\SetList;
2828
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2929
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
30+
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
3031
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;
3132

3233
return static function (ContainerConfigurator $containerConfigurator): void {
@@ -84,4 +85,5 @@
8485
$services->set(ChangeArrayPushToArrayAssignRector::class);
8586
$services->set(UnnecessaryTernaryExpressionRector::class);
8687
$services->set(RemoveUnusedPrivatePropertyRector::class);
88+
$services->set(RemoveErrorSuppressInTryCatchStmtsRector::class);
8789
};

system/Helpers/filesystem_helper.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ function directory_map(string $sourceDir, int $directoryDepth = 0, bool $hidden
8181
* Recursively copies the files and directories of the origin directory
8282
* into the target directory, i.e. "mirror" its contents.
8383
*
84-
* @param string $originDir
85-
* @param string $targetDir
86-
* @param bool $overwrite Whether individual files overwrite on collision
84+
* @param string $originDir
85+
* @param string $targetDir
86+
* @param boolean $overwrite Whether individual files overwrite on collision
8787
*
8888
* @return void
8989
*
@@ -103,7 +103,9 @@ function directory_mirror(string $originDir, string $targetDir, bool $overwrite
103103

104104
$dirLen = strlen($originDir);
105105

106-
/** @var SplFileInfo $file */
106+
/**
107+
* @var SplFileInfo $file
108+
*/
107109
foreach (new RecursiveIteratorIterator(
108110
new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
109111
RecursiveIteratorIterator::SELF_FIRST
@@ -210,12 +212,12 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
210212
$isDir = $object->isDir();
211213
if ($isDir && $delDir)
212214
{
213-
@rmdir($object->getPathname());
215+
rmdir($object->getPathname());
214216
continue;
215217
}
216218
if (! $isDir)
217219
{
218-
@unlink($object->getPathname());
220+
unlink($object->getPathname());
219221
}
220222
}
221223
}
@@ -315,7 +317,7 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r
315317

316318
try
317319
{
318-
$fp = @opendir($sourceDir); {
320+
$fp = opendir($sourceDir); {
319321
// reset the array and make sure $source_dir has a trailing slash on the initial call
320322
if ($recursion === false)
321323
{
@@ -507,7 +509,7 @@ function octal_permissions(int $perms): string
507509
* @param string $file1
508510
* @param string $file2
509511
*
510-
* @return bool Same or not
512+
* @return boolean Same or not
511513
*/
512514
function same_file(string $file1, string $file2): bool
513515
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Utils\Rector;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ErrorSuppress;
9+
use PhpParser\Node\Stmt\TryCatch;
10+
use Rector\Core\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
final class RemoveErrorSuppressInTryCatchStmtsRector extends AbstractRector
15+
{
16+
public function getRuleDefinition(): RuleDefinition
17+
{
18+
return new RuleDefinition('Remove error suppression operator `@` inside try...catch blocks', [
19+
new CodeSample(
20+
<<<'CODE_SAMPLE'
21+
try {
22+
@rmdir($dirname);
23+
} catch (Exception $e) {}
24+
CODE_SAMPLE,
25+
<<<'CODE_SAMPLE'
26+
try {
27+
rmdir($dirname);
28+
} catch (Exception $e) {}
29+
CODE_SAMPLE
30+
),
31+
]);
32+
}
33+
34+
/**
35+
* @return string[]
36+
*/
37+
public function getNodeTypes(): array
38+
{
39+
return [ErrorSuppress::class];
40+
}
41+
42+
/**
43+
* @param ErrorSuppress $node
44+
*/
45+
public function refactor(Node $node): ?Node
46+
{
47+
$tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class);
48+
49+
// not in try catch
50+
if (! $tryCatch instanceof TryCatch)
51+
{
52+
return null;
53+
}
54+
55+
$inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, static function (Node $n) use ($node) : bool {
56+
return $n === $node;
57+
});
58+
59+
// not in stmts, means it in catch or finally
60+
if (! $inStmts)
61+
{
62+
return null;
63+
}
64+
65+
// in try { ... } stmts
66+
return $node->expr;
67+
}
68+
}

0 commit comments

Comments
 (0)