Skip to content

Commit

Permalink
apply naming
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 9, 2023
1 parent 7bc0dc4 commit 7ebaf11
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 43 deletions.
8 changes: 2 additions & 6 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,14 @@ public function getNames(array $nodes): array
/**
* @param string|string[] $suffix
*/
public function endsWith(\PhpParser\Node $node, string|array $suffix): bool
public function endsWith(Node $node, string|array $suffix): bool
{
$name = $this->getName($node);
if (! is_string($name)) {
return false;
}

if (! is_array($suffix)) {
$suffixes = [$suffix];
} else {
$suffixes = $suffix;
}
$suffixes = is_array($suffix) ? $suffix : [$suffix];

foreach ($suffixes as $suffix) {
if (str_ends_with($name, $suffix) || str_ends_with($name, ucfirst($suffix))) {
Expand Down
24 changes: 12 additions & 12 deletions rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ public function __construct(
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Shortens if return false/true to direct return',
'Shortens if nextReturn false/true to direct nextReturn',
[
new CodeSample(
<<<'CODE_SAMPLE'
if (strpos($docToken->getContent(), "\n") === false) {
return true;
nextReturn true;
}
return false;
nextReturn false;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return strpos($docToken->getContent(), "\n") === false;
nextReturn strpos($docToken->getContent(), "\n") === false;
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
* @nextReturn array<class-string<Node>>
*/
public function getNodeTypes(): array
{
Expand Down Expand Up @@ -154,30 +154,30 @@ private function shouldSkipIfAndReturn(If_ $if, Return_ $return): bool
return ! $if->cond instanceof NotIdentical;
}

private function processReturnTrue(If_ $if, Return_ $nextReturnNode): Return_
private function processReturnTrue(If_ $if, Return_ $nextReturn): Return_
{
if ($if->cond instanceof BooleanNot && $nextReturnNode->expr instanceof Expr && $this->valueResolver->isTrue(
$nextReturnNode->expr
if ($if->cond instanceof BooleanNot && $nextReturn->expr instanceof Expr && $this->valueResolver->isTrue(
$nextReturn->expr
)) {
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond->expr));
}

return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond));
}

private function processReturnFalse(If_ $if, Return_ $nextReturnNode): ?Return_
private function processReturnFalse(If_ $if, Return_ $nextReturn): ?Return_
{
if ($if->cond instanceof Identical) {
$notIdentical = new NotIdentical($if->cond->left, $if->cond->right);

return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($notIdentical));
}

if (! $nextReturnNode->expr instanceof Expr) {
if (! $nextReturn->expr instanceof Expr) {
return null;
}

if (! $this->valueResolver->isTrue($nextReturnNode->expr)) {
if (! $this->valueResolver->isTrue($nextReturn->expr)) {
return null;
}

Expand Down Expand Up @@ -221,7 +221,7 @@ private function isIfWithSingleReturnExpr(If_ $if): bool
return false;
}

// return must have value
// nextReturn must have value
return $ifInnerNode->expr instanceof Expr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ private function processNestedIfsWithNonBreaking(Foreach_ $foreach, array $neste
return $foreach;
}

private function addInvertedIfStmtWithContinue(If_ $nestedIfWithOnlyReturn, Foreach_ $foreach): void
private function addInvertedIfStmtWithContinue(If_ $if, Foreach_ $foreach): void
{
$invertedCondExpr = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);
$invertedCondExpr = $this->conditionInverter->createInvertedCondition($if->cond);

// special case
if ($invertedCondExpr instanceof BooleanNot && $invertedCondExpr->expr instanceof BooleanAnd) {
Expand All @@ -147,18 +147,18 @@ private function addInvertedIfStmtWithContinue(If_ $nestedIfWithOnlyReturn, Fore
}

// should skip for weak inversion
if ($this->isBooleanOrWithWeakComparison($nestedIfWithOnlyReturn->cond)) {
$foreach->stmts[] = $nestedIfWithOnlyReturn;
if ($this->isBooleanOrWithWeakComparison($if->cond)) {
$foreach->stmts[] = $if;

return;
}

$nestedIfWithOnlyReturn->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$if->setAttribute(AttributeKey::ORIGINAL_NODE, null);

$nestedIfWithOnlyReturn->cond = $invertedCondExpr;
$nestedIfWithOnlyReturn->stmts = [new Continue_()];
$if->cond = $invertedCondExpr;
$if->stmts = [new Continue_()];

$foreach->stmts[] = $nestedIfWithOnlyReturn;
$foreach->stmts[] = $if;
}

/**
Expand Down
26 changes: 13 additions & 13 deletions rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ public function __construct(

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Changes if && to early return', [
return new RuleDefinition('Changes if && to early ifNextReturn', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function canDrive(Car $car)
{
if ($car->hasWheels && $car->hasFuel) {
return true;
ifNextReturn true;
}
return false;
ifNextReturn false;
}
}
CODE_SAMPLE
Expand All @@ -68,14 +68,14 @@ class SomeClass
public function canDrive(Car $car)
{
if (! $car->hasWheels) {
return false;
ifNextReturn false;
}
if (! $car->hasFuel) {
return false;
ifNextReturn false;
}
return true;
ifNextReturn true;
}
}
CODE_SAMPLE
Expand All @@ -84,7 +84,7 @@ public function canDrive(Car $car)
}

/**
* @return array<class-string<Node>>
* @ifNextReturn array<class-string<Node>>
*/
public function getNodeTypes(): array
{
Expand Down Expand Up @@ -196,32 +196,32 @@ private function isInLoopWithoutContinueOrBreak(If_ $if): bool
/**
* @param Expr[] $conditions
* @param Stmt[] $afters
* @return Stmt[]
* @ifNextReturn Stmt[]
*/
private function processReplaceIfs(
If_ $if,
array $conditions,
Return_ $ifNextReturnClone,
Return_ $ifNextReturn,
array $afters,
?Stmt $nextStmt
): array {
$ifs = $this->invertedIfFactory->createFromConditions($if, $conditions, $ifNextReturnClone, $nextStmt);
$ifs = $this->invertedIfFactory->createFromConditions($if, $conditions, $ifNextReturn, $nextStmt);
$this->mirrorComments($ifs[0], $if);

$result = array_merge($ifs, $afters);
if ($if->stmts[0] instanceof Return_) {
return $result;
}

if (! $ifNextReturnClone->expr instanceof Expr) {
if (! $ifNextReturn->expr instanceof Expr) {
return $result;
}

if ($this->contextAnalyzer->isInLoop($if)) {
return $result;
}

return array_merge($result, [$ifNextReturnClone]);
return array_merge($result, [$ifNextReturn]);
}

private function shouldSkip(If_ $if, ?Stmt $nexStmt): bool
Expand All @@ -238,7 +238,7 @@ private function shouldSkip(If_ $if, ?Stmt $nexStmt): bool
return true;
}

// is simple return? skip it
// is simple ifNextReturn? skip it
$onlyStmt = $if->stmts[0];
if ($onlyStmt instanceof Return_ && $onlyStmt->expr instanceof Expr && $this->simpleScalarAnalyzer->isSimpleScalar(
$onlyStmt->expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ private function resolveYieldStaticArrayTypeByParameterPosition(array $yields, i
return $this->typeFactory->createMixedPassedOrUnionType($paramOnPositionTypes);
}

private function getTypeFromClassMethodYield(Array_ $classMethodYieldArrayNode): MixedType | ConstantArrayType
private function getTypeFromClassMethodYield(Array_ $classMethodYieldArray): MixedType | ConstantArrayType
{
$arrayType = $this->nodeTypeResolver->getType($classMethodYieldArrayNode);
$arrayType = $this->nodeTypeResolver->getType($classMethodYieldArray);

// impossible to resolve
if (! $arrayType instanceof ConstantArrayType) {
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpParser/Node/NodeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ protected function setUp(): void
* @param int[]|array<string, int> $inputArray
*/
#[DataProvider('provideDataForArray')]
public function testCreateArray(array $inputArray, Array_ $expectedArrayNode): void
public function testCreateArray(array $inputArray, Array_ $expectedArray): void
{
$arrayNode = $this->nodeFactory->createArray($inputArray);

$this->assertEquals($expectedArrayNode, $arrayNode);
$this->assertEquals($expectedArray, $arrayNode);
}

/**
Expand Down

0 comments on commit 7ebaf11

Please sign in to comment.