Skip to content

Commit

Permalink
[Php83] Add CombineHostPortLdapUriRector (#5397)
Browse files Browse the repository at this point in the history
* [Php83] Add CombineHostPortLdapUriRector

* register

* skip different func call

* initial support

* fixture
  • Loading branch information
samsonasik authored Dec 27, 2023
1 parent ad09d9c commit 109d734
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 1 deletion.
7 changes: 6 additions & 1 deletion config/set/php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
use Rector\Config\RectorConfig;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([AddOverrideAttributeToOverriddenMethodsRector::class, AddTypeToConstRector::class]);
$rectorConfig->rules([
AddOverrideAttributeToOverriddenMethodsRector::class,
AddTypeToConstRector::class,
CombineHostPortLdapUriRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class CombineHostPortLdapUriRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class Fixture
{
public function run()
{
$ldapconn = ldap_connect('ldap://ldap.example.com', 389);
}
}

?>
-----
<?php

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

class Fixture
{
public function run()
{
$ldapconn = ldap_connect('ldap://ldap.example.com:389');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class SkipDifferentFuncCall
{
public function run($a, $b)
{
return str_contains($a, $b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

class SkipNo2ndArg
{
public function run()
{
$ldapuri = "ldap://ldap.example.com:389";
$ldapconn = ldap_connect($ldapuri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(CombineHostPortLdapUriRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_83);
};
86 changes: 86 additions & 0 deletions rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Rector\Php83\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.ldap
* @see \Rector\Tests\Php83\Rector\FuncCall\CombineHostPortLdapUriRector\CombineHostPortLdapUriRectorTest
*/
final class CombineHostPortLdapUriRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Combine separated host and port on ldap_connect() args',
[
new CodeSample(
<<<'CODE_SAMPLE'
ldap_connect('ldap://ldap.example.com', 389);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
ldap_connect('ldap://ldap.example.com:389');
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'ldap_connect')) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
if (count($args) !== 2) {
return null;
}

$firstArg = $args[0]->value;
$secondArg = $args[1]->value;

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

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

return $node;
}

return null;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_HOST_PORT_SEPARATE_ARGS;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,10 @@ final class PhpVersionFeature
* @var int
*/
public const TYPED_CLASS_CONSTANTS = PhpVersion::PHP_83;

/**
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.ldap
* @var int
*/
public const DEPRECATE_HOST_PORT_SEPARATE_ARGS = PhpVersion::PHP_83;
}

0 comments on commit 109d734

Please sign in to comment.