Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CS] Fix dynamic and broken indent detection, allow to configure spacing via RectorConfig::indent() method #2442

Merged
merged 3 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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