From 9cb0a5efb59c51c662a4b6eb56753b9cc2174e21 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 20 Jul 2022 11:55:29 +0700 Subject: [PATCH] [PHPStan] Resolve duplicated regex value (#2690) * [PHPStan] Resolve duplicated regex value * [ci-review] Rector Rectify * final touch: more @see Co-authored-by: GitHub Action --- phpstan.neon | 1 - .../Guard/BreakingVariableRenameGuard.php | 2 +- .../Guard/DateTimeAtNamingConventionGuard.php | 11 ++++------- src/Validation/RectorAssert.php | 17 +++++------------ 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 501d0d5a80f..55f25b72075 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -711,4 +711,3 @@ parameters: # @todo resolve later - '#Lowered variable "(.*?)" is used in various\-cased names\: "(.*?)", unite it to one#' - - '#The "(.*?)" constant contains duplicated regex "(.*?)"\. Instead of duplicated regexes, extract domain regexes together to save maintenance#' diff --git a/rules/Naming/Guard/BreakingVariableRenameGuard.php b/rules/Naming/Guard/BreakingVariableRenameGuard.php index f59b558b30e..5ea4adee7dc 100644 --- a/rules/Naming/Guard/BreakingVariableRenameGuard.php +++ b/rules/Naming/Guard/BreakingVariableRenameGuard.php @@ -34,7 +34,7 @@ final class BreakingVariableRenameGuard * @var string * @see https://regex101.com/r/1pKLgf/1 */ - private const AT_NAMING_REGEX = '#[\w+]At$#'; + public const AT_NAMING_REGEX = '#[\w+]At$#'; public function __construct( private readonly BetterNodeFinder $betterNodeFinder, diff --git a/rules/Naming/Guard/DateTimeAtNamingConventionGuard.php b/rules/Naming/Guard/DateTimeAtNamingConventionGuard.php index 58d1d68fc36..8b23a8c0e7f 100644 --- a/rules/Naming/Guard/DateTimeAtNamingConventionGuard.php +++ b/rules/Naming/Guard/DateTimeAtNamingConventionGuard.php @@ -18,12 +18,6 @@ */ final class DateTimeAtNamingConventionGuard implements ConflictingNameGuardInterface { - /** - * @var string - * @see https://regex101.com/r/1pKLgf/1/ - */ - private const AT_NAMING_REGEX = '#[\w+]At$#'; - public function __construct( private readonly NodeTypeResolver $nodeTypeResolver, private readonly TypeUnwrapper $typeUnwrapper @@ -51,6 +45,9 @@ private function isDateTimeAtNamingConvention(PropertyRename $propertyRename): b return false; } - return StringUtils::isMatch($propertyRename->getCurrentName(), self::AT_NAMING_REGEX . ''); + return StringUtils::isMatch( + $propertyRename->getCurrentName(), + BreakingVariableRenameGuard::AT_NAMING_REGEX . '' + ); } } diff --git a/src/Validation/RectorAssert.php b/src/Validation/RectorAssert.php index 2032802bae0..a8a7eaa8aa0 100644 --- a/src/Validation/RectorAssert.php +++ b/src/Validation/RectorAssert.php @@ -41,12 +41,13 @@ final class RectorAssert private const PROPERTY_NAME_REGEX = '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#'; /** - * @see https://regex101.com/r/uh5B0S/1 + * @see https://regex101.com/r/uh5B0S/2 * @see https://www.php.net/manual/en/functions.user-defined.php + * @see https://www.php.net/manual/en/language.constants.php * * @var string */ - private const METHOD_NAME_REGEX = '#^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$#'; + private const METHOD_OR_CONSTANT_NAME_REGEX = '#^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$#'; /** * @see https://regex101.com/r/uh5B0S/1 @@ -56,14 +57,6 @@ final class RectorAssert */ private const FUNCTION_NAME_REGEX = '#^(' . self::NAKED_NAMESPACE_REGEX . '\\\\)?([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)$#'; - /** - * @see https://www.php.net/manual/en/language.constants.php - * @see https://regex101.com/r/Fu6WHQ/1 - * - * @var string - */ - private const CONSTANT_REGEX = '#^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$#'; - public static function namespaceName(string $name): void { self::elementName($name, self::NAMESPACE_REGEX, 'namespace'); @@ -71,7 +64,7 @@ public static function namespaceName(string $name): void public static function constantName(string $name): void { - self::elementName($name, self::CONSTANT_REGEX, 'constant'); + self::elementName($name, self::METHOD_OR_CONSTANT_NAME_REGEX, 'constant'); } public static function className(string $name): void @@ -86,7 +79,7 @@ public static function propertyName(string $name): void public static function methodName(string $name): void { - self::elementName($name, self::METHOD_NAME_REGEX, 'method'); + self::elementName($name, self::METHOD_OR_CONSTANT_NAME_REGEX, 'method'); } public static function functionName(string $name): void