Skip to content

Commit

Permalink
[Php83] Handle dynamic host and port values on CombineHostPortLdapUri…
Browse files Browse the repository at this point in the history
…Rector (#5401)

* [Php83] Handle dynamic host and port values on CombineHostPortLdapUriRector

* [Php83] Handle dynamic host and port values on CombineHostPortLdapUriRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 31, 2023
1 parent e044d50 commit fff370b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
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;
}
}

0 comments on commit fff370b

Please sign in to comment.