Skip to content

Commit

Permalink
[PHPStan] Resolve duplicated regex value (#2690)
Browse files Browse the repository at this point in the history
* [PHPStan] Resolve duplicated regex value

* [ci-review] Rector Rectify

* final touch: more @see

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 20, 2022
1 parent c74cda7 commit 9cb0a5e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
2 changes: 1 addition & 1 deletion rules/Naming/Guard/BreakingVariableRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 4 additions & 7 deletions rules/Naming/Guard/DateTimeAtNamingConventionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 . ''
);
}
}
17 changes: 5 additions & 12 deletions src/Validation/RectorAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -56,22 +57,14 @@ 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');
}

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
Expand All @@ -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
Expand Down

0 comments on commit 9cb0a5e

Please sign in to comment.