From 2c66bfc9eaa18a09a53b1d2e1412822e626e4bd1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 11 May 2023 14:45:29 +0200 Subject: [PATCH 1/2] [CodingStyle] Remove ConsistentPregDelimiterRector as not much practical, importance of regex is a testing it matches expectationg, area of static analysis --- .../docs/rector_rules_overview.md | 44 +-- config/set/coding-style.php | 2 - .../Fixture/skip_anonymous_class.php | 49 ++++ .../ConsistentPregDelimiterRectorTest.php | 28 -- .../DirectStringConfigTest.php | 28 -- ...o_not_escape_expression_and_anchor.php.inc | 29 -- ...scape_content_delimiter_in_pattern.php.inc | 27 -- ...content_delimiter_in_pattern_multi.php.inc | 27 -- .../Fixture/escape_nette_static_call.php.inc | 31 -- .../Fixture/escaped_dollar_char.php.inc | 27 -- .../Fixture/fixture.php.inc | 29 -- .../Fixture/skip_concat.php.inc | 13 - .../Fixture/skip_escaped_quote.php.inc | 11 - .../Fixture/skip_escaped_quote2.php.inc | 11 - .../Fixture/skip_new_line.php.inc | 20 -- ...te_multiline_pcre_extended_pattern.php.inc | 39 --- .../FixtureDirectStringConfig/fixture.php.inc | 29 -- .../config/configured_rule.php | 10 - .../config/direct_string_configured_rule.php | 11 - .../ConsistentPregDelimiterRector.php | 265 ------------------ src/Autoloading/BootstrapFilesIncluder.php | 12 +- 21 files changed, 57 insertions(+), 685 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/skip_anonymous_class.php delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/ConsistentPregDelimiterRectorTest.php delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/DirectStringConfigTest.php delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/do_not_escape_expression_and_anchor.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern_multi.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_nette_static_call.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escaped_dollar_char.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_concat.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_escaped_quote.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_escaped_quote2.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_new_line.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/update_multiline_pcre_extended_pattern.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/FixtureDirectStringConfig/fixture.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/configured_rule.php delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/direct_string_configured_rule.php delete mode 100644 rules/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector.php diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 9f1614133a7..ee4f24c21b0 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 412 Rules Overview +# 411 Rules Overview
@@ -8,7 +8,7 @@ - [CodeQuality](#codequality) (76) -- [CodingStyle](#codingstyle) (37) +- [CodingStyle](#codingstyle) (36) - [Compatibility](#compatibility) (1) @@ -1997,46 +1997,6 @@ Changes various implode forms to consistent one
-### ConsistentPregDelimiterRector - -Replace PREG delimiter with configured one - -:wrench: **configure it!** - -- class: [`Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector`](../rules/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector.php) - -```php -ruleWithConfiguration(ConsistentPregDelimiterRector::class, [ - ConsistentPregDelimiterRector::DELIMITER => '#', - ]); -}; -``` - -↓ - -```diff - class SomeClass - { - public function run() - { -- preg_match('~value~', $value); -- preg_match_all('~value~im', $value); -+ preg_match('#value#', $value); -+ preg_match_all('#value#im', $value); - } - } -``` - -
- ### CountArrayToEmptyArrayComparisonRector Change count array comparison to empty array comparison to improve performance diff --git a/config/set/coding-style.php b/config/set/coding-style.php index b3c590c2574..e62c655394b 100644 --- a/config/set/coding-style.php +++ b/config/set/coding-style.php @@ -19,7 +19,6 @@ use Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector; use Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector; use Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector; -use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector; use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector; use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector; use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector; @@ -57,7 +56,6 @@ TernaryConditionVariableAssignmentRector::class, SymplifyQuoteEscapeRector::class, StringClassNameToClassConstantRector::class, - ConsistentPregDelimiterRector::class, CatchExceptionNameMatchingTypeRector::class, UseIncrementAssignRector::class, SplitDoubleAssignRector::class, diff --git a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/skip_anonymous_class.php b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/skip_anonymous_class.php new file mode 100644 index 00000000000..76bab72d64a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/skip_anonymous_class.php @@ -0,0 +1,49 @@ +addVisitor($this->createNodeVisitor($callable)); + $nodeTraverser->traverse($nodes); + } + + private function createNodeVisitor(callable $callable): NodeVisitor + { + return new class($callable) extends NodeVisitorAbstract { + /** + * @var callable + */ + private $callable; + + public function __construct(callable $callable) + { + $this->callable = $callable; + } + + /** + * @return int|Node|null + */ + public function enterNode(Node $node) + { + $callable = $this->callable; + return $callable($node); + } + }; + } +} diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/ConsistentPregDelimiterRectorTest.php b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/ConsistentPregDelimiterRectorTest.php deleted file mode 100644 index 1013390c8e0..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/ConsistentPregDelimiterRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/DirectStringConfigTest.php b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/DirectStringConfigTest.php deleted file mode 100644 index 695cc87c526..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/DirectStringConfigTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/FixtureDirectStringConfig'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/direct_string_configured_rule.php'; - } -} diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/do_not_escape_expression_and_anchor.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/do_not_escape_expression_and_anchor.php.inc deleted file mode 100644 index 6373a968d98..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/do_not_escape_expression_and_anchor.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern.php.inc deleted file mode 100644 index 856e1d24944..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern_multi.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern_multi.php.inc deleted file mode 100644 index 41c6c30ec3c..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_content_delimiter_in_pattern_multi.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_nette_static_call.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_nette_static_call.php.inc deleted file mode 100644 index 7946ab79703..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escape_nette_static_call.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escaped_dollar_char.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escaped_dollar_char.php.inc deleted file mode 100644 index 933999dc52d..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/escaped_dollar_char.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/fixture.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/fixture.php.inc deleted file mode 100644 index ba6bc523bb8..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_concat.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_concat.php.inc deleted file mode 100644 index 29123449f73..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture/skip_concat.php.inc +++ /dev/null @@ -1,13 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/FixtureDirectStringConfig/fixture.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/FixtureDirectStringConfig/fixture.php.inc deleted file mode 100644 index 6aaaf06f824..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/FixtureDirectStringConfig/fixture.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/configured_rule.php b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/configured_rule.php deleted file mode 100644 index 56f85841f24..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ConsistentPregDelimiterRector::class); -}; diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/direct_string_configured_rule.php b/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/direct_string_configured_rule.php deleted file mode 100644 index 66d69963afe..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector/config/direct_string_configured_rule.php +++ /dev/null @@ -1,11 +0,0 @@ -ruleWithConfiguration(ConsistentPregDelimiterRector::class, ['/']); -}; diff --git a/rules/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector.php b/rules/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector.php deleted file mode 100644 index 11334d511b3..00000000000 --- a/rules/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector.php +++ /dev/null @@ -1,265 +0,0 @@ -.*?)(?[imsxeADSUXJu]*)$#s'; - - /** - * @var string - * @see https://regex101.com/r/nnuwUo/1 - * - * For modifiers see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php - */ - private const INNER_UNICODE_REGEX = '#(?.*?)(?[imsxeADSUXJu]*)$#u'; - - /** - * @var string - * @see https://regex101.com/r/KpCzg6/1 - */ - private const NEW_LINE_REGEX = '#(\\r|\\n)#'; - - /** - * @var string - * @see https://regex101.com/r/EyXsV6/6 - */ - private const DOUBLE_QUOTED_REGEX = '#^"(?.{1}).{1,}\k[imsxeADSUXJu]*"$#'; - - /** - * All with pattern as 1st argument - * @var array - */ - private const FUNCTIONS_WITH_REGEX_PATTERN = [ - 'preg_match' => 0, - 'preg_replace_callback_array' => 0, - 'preg_replace_callback' => 0, - 'preg_replace' => 0, - 'preg_match_all' => 0, - 'preg_split' => 0, - 'preg_grep' => 0, - ]; - - /** - * All with pattern as 2st argument - * @var array> - */ - private const STATIC_METHODS_WITH_REGEX_PATTERN = [ - 'Nette\Utils\Strings' => [ - 'match' => 1, - 'matchAll' => 1, - 'replace' => 1, - 'split' => 1, - ], - ]; - - private string $delimiter = '#'; - - public function __construct( - private readonly NodePrinterInterface $nodePrinter - ) { - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition('Replace PREG delimiter with configured one', [ - new ConfiguredCodeSample( - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - preg_match('~value~', $value); - preg_match_all('~value~im', $value); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - preg_match('#value#', $value); - preg_match_all('#value#im', $value); - } -} -CODE_SAMPLE - , - [ - self::DELIMITER => '#', - ] - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [FuncCall::class, StaticCall::class]; - } - - /** - * @param FuncCall|StaticCall $node - */ - public function refactor(Node $node): ?Node - { - if ($node instanceof FuncCall) { - return $this->refactorFuncCall($node); - } - - foreach (self::STATIC_METHODS_WITH_REGEX_PATTERN as $type => $methodsToPositions) { - if (! $this->isObjectType($node->class, new ObjectType($type))) { - continue; - } - - foreach ($methodsToPositions as $method => $position) { - if (! $this->isName($node->name, $method)) { - continue; - } - - if (! $node->args[$position] instanceof Arg) { - continue; - } - - return $this->refactorArgument($node, $node->args[$position]); - } - } - - return null; - } - - public function configure(array $configuration): void - { - $this->delimiter = $configuration[self::DELIMITER] ?? (string) current($configuration); - } - - private function refactorFuncCall(FuncCall $funcCall): ?FuncCall - { - foreach (self::FUNCTIONS_WITH_REGEX_PATTERN as $function => $position) { - if (! $this->isName($funcCall, $function)) { - continue; - } - - if (! $funcCall->args[$position] instanceof Arg) { - continue; - } - - return $this->refactorArgument($funcCall, $funcCall->args[$position]); - } - - return null; - } - - private function hasNewLineWithUnicodeModifier(string $string): bool - { - $matchInnerRegex = Strings::match($string, self::INNER_REGEX); - $matchInnerUnionRegex = Strings::match($string, self::INNER_UNICODE_REGEX); - - if (! is_array($matchInnerRegex)) { - return false; - } - - if (! is_array($matchInnerUnionRegex)) { - return false; - } - - if ($matchInnerRegex === $matchInnerUnionRegex) { - return false; - } - - if (StringUtils::isMatch($matchInnerUnionRegex['content'], self::NEW_LINE_REGEX)) { - return true; - } - - return isset($string[0]) && $matchInnerUnionRegex['content'] === $string[0]; - } - - private function hasEscapedQuote(String_ $string): bool - { - $kind = $string->getAttribute(AttributeKey::KIND); - - if ($kind === String_::KIND_DOUBLE_QUOTED && str_contains($string->value, '"')) { - return true; - } - - return $kind === String_::KIND_SINGLE_QUOTED && str_contains($string->value, "'"); - } - - private function refactorArgument(FuncCall|StaticCall $node, Arg $arg): Node|null - { - if (! $arg->value instanceof String_) { - return null; - } - - /** @var String_ $string */ - $string = $arg->value; - - if ($this->hasEscapedQuote($string)) { - return null; - } - - if ($this->hasNewLineWithUnicodeModifier($string->value)) { - return null; - } - - $string->value = Strings::replace($string->value, self::INNER_REGEX, function (array $match) use ( - &$string - ): string { - $printedString = $this->nodePrinter->print($string); - if (StringUtils::isMatch($printedString, self::DOUBLE_QUOTED_REGEX)) { - $string->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true); - } - - $innerPattern = $match['content']; - $positionDelimiter = strpos($innerPattern, $this->delimiter); - - if ($positionDelimiter > 0) { - $innerPattern = str_replace($this->delimiter, '\\' . $this->delimiter, $innerPattern); - } - - // change delimiter - if (strlen($innerPattern) > 2 && $innerPattern[0] === $innerPattern[strlen($innerPattern) - 1]) { - $innerPattern[0] = $this->delimiter; - $innerPattern[strlen($innerPattern) - 1] = $this->delimiter; - } - - return $innerPattern . $match['close']; - }); - return $node; - } -} diff --git a/src/Autoloading/BootstrapFilesIncluder.php b/src/Autoloading/BootstrapFilesIncluder.php index 2995b7bae36..c257ad4099b 100644 --- a/src/Autoloading/BootstrapFilesIncluder.php +++ b/src/Autoloading/BootstrapFilesIncluder.php @@ -21,17 +21,17 @@ */ final class BootstrapFilesIncluder { + /** + * @var array + */ + private $configCache = []; + public function __construct( private readonly ParameterProvider $parameterProvider, private readonly PHPStanExtensionsConfigResolver $phpStanExtensionsConfigResolver, ) { } - /** - * @var array - */ - private $configCache = []; - public function includePHPStanExtensionsBoostrapFiles(?Container $container = null): void { $extensionConfigFiles = $this->phpStanExtensionsConfigResolver->resolve(); @@ -74,7 +74,7 @@ private function resolveAbsoluteBootstrapFilePaths(array $extensionConfigFiles): $absoluteBootstrapFilePaths = []; foreach ($extensionConfigFiles as $extensionConfigFile) { - if (!array_key_exists($extensionConfigFile, $this->configCache)) { + if (! array_key_exists($extensionConfigFile, $this->configCache)) { $extensionConfigContents = Neon::decodeFile($extensionConfigFile); $this->configCache[$extensionConfigFile] = $extensionConfigContents; From 4b3bbf2720774ba39a39202cd700b834d4e31508 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 11 May 2023 13:23:55 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- src/Autoloading/BootstrapFilesIncluder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Autoloading/BootstrapFilesIncluder.php b/src/Autoloading/BootstrapFilesIncluder.php index c257ad4099b..9c0be92637a 100644 --- a/src/Autoloading/BootstrapFilesIncluder.php +++ b/src/Autoloading/BootstrapFilesIncluder.php @@ -24,7 +24,7 @@ final class BootstrapFilesIncluder /** * @var array */ - private $configCache = []; + private array $configCache = []; public function __construct( private readonly ParameterProvider $parameterProvider, @@ -81,6 +81,7 @@ private function resolveAbsoluteBootstrapFilePaths(array $extensionConfigFiles): } else { $extensionConfigContents = $this->configCache[$extensionConfigFile]; } + $configDirectory = dirname($extensionConfigFile); $bootstrapFiles = $extensionConfigContents['parameters']['bootstrapFiles'] ?? [];