Skip to content

Commit

Permalink
[DX] Add strict PHPStan rules - step #1 (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 28, 2021
1 parent 3f6f706 commit f985e81
Show file tree
Hide file tree
Showing 63 changed files with 182 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Util\StringUtils;

final class DoctrineAnnotationDecorator
{
Expand Down Expand Up @@ -96,7 +97,7 @@ private function mergeNestedDoctrineAnnotations(PhpDocNode $phpDocNode): void

$nextPhpDocChildNode = $phpDocNode->children[$key];

if ($nextPhpDocChildNode instanceof PhpDocTextNode && Strings::match(
if ($nextPhpDocChildNode instanceof PhpDocTextNode && StringUtils::isMatch(
$nextPhpDocChildNode->text,
self::NESTED_ANNOTATION_END_REGEX
)) {
Expand Down
10 changes: 5 additions & 5 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Util\StringUtils;
use Symplify\SimplePhpDocParser\PhpDocNodeTraverser;

/**
Expand Down Expand Up @@ -168,15 +169,15 @@ private function printPhpDocNode(PhpDocNode $phpDocNode): string
$output = $this->printEnd($output);

// fix missing start
if (! Strings::match($output, self::DOCBLOCK_START_REGEX) && $output) {
if (! StringUtils::isMatch($output, self::DOCBLOCK_START_REGEX) && $output) {
$output = '/**' . $output;
}

// fix missing end
if (Strings::match($output, self::OPENING_DOCBLOCK_REGEX) && $output && ! Strings::match(
if (StringUtils::isMatch(
$output,
self::CLOSING_DOCBLOCK_REGEX
)) {
self::OPENING_DOCBLOCK_REGEX
) && $output && ! StringUtils::isMatch($output, self::CLOSING_DOCBLOCK_REGEX)) {
$output .= ' */';
}

Expand Down Expand Up @@ -295,7 +296,6 @@ private function appendToOutput(string $output, int $from, int $to, array $posit
for ($i = $from; $i < $to; ++$i) {
while (isset($positionJumpSet[$i])) {
$i = $positionJumpSet[$i];
continue;
}

$output .= $this->tokens[$i][0] ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Util\StringUtils;

abstract class AbstractValuesAwareNode implements PhpDocTagValueNode
{
Expand Down Expand Up @@ -81,11 +82,11 @@ public function getValue(string | int $key)
public function changeValue(string $key, $value): void
{
// is quoted?
if (isset($this->values[$key]) && is_string($this->values[$key])) {
$isQuoted = (bool) Strings::match($this->values[$key], self::UNQUOTED_VALUE_REGEX);
if ($isQuoted) {
$value = '"' . $value . '"';
}
if (isset($this->values[$key]) && is_string($this->values[$key]) && StringUtils::isMatch(
$this->values[$key],
self::UNQUOTED_VALUE_REGEX
)) {
$value = '"' . $value . '"';
}

$this->values[$key] = $value;
Expand Down Expand Up @@ -113,8 +114,7 @@ public function getValueWithoutQuotes(string | int $key)
public function changeSilentValue($value): void
{
// is quoted?
$isQuoted = (bool) Strings::match($this->values[0], self::UNQUOTED_VALUE_REGEX);
if ($isQuoted) {
if (StringUtils::isMatch($this->values[0], self::UNQUOTED_VALUE_REGEX)) {
$value = '"' . $value . '"';
}

Expand Down
5 changes: 3 additions & 2 deletions packages/FileFormatter/Formatter/XmlFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\FileFormatter\Formatter;

use Nette\Utils\Strings;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\Formatter\FileFormatterInterface;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
Expand Down Expand Up @@ -143,12 +144,12 @@ private function getPaddedString(string $part): string

private function isOpeningTag(string $part): bool
{
return (bool) Strings::match($part, self::IS_OPENING_TAG_REGEX);
return StringUtils::isMatch($part, self::IS_OPENING_TAG_REGEX);
}

private function isClosingTag(string $part): bool
{
return (bool) Strings::match($part, self::IS_CLOSING_TAG_REGEX);
return StringUtils::isMatch($part, self::IS_CLOSING_TAG_REGEX);
}

private function isOpeningCdataTag(string $part): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\FileSystemRector\ValueObjectFactory;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
Expand All @@ -13,6 +12,7 @@
use Rector\Autodiscovery\Configuration\CategoryNamespaceProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;
use Rector\FileSystemRector\ValueObject\AddedFileWithNodes;
use Rector\PSR4\FileInfoAnalyzer\FileInfoDeletionAnalyzer;
Expand Down Expand Up @@ -67,7 +67,7 @@ public function createWithDesiredGroup(
return null;
}

if (Strings::match($oldClassName, '#\b' . $desiredGroupName . '\b#')) {
if (StringUtils::isMatch($oldClassName, '#\b' . $desiredGroupName . '\b#')) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/NodeCollector/StaticAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Rector\NodeCollector;

use Nette\Utils\Strings;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Util\StringUtils;

final class StaticAnalyzer
{
Expand Down Expand Up @@ -44,7 +44,7 @@ private function hasStaticAnnotation(string $methodName, ClassReflection $classR
}

// @see https://regex101.com/r/7Zkej2/1
return (bool) Strings::match(
return StringUtils::isMatch(
$resolvedPhpDocBlock->getPhpDocString(),
'#@method\s*static\s*((([\w\|\\\\]+)|\$this)*+(\[\])*)*\s+\b' . $methodName . '\b#'
);
Expand Down
6 changes: 3 additions & 3 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\NodeNameResolver;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
Expand All @@ -16,6 +15,7 @@
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\CallAnalyzer;
use Rector\Core\Util\StringUtils;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeNameResolver\Error\InvalidNameNodeReporter;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
Expand Down Expand Up @@ -193,7 +193,7 @@ public function isLocalPropertyFetchNamed(Node $node, string $name): bool
public function endsWith(string $currentName, string $expectedName): bool
{
$suffixNamePattern = '#\w+' . ucfirst($expectedName) . '#';
return (bool) Strings::match($currentName, $suffixNamePattern);
return StringUtils::isMatch($currentName, $suffixNamePattern);
}

public function getShortName(string | Name | Identifier | ClassLike $name): string
Expand All @@ -218,7 +218,7 @@ public function isStringName(string $resolvedName, string $desiredName): bool

// is probably regex pattern
if ($this->regexPatternDetector->isRegexPattern($desiredName)) {
return (bool) Strings::match($resolvedName, $desiredName);
return StringUtils::isMatch($resolvedName, $desiredName);
}

// is probably fnmatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\NodeTypeResolver\PHPStan\Scope;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -24,6 +23,7 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\StaticReflection\SourceLocator\ParentAttributeSourceLocator;
use Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
Expand Down Expand Up @@ -142,7 +142,7 @@ private function resolveClassOrInterfaceScope(
$className = $this->resolveClassName($classLike);

// is anonymous class? - not possible to enter it since PHPStan 0.12.33, see https://github.com/phpstan/phpstan-src/commit/e87fb0ec26f9c8552bbeef26a868b1e5d8185e91
if ($classLike instanceof Class_ && Strings::match($className, self::ANONYMOUS_CLASS_START_REGEX)) {
if ($classLike instanceof Class_ && StringUtils::isMatch($className, self::ANONYMOUS_CLASS_START_REGEX)) {
$classReflection = $this->reflectionProvider->getAnonymousClassReflection($classLike, $mutatingScope);
} elseif (! $this->reflectionProvider->hasClass($className)) {
return $mutatingScope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ private function isParentClassLocked(ClassReflection $classReflection, string $p
// validate parent not typed yet → it's not ok
return true;
}

continue;
}

return false;
Expand Down
20 changes: 20 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,23 @@ parameters:

# @todo fix in symplify
- '#Parameter \#3 \$configuration of class Symplify\\RuleDocGenerator\\ValueObject\\CodeSample\\ConfiguredCodeSample constructor expects array<string, mixed\>, array<int, Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration\> given#'

# strict rules
- '#Dynamic call to static method PHPUnit\\Framework\\Assert\:\:(.*?)\(\)#'

# specific generics
- '#Parameter \#1 \$node (.*?) of method Rector\\(.*?)Rector\:\:refactor\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#'

# only for empty ctor
- '#Rector\\Core\\PhpParser\\NodeTraverser\\RectorNodeTraverser\:\:__construct\(\) does not call parent constructor from PhpParser\\NodeTraverser#'

-
message: '#Class has a static method must so must contains "Static" in its name#'
path: src/Util/StringUtils.php

# generics
- '#Method Rector\\Core\\PhpParser\\NodeTraverser\\RectorNodeTraverser\:\:traverse\(\) should return array<TNode of PhpParser\\Node\> but returns array<PhpParser\\Node\>#'
- '#Parameter \#1 \$stmts of class Rector\\Core\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace constructor expects array<PhpParser\\Node\\Stmt\>, array<TNode of PhpParser\\Node\> given#'

# skip === false compare
- '#Do not compare call directly, use a variable assign#'
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Rector\Autodiscovery\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\Doctrine\PhpDocParser\DoctrineDocBlockResolver;
use Rector\FileSystemRector\ValueObject\AddedFileWithNodes;
use Rector\FileSystemRector\ValueObjectFactory\AddedFileWithNodesFactory;
Expand Down Expand Up @@ -89,7 +89,7 @@ public function refactor(Node $node): ?Node

// is entity in expected directory?
$smartFileInfo = $this->file->getSmartFileInfo();
if (Strings::match($smartFileInfo->getRealPath(), self::ENTITY_PATH_REGEX)) {
if (StringUtils::isMatch($smartFileInfo->getRealPath(), self::ENTITY_PATH_REGEX)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Rector\Autodiscovery\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Autodiscovery\FileLocation\ExpectedFileLocationResolver;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;
use Rector\FileSystemRector\ValueObject\AddedFileWithNodes;
use Rector\FileSystemRector\ValueObjectFactory\AddedFileWithNodesFactory;
Expand Down Expand Up @@ -115,7 +115,7 @@ private function processGroupNamesBySuffix(SmartFileInfo $smartFileInfo, array $
foreach ($groupNamesBySuffix as $groupNames) {
// has class suffix
$suffixPattern = '\w+' . $groupNames . '(Test)?\.php$';
if (! Strings::match($smartFileInfo->getRealPath(), '#' . $suffixPattern . '#')) {
if (! StringUtils::isMatch($smartFileInfo->getRealPath(), '#' . $suffixPattern . '#')) {
continue;
}

Expand All @@ -124,7 +124,7 @@ private function processGroupNamesBySuffix(SmartFileInfo $smartFileInfo, array $
}

// file is already in the group
if (Strings::match($smartFileInfo->getPath(), '#' . $groupNames . '$#')) {
if (StringUtils::isMatch($smartFileInfo->getPath(), '#' . $groupNames . '$#')) {
continue;
}

Expand All @@ -140,7 +140,7 @@ private function isLocatedInExpectedLocation(
): bool {
$expectedLocationFilePattern = $this->expectedFileLocationResolver->resolve($groupName, $suffixPattern);

return (bool) Strings::match($smartFileInfo->getRealPath(), $expectedLocationFilePattern);
return StringUtils::isMatch($smartFileInfo->getRealPath(), $expectedLocationFilePattern);
}

private function moveFileToGroupName(SmartFileInfo $fileInfo, File $file, string $desiredGroupName): void
Expand Down
4 changes: 2 additions & 2 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodingStyle\ClassNameImport;

use Nette\Utils\Reflection;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand All @@ -19,6 +18,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -171,7 +171,7 @@ private function resolveFromStmtsDocBlocks(array $stmts): array
function ($node) use (&$shortNames) {
if ($node instanceof PhpDocTagNode) {
$shortName = trim($node->name, '@');
if (Strings::match($shortName, self::BIG_LETTER_START_REGEX)) {
if (StringUtils::isMatch($shortName, self::BIG_LETTER_START_REGEX)) {
$shortNames[] = $shortName;
}

Expand Down
4 changes: 2 additions & 2 deletions rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StringUtils;

final class UseImportNameMatcher
{
Expand Down Expand Up @@ -58,8 +59,7 @@ private function isUseMatchingName(string $tag, UseUse $useUse): bool
$shortName = $useUse->alias !== null ? $useUse->alias->name : $useUse->name->getLast();
$shortNamePattern = preg_quote($shortName, '#');
$pattern = sprintf(self::SHORT_NAME_REGEX, $shortNamePattern);

return (bool) Strings::match($tag, $pattern);
return StringUtils::isMatch($tag, $pattern);
}

private function resolveName(string $tag, UseUse $useUse): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\CodingStyle\ValueObject\ConcatExpressionJoinData;
use Rector\CodingStyle\ValueObject\NodeToRemoveAndConcatItem;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -168,7 +169,7 @@ public function refactor(Node $node): ?Node

private function isJsonString(string $stringValue): bool
{
if (! (bool) Strings::match($stringValue, self::JSON_STRING_REGEX)) {
if (! StringUtils::isMatch($stringValue, self::JSON_STRING_REGEX)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function refactor(Node $node): ?Node

$varName = '$' . $this->getName($node->var);
$varPattern = '# ' . preg_quote($varName, '#') . ' #';
if (! Strings::match($docContent, $varPattern)) {
if (! StringUtils::isMatch($docContent, $varPattern)) {
return null;
}

Expand Down Expand Up @@ -161,7 +162,7 @@ private function createDocComment(Node $node): Doc
}

// $value is first, instead of type is first
if (Strings::match($docContent, self::VAR_ANNOTATION_REGEX)) {
if (StringUtils::isMatch($docContent, self::VAR_ANNOTATION_REGEX)) {
$docContent = Strings::replace($docContent, self::VARIABLE_NAME_AND_TYPE_MATCH_REGEX, '$3$2$1');
}

Expand Down

0 comments on commit f985e81

Please sign in to comment.