Skip to content
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
11 changes: 11 additions & 0 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ jobs:
coverage: none # disable xdebug, pcov
- run: composer install --no-progress
- run: composer rector
rector-ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
-
uses: shivammathur/setup-php@v1
with:
php-version: 7.3
coverage: none # disable xdebug, pcov
- run: composer install --no-progress
- run: composer rector-ci
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You remove the rector job above. It's duplicated now and only with --dry-run it actually fails the CI

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll handle it


docs:
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ public function __toString(): string
}

if ($this->field) {
if (is_array($this->field)) {
$contentItems['field'] = $this->printArrayItem($this->field, 'field');
} else {
$contentItems['field'] = $this->field;
}
$contentItems['field'] = is_array($this->field) ?
$this->printArrayItem($this->field, 'field') : $this->field;
}

if ($this->value !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function __toString(): string
$contentItems['fieldName'] = $this->fieldName;
}

if ($this->timeAware !== false) {
if ($this->timeAware) {
$contentItems['timeAware'] = sprintf('strict=%s', $this->timeAware ? 'true' : 'false');
}

if ($this->hardDelete !== false) {
if ($this->hardDelete) {
$contentItems['hardDelete'] = sprintf('strict=%s', $this->hardDelete ? 'true' : 'false');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ private function tryStartWithKey(string $name, bool $start, TokenIterator $local
return true;
}

if ($localTokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
if ($localTokenIterator->currentTokenValue() === $name) {
// consume "=" as well
$localTokenIterator->next();
$localTokenIterator->tryConsumeTokenType(Lexer::TOKEN_EQUAL);

return true;
}
if ($localTokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER) &&
$localTokenIterator->currentTokenValue() === $name
) {
// consume "=" as well
$localTokenIterator->next();
$localTokenIterator->tryConsumeTokenType(Lexer::TOKEN_EQUAL);
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ final class MultilineSpaceFormatPreserver
{
public function resolveCurrentPhpDocNodeText(AttributeAwareNodeInterface $attributeAwareNode): ?string
{
if ($attributeAwareNode instanceof PhpDocTagNode) {
if (property_exists($attributeAwareNode->value, 'description')) {
return $attributeAwareNode->value->description;
}
if ($attributeAwareNode instanceof PhpDocTagNode &&
property_exists($attributeAwareNode->value, 'description')
) {
return $attributeAwareNode->value->description;
}

if ($attributeAwareNode instanceof PhpDocTextNode) {
Expand All @@ -45,20 +45,19 @@ public function setNewTextToPhpDocNode(
AttributeAwareNodeInterface $attributeAwareNode,
string $newText
): AttributeAwareNodeInterface {
if ($attributeAwareNode instanceof PhpDocTagNode) {
if (property_exists($attributeAwareNode->value, 'description')) {
$attributeAwareNode->value->description = $newText;
}
if ($attributeAwareNode instanceof PhpDocTagNode && property_exists(
$attributeAwareNode->value,
'description'
)) {
$attributeAwareNode->value->description = $newText;
}

if ($attributeAwareNode instanceof PhpDocTextNode) {
$attributeAwareNode->text = $newText;
}

if ($attributeAwareNode instanceof AttributeAwarePhpDocTagNode) {
if ($attributeAwareNode->value instanceof AttributeAwareGenericTagValueNode) {
$attributeAwareNode->value->value = $newText;
}
if ($attributeAwareNode instanceof AttributeAwarePhpDocTagNode && $attributeAwareNode->value instanceof AttributeAwareGenericTagValueNode) {
$attributeAwareNode->value->value = $newText;
}

return $attributeAwareNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ private function detectOldWhitespaces(Node $node, array $tokens, StartEndValueOb
if ($tokens[$i][1] === Lexer::TOKEN_HORIZONTAL_WS) {
$value = $tokens[$i][0];

if ($node instanceof DoctrineTagNodeInterface) {
// give back "\s+\*" as well
if ($i - 1 > $start) { // do not overlap to previous node
if (isset($tokens[$i - 1]) && $tokens[$i - 1][1] === Lexer::TOKEN_PHPDOC_EOL) {
$previousTokenValue = $tokens[$i - 1][0];
if (Strings::match($previousTokenValue, '#\s+\*#m')) {
$value = $previousTokenValue . $value;
}
}
// give back "\s+\*" as well
// do not overlap to previous node
if ($node instanceof DoctrineTagNodeInterface &&
$i - 1 > $start &&
isset($tokens[$i - 1]) &&
$tokens[$i - 1][1] === Lexer::TOKEN_PHPDOC_EOL
) {
$previousTokenValue = $tokens[$i - 1][0];
if (Strings::match($previousTokenValue, '#\s+\*#m')) {
$value = $previousTokenValue . $value;
}
}

Expand Down
38 changes: 15 additions & 23 deletions packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,12 @@ private function addTokensFromTo(
--$from;
}

if ($shouldSkipEmptyLinesAbove) {
// skip extra empty lines above if this is the last one
if (Strings::contains($this->tokens[$from][0], PHP_EOL) && Strings::contains(
$this->tokens[$from + 1][0],
PHP_EOL
)) {
++$from;
}
// 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)
) {
++$from;
}

return $this->appendToOutput($output, $from, $to, $positionJumpSet);
Expand All @@ -234,21 +232,15 @@ private function printPhpDocTagNode(
$output .= ' ';
}

if ($phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES)) {
if (property_exists($phpDocTagNode->value, 'description') && $phpDocTagNode->value->description) {
$quotedDescription = preg_quote($phpDocTagNode->value->description, '#');

$pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+');

$nodeOutput = Strings::replace(
$nodeOutput,
'#' . $pattern . '#',
$phpDocTagNode->value->description
);

if (substr_count($nodeOutput, "\n") !== 0) {
$nodeOutput = Strings::replace($nodeOutput, "#\n#", PHP_EOL . ' * ');
}
if ($phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES) && (property_exists(
$phpDocTagNode->value,
'description'
) && $phpDocTagNode->value->description)) {
$quotedDescription = preg_quote($phpDocTagNode->value->description, '#');
$pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+');
$nodeOutput = Strings::replace($nodeOutput, '#' . $pattern . '#', $phpDocTagNode->value->description);
if (substr_count($nodeOutput, "\n") !== 0) {
$nodeOutput = Strings::replace($nodeOutput, "#\n#", PHP_EOL . ' * ');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ protected function isInCakePHPController(Node $node): bool
}

$class = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($class !== null) {
return true;
}

return false;
return $class !== null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function refactor(Node $node): ?Node
*/
private function getComponentClasses(): array
{
if ($this->componentsClasses) {
if ($this->componentsClasses !== []) {
return $this->componentsClasses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ private function isThisFlashMethodCall(Node $node): bool
if (! $this->isName($node->var->var, 'this')) {
return false;
}

if (! $this->isName($node->var->name, 'Flash')) {
return false;
}

return true;
return $this->isName($node->var->name, 'Flash');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ public function refactor(Node $node): ?Node

# e.g. |trans https://symfony.com/doc/current/translation/templates.html#using-twig-filters
$labelFilters = [];
if ($label instanceof FuncCall) {
if ($this->isName($label, '__')) {
$labelFilters[] = 'trans';

$label = $label->args[0]->value;
}
if ($label instanceof FuncCall && $this->isName($label, '__')) {
$labelFilters[] = 'trans';
$label = $label->args[0]->value;
}

$parametersValue = $this->getValue($parameters);
Expand All @@ -112,23 +109,18 @@ private function isThisHtmlPropertyFetch(Expr $expr): bool
if (! $this->isName($expr->var, 'this')) {
return false;
}

if (! $this->isName($expr->name, 'Html')) {
return false;
}

return true;
return $this->isName($expr->name, 'Html');
}

private function createAHtml(string $routeName, array $labelFilters, string $labelValue): string
{
$aHtml = Html::el('a');
$aHtml->href = sprintf('{{ path(\'%s\') }}', $routeName);
$aHtml->href = sprintf("{{ path('%s') }}", $routeName);

if ($labelFilters !== []) {
$labelFilterAsString = implode('|', $labelFilters);

$labelValue = sprintf('{{ \'%s\'|%s }}', $labelValue, $labelFilterAsString);
$labelValue = sprintf("{{ '%s'|%s }}", $labelValue, $labelFilterAsString);
}

$aHtml->setText($labelValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CakePHPToSymfony\Rector\Echo_;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\InlineHTML;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$expr = $node->exprs[0];
if (! $expr instanceof Node\Expr\FuncCall) {
if (! $expr instanceof FuncCall) {
return null;
}

Expand All @@ -48,7 +49,7 @@ public function refactor(Node $node): ?Node
$translatedValue = $expr->args[0]->value;
$translatedValue = $this->getValue($translatedValue);

$html = sprintf('{{ \'%s\'|trans }}', $translatedValue);
$html = sprintf("{{ '%s'|trans }}", $translatedValue);

return new InlineHTML($html);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ private function createAnonymousFunction(ClassMethod $classMethod, Node $node):
$anonymousFunction->stmts[] = new Expression($innerMethodCall);
}

if ($node instanceof Variable) {
if (! $this->isName($node, 'this')) {
$anonymousFunction->uses[] = new ClosureUse($node);
}
if ($node instanceof Variable && ! $this->isName($node, 'this')) {
$anonymousFunction->uses[] = new ClosureUse($node);
}

return $anonymousFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ public function refactor(Node $node): ?Node

protected function createCountedValueName(?string $valueName, ?Scope $scope): string
{
if ($valueName === null) {
$countedValueName = self::DEFAULT_VARIABLE_COUNT_NAME;
} else {
$countedValueName = $valueName . 'Count';
}
$countedValueName = $valueName === null ? self::DEFAULT_VARIABLE_COUNT_NAME : $valueName . 'Count';

return parent::createCountedValueName($countedValueName, $scope);
}
Expand Down
19 changes: 7 additions & 12 deletions packages/CodeQuality/src/Rector/For_/ForToForeachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,11 @@ private function isConditionMatch(array $condExprs): bool
}

// count($values)
if ($condExprs[0]->right instanceof FuncCall) {
if ($this->isName($condExprs[0]->right, 'count')) {
/** @var FuncCall $countFuncCall */
$countFuncCall = $condExprs[0]->right;
$this->iteratedExpr = $countFuncCall->args[0]->value;

return true;
}
if ($condExprs[0]->right instanceof FuncCall && $this->isName($condExprs[0]->right, 'count')) {
/** @var FuncCall $countFuncCall */
$countFuncCall = $condExprs[0]->right;
$this->iteratedExpr = $countFuncCall->args[0]->value;
return true;
}

return false;
Expand Down Expand Up @@ -288,10 +285,8 @@ private function isPartOfAssign(?Node $node): bool
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

while ($parentNode !== null && ! $parentNode instanceof Expression) {
if ($parentNode instanceof Assign) {
if ($this->areNodesEqual($parentNode->var, $previousNode)) {
return true;
}
if ($parentNode instanceof Assign && $this->areNodesEqual($parentNode->var, $previousNode)) {
return true;
}

$previousNode = $parentNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private function shouldSkip(Foreach_ $foreachNode): bool
return true;
}

if ($ifNode->elseifs) {
if ($ifNode->elseifs !== []) {
return true;
}

Expand Down
11 changes: 5 additions & 6 deletions packages/CodeQuality/src/Rector/If_/CombineIfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function refactor(Node $node): ?Node
$subIf = $node->stmts[0];
$node->cond = new BooleanAnd($node->cond, $subIf->cond);
$node->stmts = $subIf->stmts;

$node->setAttribute('comments', array_merge($node->getComments(), $subIf->getComments()));

return $node;
}

Expand All @@ -82,7 +85,7 @@ private function shouldSkip(If_ $node): bool
return true;
}

if ($node->elseifs) {
if ($node->elseifs !== []) {
return true;
}

Expand All @@ -93,10 +96,6 @@ private function shouldSkip(If_ $node): bool
if ($node->stmts[0]->else !== null) {
return true;
}

if ($node->stmts[0]->elseifs) {
return true;
}
return false;
return (bool) $node->stmts[0]->elseifs;
}
}
Loading