Skip to content

Commit

Permalink
Let Rector upgrade itself to PHP 8 πŸ€–πŸ˜‡ (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 29, 2021
1 parent 763bce6 commit b90952f
Show file tree
Hide file tree
Showing 90 changed files with 162 additions and 169 deletions.
9 changes: 9 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);

$services = $containerConfigurator->services();
$services->set(RemoveInterfacesRector::class)
->call('configure', [[
RemoveInterfacesRector::INTERFACES_TO_REMOVE => [
// @see https://wiki.php.net/rfc/stringable
'Stringable',
],
]]);

$services->set(DowngradeUnionTypeTypedPropertyRector::class);
$services->set(DowngradeUnionTypeDeclarationRector::class);
$services->set(DowngradeMixedTypeDeclarationRector::class);
Expand Down
4 changes: 2 additions & 2 deletions config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
$services->set(StrEndsWithRector::class);

$services->set(StaticCallToFuncCallRector::class)
->call('configure', [
->call('configure', [[
StaticCallToFuncCallRector::STATIC_CALLS_TO_FUNCTIONS => ValueObjectInliner::inline([
new StaticCallToFuncCall('Nette\Utils\Strings', 'startsWith', 'str_starts_with'),
new StaticCallToFuncCall('Nette\Utils\Strings', 'endsWith', 'str_ends_with'),
new StaticCallToFuncCall('Nette\Utils\Strings', 'contains', 'str_contains'),
]),
]);
]]);

$services->set(StringableForToStringRector::class);
$services->set(ClassOnObjectRector::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Rector\BetterPhpDocParser\PhpDoc;

use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\AbstractValuesAwareNode;
use Stringable;

final class DoctrineAnnotationTagValueNode extends AbstractValuesAwareNode
final class DoctrineAnnotationTagValueNode extends AbstractValuesAwareNode implements Stringable
{
/**
* @param array<mixed, mixed> $values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Rector\BetterPhpDocParser\PhpDoc;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Stringable;

/**
* Useful for annotation class based annotation, e.g. @ORM\Entity to prevent space
* between the @ORM\Entity and (someContent)
*/
final class SpacelessPhpDocTagNode extends PhpDocTagNode
final class SpacelessPhpDocTagNode extends PhpDocTagNode implements Stringable
{
public function __toString(): string
{
Expand Down
2 changes: 1 addition & 1 deletion packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagV

private function isFnmatch(string $currentValue, string $desiredValue): bool
{
if (! Strings::contains($desiredValue, '*')) {
if (! \str_contains($desiredValue, '*')) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function resolveName(string $tag, UseUse $useUse): string
}

$unaliasedShortClass = Strings::substring($tag, Strings::length($useUse->alias->toString()));
if (Strings::startsWith($unaliasedShortClass, '\\')) {
if (\str_starts_with($unaliasedShortClass, '\\')) {
return $useUse->name . $unaliasedShortClass;
}

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

namespace Rector\BetterPhpDocParser\PhpDocParser;

use Nette\Utils\Strings;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
Expand Down Expand Up @@ -142,7 +141,7 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
);

// not an annotations class
if (! Strings::contains($fullyQualifiedAnnotationClass, '\\')) {
if (! \str_contains($fullyQualifiedAnnotationClass, '\\')) {
continue;
}

Expand Down Expand Up @@ -193,15 +192,15 @@ private function isClosedContent(string $composedContent): bool
if ($composedTokenIterator->isCurrentTokenTypes([
Lexer::TOKEN_OPEN_CURLY_BRACKET,
Lexer::TOKEN_OPEN_PARENTHESES,
]) || Strings::contains($composedTokenIterator->currentTokenValue(), '(')) {
]) || \str_contains($composedTokenIterator->currentTokenValue(), '(')) {
++$openBracketCount;
}

if ($composedTokenIterator->isCurrentTokenTypes([
Lexer::TOKEN_CLOSE_CURLY_BRACKET,
Lexer::TOKEN_CLOSE_PARENTHESES,
// sometimes it gets mixed int ")
]) || Strings::contains($composedTokenIterator->currentTokenValue(), ')')) {
]) || \str_contains($composedTokenIterator->currentTokenValue(), ')')) {
++$closeBracketCount;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ private function addTokensFromTo(string $output, int $from, int $to, bool $shoul

// skip extra empty lines above if this is the last one
if ($shouldSkipEmptyLinesAbove &&
Strings::contains($this->tokens[$from][0], PHP_EOL) &&
Strings::contains($this->tokens[$from + 1][0], PHP_EOL)
\str_contains($this->tokens[$from][0], PHP_EOL) &&
\str_contains($this->tokens[$from + 1][0], PHP_EOL)
) {
++$from;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation;

final class CurlyListNode extends AbstractValuesAwareNode
use Stringable;

final class CurlyListNode extends AbstractValuesAwareNode implements Stringable
{
public function __toString(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Stringable;

final class SpacingAwareTemplateTagValueNode extends TemplateTagValueNode
final class SpacingAwareTemplateTagValueNode extends TemplateTagValueNode implements Stringable
{
public function __construct(
string $name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Rector\BetterPhpDocParser\ValueObject\PhpDoc;

use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use Stringable;

final class VariadicAwareParamTagValueNode extends ParamTagValueNode
final class VariadicAwareParamTagValueNode extends ParamTagValueNode implements Stringable
{
public function __toString(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use Rector\Core\Exception\ShouldNotHappenException;
use Stringable;

/**
* @deprecated
* Just for back compatibility
*/
abstract class AbstractTagValueNode implements Node
abstract class AbstractTagValueNode implements Node, Stringable
{
use NodeAttributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Rector\BetterPhpDocParser\ValueObject\Type;

use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use Stringable;

final class BracketsAwareIntersectionTypeNode extends IntersectionTypeNode
final class BracketsAwareIntersectionTypeNode extends IntersectionTypeNode implements Stringable
{
public function __toString(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Stringable;

final class BracketsAwareUnionTypeNode extends UnionTypeNode
final class BracketsAwareUnionTypeNode extends UnionTypeNode implements Stringable
{
/**
* @param TypeNode[] $types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Stringable;

final class EmptyGenericTypeNode extends GenericTypeNode
final class EmptyGenericTypeNode extends GenericTypeNode implements Stringable
{
public function __construct(IdentifierTypeNode $identifierTypeNode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Rector\BetterPhpDocParser\ValueObject\Type;

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Stringable;

final class FullyQualifiedIdentifierTypeNode extends IdentifierTypeNode
final class FullyQualifiedIdentifierTypeNode extends IdentifierTypeNode implements Stringable
{
public function __toString(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;

use Rector\PHPStanStaticTypeMapper\TypeMapper\ArrayTypeMapper;

final class SpacingAwareArrayTypeNode extends ArrayTypeNode
use Stringable;

final class SpacingAwareArrayTypeNode extends ArrayTypeNode implements Stringable
{
public function __toString(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Rector\BetterPhpDocParser\ValueObject\Type;

use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Stringable;

final class SpacingAwareCallableTypeNode extends CallableTypeNode
final class SpacingAwareCallableTypeNode extends CallableTypeNode implements Stringable
{
use NodeAttributes;

Expand All @@ -28,7 +28,7 @@ private function createExplicitCallable(): string
$parameterTypeString = $this->createParameterTypeString();

$returnTypeAsString = (string) $returnType;
if (Strings::contains($returnTypeAsString, '|')) {
if (\str_contains($returnTypeAsString, '|')) {
$returnTypeAsString = '(' . $returnTypeAsString . ')';
}

Expand Down
6 changes: 3 additions & 3 deletions packages/FileFormatter/Formatter/XmlFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function formatXml(string $xml): string

$parts = $this->getXmlParts($xml);

if (strpos($parts[0], '<?xml') === 0) {
if (str_starts_with($parts[0], '<?xml')) {
$output = array_shift($parts) . PHP_EOL;
}

Expand Down Expand Up @@ -151,11 +151,11 @@ private function isClosingTag(string $part): bool

private function isOpeningCdataTag(string $part): bool
{
return Strings::contains($part, '<![CDATA[');
return \str_contains($part, '<![CDATA[');
}

private function isClosingCdataTag(string $part): bool
{
return Strings::contains($part, ']]>');
return \str_contains($part, ']]>');
}
}
5 changes: 3 additions & 2 deletions packages/FileFormatter/ValueObject/Indent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
use Rector\FileFormatter\Exception\InvalidIndentStringException;
use Rector\FileFormatter\Exception\InvalidIndentStyleException;
use Rector\FileFormatter\Exception\ParseIndentException;
use Stringable;

/**
* @see \Rector\Tests\FileFormatter\ValueObject\IndentTest
*/
final class Indent
final class Indent implements Stringable
{
/**
* @var array<string, string>
Expand Down Expand Up @@ -122,6 +123,6 @@ public function getIndentStyleCharacter(): string

private function startsWithSpace(): bool
{
return Strings::startsWith($this->string, ' ');
return \str_starts_with($this->string, ' ');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function createWithDesiredGroup(

// is already in the right group
$currentNamespaceName = $currentNamespace->name->toString();
if (Strings::endsWith($currentNamespaceName, '\\' . $desiredGroupName)) {
if (\str_ends_with($currentNamespaceName, '\\' . $desiredGroupName)) {
return null;
}

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

use Nette\Utils\Arrays;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -158,7 +157,7 @@ public function findClassMethodByStaticCall(StaticCall $staticCall): ?ClassMetho

public function findClassMethod(string $className, string $methodName): ?ClassMethod
{
if (Strings::contains($methodName, '\\')) {
if (\str_contains($methodName, '\\')) {
$message = sprintf('Class and method arguments are switched in "%s"', __METHOD__);
throw new ShouldNotHappenException($message);
}
Expand Down Expand Up @@ -282,7 +281,7 @@ public function findClassesBySuffix(string $suffix): array
$classNodes = [];

foreach ($this->parsedNodeCollector->getClasses() as $className => $classNode) {
if (! Strings::endsWith($className, $suffix)) {
if (! \str_ends_with($className, $suffix)) {
continue;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/NodeCollector/NodeCollector/ParsedNodeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\NodeCollector\NodeCollector;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
Expand Down Expand Up @@ -117,7 +116,7 @@ public function findTrait(string $name): ?Trait_
public function findByShortName(string $shortName): ?Class_
{
foreach ($this->classes as $className => $classNode) {
if (Strings::endsWith($className, '\\' . $shortName)) {
if (\str_ends_with($className, '\\' . $shortName)) {
return $classNode;
}
}
Expand All @@ -127,7 +126,7 @@ public function findByShortName(string $shortName): ?Class_

public function findClassConstant(string $className, string $constantName): ?ClassConst
{
if (Strings::contains($constantName, '\\')) {
if (\str_contains($constantName, '\\')) {
throw new ShouldNotHappenException(sprintf('Switched arguments in "%s"', __METHOD__));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private function isSingleName(Node $node, string $name): bool
}

// is probably fnmatch
if (Strings::contains($name, '*')) {
if (\str_contains($name, '*')) {
return fnmatch($name, $resolvedName, FNM_NOESCAPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private function resolveDependentFiles(Node $node, MutatingScope $mutatingScope)
foreach ($dependentFiles as $dependentFile) {
$this->dependentFiles[] = $dependentFile;
}
} catch (AnalysedCodeException $analysedCodeException) {
} catch (AnalysedCodeException) {
// @ignoreException
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function shouldSkip(
return true;
}

if (! Strings::startsWith($staticType->getClassName(), $pseudoNamespaceToNamespace->getNamespacePrefix())) {
if (! \str_starts_with($staticType->getClassName(), $pseudoNamespaceToNamespace->getNamespacePrefix())) {
return true;
}

Expand Down
Loading

0 comments on commit b90952f

Please sign in to comment.