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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;

class KeepRightParentheses
{
public function run($port, $scheme)
{
$port = isset($port) ? $port : (($scheme == 'https') ? '443' : '80');
}
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;

class KeepRightParentheses
{
public function run($port, $scheme)
{
$port = $port ?? (($scheme == 'https') ? '443' : '80');
}
}

?>
4 changes: 2 additions & 2 deletions rules/Carbon/NodeFactory/CarbonCallFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function createFromDateTimeString(
$methodCall = $this->createModifyMethodCall(
$carbonCall,
new Int_((int) $match['count']),
(string) $match['unit'],
(string) $match['operator']
$match['unit'],
$match['operator']
);
if ($methodCall instanceof MethodCall) {
$carbonCall = $methodCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function resolveNewVariableName(string $typeShortName): string
lcfirst($typeShortName),
self::STARTS_WITH_ABBREVIATION_REGEX,
static function (array $matches): string {
$output = isset($matches[1]) ? strtolower($matches[1]) : '';
$output = isset($matches[1]) ? strtolower((string) $matches[1]) : '';
$output .= $matches[2] ?? '';

return $output . ($matches[3] ?? '');
Expand Down
36 changes: 36 additions & 0 deletions rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Ternary;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\Application\File;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -112,9 +114,43 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal
return null;
}

if ($ternary->else instanceof Ternary && $this->isTernaryParenthesized($this->file, $ternary->cond, $ternary)) {
$ternary->else->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}

return new Coalesce($ternary->if, $ternary->else);
}

private function isTernaryParenthesized(File $file, Expr $expr, Ternary $ternary): bool
{
$oldTokens = $file->getOldTokens();
$endTokenPost = $ternary->getEndTokenPos();

if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')') {
$startTokenPos = $ternary->else->getStartTokenPos();
$previousEndTokenPost = $expr->getEndTokenPos();

while ($startTokenPos > $previousEndTokenPost) {
--$startTokenPos;

if (! isset($oldTokens[$startTokenPos])) {
return false;
}

// handle space before open parentheses
if (trim((string) $oldTokens[$startTokenPos]) === '') {
continue;
}

return (string) $oldTokens[$startTokenPos] === '(';
}

return false;
}

return false;
}

private function isNullMatch(Expr $possibleNullExpr, Expr $firstNode, Expr $secondNode): bool
{
if (! $this->valueResolver->isNull($possibleNullExpr)) {
Expand Down