Skip to content

Commit

Permalink
[CS] Fix dynamic and broken indent detection, allow to configure spac…
Browse files Browse the repository at this point in the history
…ing via RectorConfig::indent() method (#2442)
  • Loading branch information
TomasVotruba authored Jun 6, 2022
1 parent 1c64bb1 commit 38a9718
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 161 deletions.
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

$rectorConfig->disableImportNames();
$rectorConfig->importShortClasses();
$rectorConfig->indent(' ', 4);

$rectorConfig->fileExtensions(['php']);
$rectorConfig->nestedChainMethodCallLimit(60);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ private function _resolveTagFullyQualifiedName(
/**
* @param Use_[]|GroupUse[] $uses
*/
private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag, bool $returnNullOnUnknownClass): ?string
{
private function resolveFullyQualifiedClass(
array $uses,
Node $node,
string $tag,
bool $returnNullOnUnknownClass
): ?string {
$scope = $node->getAttribute(AttributeKey::SCOPE);

if ($scope instanceof Scope) {
Expand All @@ -88,7 +92,7 @@ private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag
return $this->resolveAsAliased($uses, $tag, $returnNullOnUnknownClass);
}

if (str_starts_with($tag, '\\') && $this->reflectionProvider->hasClass($tag)) {
if ($this->isPreslashedExistingClass($tag)) {
// Global or absolute Class
return $tag;
}
Expand Down Expand Up @@ -127,10 +131,20 @@ private function resolveAsAliased(array $uses, string $tag, bool $returnNullOnUn

private function resolveClass(?string $class, bool $returnNullOnUnknownClass): ?string
{
if (null === $class) {
if ($class === null) {
return null;
}

$resolvedClass = $this->reflectionProvider->hasClass($class) ? $class : null;
return $returnNullOnUnknownClass ? $resolvedClass : $class;
}

private function isPreslashedExistingClass(string $tag): bool
{
if (! str_starts_with($tag, '\\')) {
return false;
}

return $this->reflectionProvider->hasClass($tag);
}
}
10 changes: 10 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,14 @@ public function cacheClass(string $cacheClass): void
$parameters = $this->parameters();
$parameters->set(Option::CACHE_CLASS, $cacheClass);
}

/**
* @see https://github.com/nikic/PHP-Parser/issues/723#issuecomment-712401963
*/
public function indent(string $character, int $count): void
{
$parameters = $this->parameters();
$parameters->set(Option::INDENT_CHAR, $character);
$parameters->set(Option::INDENT_SIZE, $count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi

final class FixtureRemoveMethodCallOnThis
{
public function __construct()
{
$this->validateLineLengths();
}

protected function validateLineLengths(): void
{
}
public function __construct()
{
$this->validateLineLengths();
}

protected function validateLineLengths(): void
{
}
}

?>
Expand All @@ -22,13 +22,13 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi

final class FixtureRemoveMethodCallOnThis
{
public function __construct()
{
}
public function __construct()
{
}

protected function validateLineLengths(): void
{
}
protected function validateLineLengths(): void
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi

abstract class Validator
{
protected function validateLineLengths(): void
{
}
protected function validateLineLengths(): void
{
}
}

final class GeneratorStubUseAbstract extends Validator
{
public function __construct()
{
$this->validateLineLengths();
}
public function __construct()
{
$this->validateLineLengths();
}
}

?>
Expand All @@ -25,16 +25,16 @@ namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fi

abstract class Validator
{
protected function validateLineLengths(): void
{
}
protected function validateLineLengths(): void
{
}
}

final class GeneratorStubUseAbstract extends Validator
{
public function __construct()
{
}
public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ class Z {}

final class CopyDoc
{
public Y $y;

/**
* @var Z[]
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = [];

/**
* @param Z[] $z
*/
public function __construct(Y $y, array $z = [])
{
$this->y = $y;
$this->z = $z;
}
public Y $y;

/**
* @var Z[]
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = [];

/**
* @param Z[] $z
*/
public function __construct(Y $y, array $z = [])
{
$this->y = $y;
$this->z = $z;
}
}
?>
-----
Expand All @@ -40,18 +40,18 @@ class Z {}

final class CopyDoc
{
/**
* @param Z[] $z
*/
public function __construct(
public Y $y,
/**
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = []
)
{
}
/**
* @param Z[] $z
*/
public function __construct(
public Y $y,
/**
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = []
)
{
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo
*/
final class Generic
{
/**
* @var T
*/
public mixed $value;
/**
* @var T
*/
public mixed $value;

/**
* @param T $value
*/
public function __construct(mixed $value)
{
$this->value = $value;
}
/**
* @param T $value
*/
public function __construct(mixed $value)
{
$this->value = $value;
}
}
?>
-----
Expand All @@ -31,11 +31,11 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo
*/
final class Generic
{
/**
* @param T $value
*/
public function __construct(public mixed $value)
{
}
/**
* @param T $value
*/
public function __construct(public mixed $value)
{
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo

class IterableTyped
{
/**
* @var iterable<string>
*/
private iterable $property;
/**
* @var iterable<string>
*/
private iterable $property;

/**
* @param iterable<string> $property
*/
public function __construct(iterable $property)
{
$this->property = $property;
}
/**
* @param iterable<string> $property
*/
public function __construct(iterable $property)
{
$this->property = $property;
}
}
?>
-----
Expand All @@ -25,11 +25,11 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo

class IterableTyped
{
/**
* @param iterable<string> $property
*/
public function __construct(private iterable $property)
{
}
/**
* @param iterable<string> $property
*/
public function __construct(private iterable $property)
{
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class Z {}

final class UnionFullyQualified
{
public Y $y;
public Y $y;

public Z $z;
public Z $z;

public function __construct(Y $y, Z $z)
{
$this->y = $y;
$this->z = $z;
}
public function __construct(Y $y, Z $z)
{
$this->y = $y;
$this->z = $z;
}

public function getX(): X
{
return $this->y;
}
public function getX(): X
{
return $this->y;
}
}
?>
-----
Expand All @@ -39,13 +39,13 @@ class Z {}

final class UnionFullyQualified
{
public function __construct(public Y $y, public Z $z)
{
}

public function getX(): X
{
return $this->y;
}
public function __construct(public Y $y, public Z $z)
{
}

public function getX(): X
{
return $this->y;
}
}
?>
Loading

0 comments on commit 38a9718

Please sign in to comment.