Skip to content

Commit

Permalink
[CodingStyle] Add NewlineAfterStatementRector (#787)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 30, 2021
1 parent 13bf954 commit a81fd4e
Show file tree
Hide file tree
Showing 158 changed files with 608 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/set/coding-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\CodingStyle\Rector\Plus\UseIncrementAssignRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\CodingStyle\Rector\Property\AddFalseDefaultToBoolPropertyRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector;
Expand Down Expand Up @@ -76,4 +77,5 @@
$services->set(RemoveDoubleUnderscoreInMethodNameRector::class);
$services->set(PostIncDecToPreIncDecRector::class);
$services->set(UnSpreadOperatorRector::class);
$services->set(NewlineAfterStatementRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ private function matchConstantValue(string $currentTokenValue): ConstExprNode |
if (strtolower($currentTokenValue) === 'true') {
return new ConstExprTrueNode();
}

if (! is_numeric($currentTokenValue)) {
return null;
}

if ((string) (int) $currentTokenValue !== $currentTokenValue) {
return null;
}

return new ConstExprIntegerNode($currentTokenValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ private function normalizeParameterType(string $parameterTypeString, string $ret
if ($parameterTypeString !== '') {
return '(' . $parameterTypeString . ')';
}

if ($returnTypeAsString === 'mixed') {
return $parameterTypeString;
}

if ($returnTypeAsString === '') {
return $parameterTypeString;
}

return '()';
}

Expand All @@ -69,9 +72,11 @@ private function normalizeReturnType(string $parameterTypeString, string $return
if ($returnTypeAsString !== 'mixed') {
return ':' . $returnTypeAsString;
}

if ($parameterTypeString !== '') {
return ':' . $returnTypeAsString;
}

return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getNext(): ?File
if ($this->affectedFiles !== []) {
return current($this->affectedFiles);
}

return null;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ public function getPossibleUnionPropertyType(
if ($ancestor->isSubclassOf('PHPUnit\Framework\TestCase')) {
continue;
}

if ($nodes === null) {
continue;
}

if (! $this->isPropertyWritten($nodes, $propertyName, $kindPropertyFetch)) {
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/FileFormatter/Formatter/XmlFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ private function runPost(string $part): void
if ($this->isOpeningTag($part)) {
++$this->depth;
}

if ($this->isClosingCdataTag($part)) {
$this->preserveWhitespace = false;
}

if ($this->isOpeningCdataTag($part)) {
$this->preserveWhitespace = true;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ public function isName(Node | array $node, string $name): bool
if ($node instanceof MethodCall) {
return false;
}

if ($node instanceof StaticCall) {
return false;
}

$nodes = is_array($node) ? $node : [$node];

foreach ($nodes as $node) {
Expand All @@ -80,6 +82,7 @@ public function isCaseSensitiveName(Node $node, string $name): bool
if ($node instanceof MethodCall) {
return false;
}

if ($node instanceof StaticCall) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function resolve(Node $node): ?string
if ($class === null) {
return null;
}

if ($name === null) {
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/NodeNestingScope/ScopeNestingComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ public function isNodeConditionallyScoped(Expr $expr): bool
if ($this->isInBothIfElseBranch($foundParent, $expr)) {
return false;
}

if (! $foundParent instanceof Else_) {
return ! $foundParent instanceof FunctionLike;
}

if (! $this->nodeComparator->areNodesEqual($expr, $this->doubleIfBranchExprs)) {
return ! $foundParent instanceof FunctionLike;
}

return false;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/NodeRemoval/BreakingRemovalGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ public function isLegalNodeRemoval(Node $node): bool
if ($parent instanceof BooleanNot) {
$parent = $parent->getAttribute(AttributeKey::PARENT_NODE);
}

if ($parent instanceof Assign) {
return false;
}

if ($this->isIfCondition($node)) {
return false;
}

return ! $this->isWhileCondition($node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private function getVariableUsages(Variable $variable): array
if (! $node instanceof Variable) {
return false;
}

return $node->name === $variable->name;
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ private function resolveObjectType(ObjectType $resolvedObjectType, ObjectType $r
if (! $this->isObjectTypeOfObjectType($renamedObjectType, $requiredObjectType)) {
return $this->isObjectTypeOfObjectType($resolvedObjectType, $requiredObjectType);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private function resolveFromParentNodes(Variable $variable): ?Scope

return $parentNodeScope;
}

return null;
}
}
6 changes: 6 additions & 0 deletions packages/NodeTypeResolver/TypeAnalyzer/ArrayTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ private function isIntersectionArrayType(Type $nodeType): bool
if ($intersectionNodeType instanceof ArrayType) {
continue;
}

if ($intersectionNodeType instanceof HasOffsetType) {
continue;
}

if ($intersectionNodeType instanceof NonEmptyArrayType) {
continue;
}

return false;
}

Expand All @@ -98,6 +101,7 @@ private function isPropertyFetchWithArrayDefault(Node $node): bool
if ($classLike instanceof Interface_) {
return false;
}

if ($classLike === null) {
return false;
}
Expand All @@ -119,9 +123,11 @@ private function isPropertyFetchWithArrayDefault(Node $node): bool
} else {
$propertyOwnerStaticType = $this->nodeTypeResolver->resolve($node->class);
}

if ($propertyOwnerStaticType instanceof ThisType) {
return false;
}

return $propertyOwnerStaticType instanceof TypeWithClassName;
}
}
4 changes: 4 additions & 0 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $second
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType && $firstType->getFullyQualifiedClass() === $secondType->getClassName()) {
return true;
}

if (! $secondType instanceof AliasedObjectType) {
return false;
}

if (! $firstType instanceof ObjectType) {
return false;
}

return $secondType->getFullyQualifiedClass() === $firstType->getClassName();
}

Expand All @@ -119,6 +122,7 @@ private function areArrayTypeWithSingleObjectChildToParent(Type $firstType, Type
if (! $firstType instanceof ArrayType) {
return false;
}

if (! $secondType instanceof ArrayType) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,19 @@ public function isScalar(UnionType $unionType): bool
if ($type instanceof StringType && ! $type instanceof ConstantStringType) {
continue;
}

if ($type instanceof FloatType) {
continue;
}

if ($type instanceof IntegerType) {
continue;
}

if ($type instanceof BooleanType) {
continue;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ private function shouldSkipIterable(UnionType $unionType): bool
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
return false;
}

if (! $unionTypeAnalysis->hasIterable()) {
return false;
}

return $unionTypeAnalysis->hasArray();
}

Expand Down
1 change: 1 addition & 0 deletions packages/PostRector/Rector/NodeRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private function isChainMethodCallNodeToBeRemoved(
if (! $mainMethodCall->var instanceof MethodCall) {
return false;
}

if ($toBeRemovedMethodCall !== $mainMethodCall->var) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/PostRector/Rector/PropertyAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public function enterNode(Node $node): ?Node
if (! $node instanceof Class_) {
return null;
}

if ($this->classAnalyzer->isAnonymousClass($node)) {
return null;
}

$this->addConstants($node);
$this->addProperties($node);
$this->addPropertiesWithoutConstructor($node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function doTestFileInfoWithAdditionalChanges(
if (trim($expectedContent)) {
$fixtureContent .= $separator . $expectedContent;
}

FileSystem::write($fixturePath, $fixtureContent);
$newFileInfo = new SmartFileInfo($fixturePath);
$this->doTestFileInfo($newFileInfo, $allowMatches);
Expand All @@ -55,6 +56,7 @@ private function prepareAdditionalChangedFiles(array $additionalFileChanges): ar
if ($path === null) {
throw new ShouldNotHappenException('Path for additional change must be set');
}

$fullPath = $this->getFixtureTempDirectory() . '/' . $path;

$input = isset($additionalFileChange[1]) ? trim($additionalFileChange[1]) : null;
Expand All @@ -65,6 +67,7 @@ private function prepareAdditionalChangedFiles(array $additionalFileChanges): ar

$expectedFileChanges[$fullPath] = isset($additionalFileChange[2]) ? trim($additionalFileChange[2]) : '';
}

return $expectedFileChanges;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

class NoNewLine
{
private $property;
private $property2;
private const VIEW = 'view';
private const ADD = 'add';
public function run(array $data)
{
if (rand(0, 1)) {

}
echo 'test';

foreach ($data as $key => $value) {
# code...
}
return true;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

class NoNewLine
{
private $property;

private $property2;

private const VIEW = 'view';

private const ADD = 'add';

public function run(array $data)
{
if (rand(0, 1)) {

}

echo 'test';

foreach ($data as $key => $value) {
# code...
}

return true;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

function run()
{
}
function run2()
{
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

function run()
{
}

function run2()
{
}

?>

0 comments on commit a81fd4e

Please sign in to comment.