From 76758f6d562ccf22c9b69cf18a5664a58de32afb Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 20 May 2024 01:24:01 +0900 Subject: [PATCH 1/2] Remove NoRelativeFilePathRule as more exceptoins --- config/code-complexity-rules.neon | 1 - docs/rules_overview.md | 24 +-- src/Rules/Explicit/NoRelativeFilePathRule.php | 155 ------------------ .../Fixture/RelativeFilePath.php | 13 -- .../Fixture/SkipAbsoluteFilePath.php | 13 -- .../Fixture/SkipEmails.php | 13 -- .../Fixture/SkipHereNowDoc.php | 17 -- .../Fixture/SkipMaskFinder.php | 20 --- .../Fixture/SkipNoFileBefore.php | 18 -- .../Fixture/SkipNotAFileExtension.php | 13 -- .../Fixture/SkipRegexConsts.php | 10 -- .../Fixture/SkipSimpleString.php | 13 -- .../Fixture/SkipStrEndsWith.php | 13 -- .../Fixture/SkipUrls.php | 17 -- .../NoRelativeFilePathRuleTest.php | 53 ------ .../config/configured_rule.neon | 5 - 16 files changed, 1 insertion(+), 397 deletions(-) delete mode 100644 src/Rules/Explicit/NoRelativeFilePathRule.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/RelativeFilePath.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipAbsoluteFilePath.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipEmails.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipHereNowDoc.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipMaskFinder.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipNoFileBefore.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipNotAFileExtension.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipRegexConsts.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipSimpleString.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipStrEndsWith.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipUrls.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/NoRelativeFilePathRuleTest.php delete mode 100644 tests/Rules/Explicit/NoRelativeFilePathRule/config/configured_rule.neon diff --git a/config/code-complexity-rules.neon b/config/code-complexity-rules.neon index 30345a33..43e8589d 100644 --- a/config/code-complexity-rules.neon +++ b/config/code-complexity-rules.neon @@ -3,5 +3,4 @@ rules: - Symplify\PHPStanRules\Rules\Explicit\NoMixedMethodCallerRule - Symplify\PHPStanRules\Rules\Enum\RequireEnumDocBlockOnConstantListPassRule - Symplify\PHPStanRules\Rules\NoDynamicNameRule - - Symplify\PHPStanRules\Rules\Explicit\NoRelativeFilePathRule - Symplify\PHPStanRules\Rules\NoReturnArrayVariableListRule diff --git a/docs/rules_overview.md b/docs/rules_overview.md index ba59637f..ca3b208c 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -1,4 +1,4 @@ -# 45 Rules Overview +# 44 Rules Overview ## AnnotateRegexClassConstWithRegexLinkRule @@ -959,28 +959,6 @@ class SomeClass
-## NoRelativeFilePathRule - -Relative file path "%s" is not allowed, use absolute one with __DIR__ - -- class: [`Symplify\PHPStanRules\Rules\Explicit\NoRelativeFilePathRule`](../src/Rules/Explicit/NoRelativeFilePathRule.php) - -```php -$filePath = 'some_file.txt'; -``` - -:x: - -
- -```php -$filePath = __DIR__ . '/some_file.txt'; -``` - -:+1: - -
- ## NoReturnArrayVariableListRule Use value object over return of values diff --git a/src/Rules/Explicit/NoRelativeFilePathRule.php b/src/Rules/Explicit/NoRelativeFilePathRule.php deleted file mode 100644 index b91a037a..00000000 --- a/src/Rules/Explicit/NoRelativeFilePathRule.php +++ /dev/null @@ -1,155 +0,0 @@ - - */ - public function getNodeType(): string - { - return InClassNode::class; - } - - /** - * @param InClassNode $node - * @return RuleError[] - */ - public function processNode(Node $node, Scope $scope): array - { - $classLike = $node->getOriginalNode(); - $strings = $this->findBareStrings($classLike); - - $errorMessages = []; - - foreach ($strings as $string) { - // is it a file string? - if (! $this->isFileString($string)) { - continue; - } - - $errorMessage = sprintf(self::ERROR_MESSAGE, $string->value); - - $errorMessages[] = RuleErrorBuilder::message($errorMessage) - ->line($string->getLine()) - ->build(); - } - - return $errorMessages; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition(self::ERROR_MESSAGE, [ - new CodeSample( - <<<'CODE_SAMPLE' -$filePath = 'some_file.txt'; -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -$filePath = __DIR__ . '/some_file.txt'; -CODE_SAMPLE - ), - ]); - } - - /** - * @return String_[] - */ - private function findBareStrings(ClassLike $classLike): array - { - // mimics node finding visitors of NodeFinder with ability to stop traversing deeper - $nodeTraverser = new NodeTraverser(); - - $stringOutsideConcatFindingNodeVisitor = new StringOutsideConcatFindingNodeVisitor( - new FileCheckingFuncCallAnalyzer() - ); - - $nodeTraverser->addVisitor($stringOutsideConcatFindingNodeVisitor); - $nodeTraverser->traverse($classLike->stmts); - - return $stringOutsideConcatFindingNodeVisitor->getFoundNodes(); - } - - private function isFileString(String_ $string): bool - { - // probably an url - if (Strings::match($string->value, self::URL_START_REGEX)) { - return false; - } - - if (Strings::match($string->value, self::DOMAIN_END_REGEX)) { - return false; - } - - // probably an email - if (str_contains($string->value, '@')) { - return false; - } - - $pathInfo = pathinfo($string->value); - if (! isset($pathInfo['extension'])) { - return false; - } - - $fileExtension = $pathInfo['extension']; - - if (strlen($fileExtension) > 3) { - return false; - } - - if (strlen($fileExtension) < 3) { - return false; - } - - if ($pathInfo['filename'] === '') { - return false; - } - - if (str_contains($pathInfo['filename'], '*')) { - return false; - } - - // only letters - return ctype_alpha($fileExtension); - } -} diff --git a/tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/RelativeFilePath.php b/tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/RelativeFilePath.php deleted file mode 100644 index 0e6aa7e6..00000000 --- a/tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/RelativeFilePath.php +++ /dev/null @@ -1,13 +0,0 @@ -files() - ->in($sources) - ->name('*.php.inc') - ->path('Fixture') - ->sortByName(); - } -} diff --git a/tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipNoFileBefore.php b/tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipNoFileBefore.php deleted file mode 100644 index 9cca4129..00000000 --- a/tests/Rules/Explicit/NoRelativeFilePathRule/Fixture/SkipNoFileBefore.php +++ /dev/null @@ -1,18 +0,0 @@ -> $expectedErrorsWithLines - */ - #[DataProvider('provideData')] - public function testRule(string $filePath, array $expectedErrorsWithLines): void - { - $this->analyse([$filePath], $expectedErrorsWithLines); - } - - public static function provideData(): Iterator - { - yield [__DIR__ . '/Fixture/SkipEmails.php', []]; - yield [__DIR__ . '/Fixture/SkipMaskFinder.php', []]; - yield [__DIR__ . '/Fixture/SkipStrEndsWith.php', []]; - yield [__DIR__ . '/Fixture/SkipRegexConsts.php', []]; - yield [__DIR__ . '/Fixture/SkipHereNowDoc.php', []]; - yield [__DIR__ . '/Fixture/SkipNoFileBefore.php', []]; - yield [__DIR__ . '/Fixture/SkipAbsoluteFilePath.php', []]; - yield [__DIR__ . '/Fixture/SkipSimpleString.php', []]; - yield [__DIR__ . '/Fixture/SkipNotAFileExtension.php', []]; - yield [__DIR__ . '/Fixture/SkipUrls.php', []]; - - $errorMessage = sprintf(NoRelativeFilePathRule::ERROR_MESSAGE, 'some_relative_path.txt'); - yield [__DIR__ . '/Fixture/RelativeFilePath.php', [[$errorMessage, 11]]]; - } - - /** - * @return string[] - */ - public static function getAdditionalConfigFiles(): array - { - return [__DIR__ . '/config/configured_rule.neon']; - } - - protected function getRule(): Rule - { - return self::getContainer()->getByType(NoRelativeFilePathRule::class); - } -} diff --git a/tests/Rules/Explicit/NoRelativeFilePathRule/config/configured_rule.neon b/tests/Rules/Explicit/NoRelativeFilePathRule/config/configured_rule.neon deleted file mode 100644 index 4f602548..00000000 --- a/tests/Rules/Explicit/NoRelativeFilePathRule/config/configured_rule.neon +++ /dev/null @@ -1,5 +0,0 @@ -includes: - - ../../../../config/included_services.neon - -rules: - - Symplify\PHPStanRules\Rules\Explicit\NoRelativeFilePathRule From 902be0f53fcb3a668053d258fd51a98ca883f84b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 20 May 2024 01:34:10 +0900 Subject: [PATCH 2/2] cleanup --- .../StringOutsideConcatFindingNodeVisitor.php | 89 ------------------- 1 file changed, 89 deletions(-) delete mode 100644 src/NodeVisitor/StringOutsideConcatFindingNodeVisitor.php diff --git a/src/NodeVisitor/StringOutsideConcatFindingNodeVisitor.php b/src/NodeVisitor/StringOutsideConcatFindingNodeVisitor.php deleted file mode 100644 index df278540..00000000 --- a/src/NodeVisitor/StringOutsideConcatFindingNodeVisitor.php +++ /dev/null @@ -1,89 +0,0 @@ -foundNodes = []; - return null; - } - - public function enterNode(Node $node): int|Node|null - { - if ($this->fileCheckingFuncCallAnalyzer->isFileExistCheck($node)) { - return NodeTraverser::DONT_TRAVERSE_CHILDREN; - } - - if ($this->isStrFuncCall($node)) { - return NodeTraverser::DONT_TRAVERSE_CHILDREN; - } - - if ($node instanceof Concat) { - return NodeTraverser::DONT_TRAVERSE_CHILDREN; - } - - if (! $node instanceof String_) { - return null; - } - - $stringKind = $node->getAttribute(AttributeKey::KIND); - - // skip here/now docs, not a file - if (in_array($stringKind, [String_::KIND_HEREDOC, String_::KIND_NOWDOC], true)) { - return null; - } - - $this->foundNodes[] = $node; - return null; - } - - /** - * @return String_[] - */ - public function getFoundNodes(): array - { - return $this->foundNodes; - } - - private function isStrFuncCall(Node $node): bool - { - if (! $node instanceof FuncCall) { - return false; - } - - if (! $node->name instanceof Name) { - return false; - } - - $functionName = $node->name->toString(); - - return in_array($functionName, ['str_ends_with', 'strpos', 'str_stars_with', 'sprintf'], true); - } -}