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

[Php83] Handle dynamic host and port values on CombineHostPortLdapUriRector #5401

Merged
merged 4 commits into from
Dec 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\CombineHostPortLdapUriRector\Fixture;

class DynamicValue
{
public function run($host, $port)
{
$ldapconn = ldap_connect($host, $port);
}
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\CombineHostPortLdapUriRector\Fixture;

class DynamicValue
{
public function run($host, $port)
{
$ldapconn = ldap_connect("{$host}:{$port}");
}
}

?>
22 changes: 16 additions & 6 deletions rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Rector\Core\NodeAnalyzer\ExprAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand All @@ -20,6 +23,11 @@
*/
final class CombineHostPortLdapUriRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -69,14 +77,16 @@ public function refactor(Node $node): ?Node

if ($firstArg instanceof String_ && $secondArg instanceof LNumber) {
$args[0]->value = new String_($firstArg->value . ':' . $secondArg->value);

unset($args[1]);
$node->args = $args;

return $node;
} elseif ($this->exprAnalyzer->isDynamicExpr($firstArg) && $this->exprAnalyzer->isDynamicExpr($secondArg)) {
$args[0]->value = new Encapsed([$firstArg, new EncapsedStringPart(':'), $secondArg]);
} else {
return null;
}

return null;
unset($args[1]);
$node->args = $args;

return $node;
}

public function provideMinPhpVersion(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer;

use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
Expand Down Expand Up @@ -70,10 +71,6 @@ private function isHardCodedExpression(Expr $expr): bool
}

// Negative numbers are wrapped in UnaryMinus, so check expression inside it
if (($expr instanceof UnaryMinus || $expr instanceof Expr\UnaryPlus) && $expr->expr instanceof Scalar) {
return true;
}

return false;
return ($expr instanceof UnaryMinus || $expr instanceof UnaryPlus) && $expr->expr instanceof Scalar;
}
}