From be8b803417378935a99df0e9e712141f59c1f9bb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 May 2021 09:06:19 +0700 Subject: [PATCH 1/4] [Rector] Add custom Rector Rule: RemoveErrorSuppressInTryCatchStmtsRector rector rule --- rector.php | 2 + system/Helpers/filesystem_helper.php | 18 ++--- ...moveErrorSuppressInTryCatchStmtsRector.php | 68 +++++++++++++++++++ 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php diff --git a/rector.php b/rector.php index 591e4e6784cf..b1518c82ed7c 100644 --- a/rector.php +++ b/rector.php @@ -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 { @@ -84,4 +85,5 @@ $services->set(ChangeArrayPushToArrayAssignRector::class); $services->set(UnnecessaryTernaryExpressionRector::class); $services->set(RemoveUnusedPrivatePropertyRector::class); + $services->set(RemoveErrorSuppressInTryCatchStmtsRector::class); }; diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index 5499c5d6430c..c39d8974f2f2 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -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 * @@ -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 @@ -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()); } } } @@ -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) { @@ -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 { diff --git a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php new file mode 100644 index 000000000000..b4884836b77f --- /dev/null +++ b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php @@ -0,0 +1,68 @@ +betterNodeFinder->findParentType($node, TryCatch::class); + if (! $tryCatch instanceof TryCatch) + { + return null; + } + + // not in stmts, means it in catch or finally + $inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, function (Node $n) use ($node) : bool { + return $n === $node; + }); + + if (! $inStmts) + { + return null; + } + + // in try { ... } stmts + return $node->expr; + } +} From 4c860dc740a63b63a1fc566201b52b3bc4c2e81f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 May 2021 21:17:01 +0700 Subject: [PATCH 2/4] apply code review --- ...emoveErrorSuppressInTryCatchStmtsRector.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php index b4884836b77f..7af456039f3f 100644 --- a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php +++ b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php @@ -15,18 +15,18 @@ final class RemoveErrorSuppressInTryCatchStmtsRector extends AbstractRector { public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Remove error suppress @', [ + return new RuleDefinition('Remove error suppression operator `@` inside try...catch blocks', [ new CodeSample( <<<'CODE_SAMPLE' -try { - @rmdir($dirname); -} catch (Exception $e) {} + try { + @rmdir($dirname); + } catch (Exception $e) {} CODE_SAMPLE , <<<'CODE_SAMPLE' -try { - rmdir($dirname); -} catch (Exception $e) {} + try { + rmdir($dirname); + } catch (Exception $e) {} CODE_SAMPLE ), ]); @@ -52,11 +52,11 @@ public function refactor(Node $node): ?Node return null; } - // not in stmts, means it in catch or finally - $inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, function (Node $n) use ($node) : bool { + $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; From 67d8ec3cad0980b4a55165d4dfd7d77809e8267b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 May 2021 21:18:47 +0700 Subject: [PATCH 3/4] another move comment --- utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php index 7af456039f3f..7573a04122f2 100644 --- a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php +++ b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php @@ -45,8 +45,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - // not in try catch $tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class); + + // not in try catch if (! $tryCatch instanceof TryCatch) { return null; From 7212c532434d64b03033a298142a469f04d8d015 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 May 2021 21:22:28 +0700 Subject: [PATCH 4/4] better indentation --- ...RemoveErrorSuppressInTryCatchStmtsRector.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php index 7573a04122f2..67d153e68e24 100644 --- a/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php +++ b/utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php @@ -18,16 +18,15 @@ 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 -, + try { + @rmdir($dirname); + } catch (Exception $e) {} + CODE_SAMPLE, <<<'CODE_SAMPLE' - try { - rmdir($dirname); - } catch (Exception $e) {} -CODE_SAMPLE + try { + rmdir($dirname); + } catch (Exception $e) {} + CODE_SAMPLE ), ]); }