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
3 changes: 2 additions & 1 deletion config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Removing\ValueObject\RemoveFuncCallArg;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([ArrayFirstLastRector::class]);
$rectorConfig->rules([ArrayFirstLastRector::class, RemoveFinfoBufferContextArgRector::class]);

$rectorConfig->ruleWithConfiguration(
RemoveFuncCallArgRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector\Fixture;

finfo_buffer($finfo, $fileContents);
finfo_buffer($finfo, $fileContents, FILEINFO_NONE);
finfo_buffer($finfo, $fileContents, FILEINFO_NONE, $context);
finfo_buffer($finfo, $fileContents, FILEINFO_NONE, context: $context);
finfo_buffer($finfo, $fileContents, flags: FILEINFO_NONE, context: $context);
finfo_buffer($finfo, $fileContents, context: $context);
finfo_buffer($finfo, $fileContents, context: $context, flags: FILEINFO_NONE);
finfo_buffer(...[$finfo, $fileContents]);
finfo_buffer(...[$finfo, $fileContents, $context]);
finfo_buffer(...[$finfo, $fileContents, FILEINFO_NONE, $context]);

function foo(\finfo $finfo): void
{
$finfo->buffer($fileContents, FILEINFO_NONE, $context);
$finfo->buffer($fileContents, FILEINFO_NONE, context: $context);
$finfo->buffer($fileContents, flags: FILEINFO_NONE, context: $context);
$finfo->buffer($fileContents, context: $context);
$finfo->buffer($fileContents, context: $context, flags: FILEINFO_NONE);
$finfo->buffer(...[$fileContents]);
$finfo->buffer(...[$fileContents, $context]);
$finfo->buffer(...[$fileContents, FILEINFO_NONE, $context]);
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector\Fixture;

finfo_buffer($finfo, $fileContents);
finfo_buffer($finfo, $fileContents, FILEINFO_NONE);
finfo_buffer($finfo, $fileContents, FILEINFO_NONE);
finfo_buffer($finfo, $fileContents, FILEINFO_NONE);
finfo_buffer($finfo, $fileContents, flags: FILEINFO_NONE);
finfo_buffer($finfo, $fileContents);
finfo_buffer($finfo, $fileContents, flags: FILEINFO_NONE);
finfo_buffer(...[$finfo, $fileContents]);
finfo_buffer(...[$finfo, $fileContents, $context]);
finfo_buffer(...[$finfo, $fileContents, FILEINFO_NONE, $context]);

function foo(\finfo $finfo): void
{
$finfo->buffer($fileContents, FILEINFO_NONE);
$finfo->buffer($fileContents, FILEINFO_NONE);
$finfo->buffer($fileContents, flags: FILEINFO_NONE);
$finfo->buffer($fileContents);
$finfo->buffer($fileContents, flags: FILEINFO_NONE);
$finfo->buffer(...[$fileContents]);
$finfo->buffer(...[$fileContents, $context]);
$finfo->buffer(...[$fileContents, FILEINFO_NONE, $context]);
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector\Fixture;

finfo_file($finfo, $fileContents, FILEINFO_NONE, $context);
finfo_file($finfo, $fileContents, FILEINFO_NONE, context: $context);
finfo_file($finfo, $fileContents, flags: FILEINFO_NONE, context: $context);

function foo(\finfo $finfo): void
{
$finfo->file($fileContents, FILEINFO_NONE, $context);
$finfo->file($fileContents, FILEINFO_NONE, context: $context);
$finfo->file($fileContents, flags: FILEINFO_NONE, context: $context);
}

function bar($otherObject): void
{
$finfo->buffer($fileContents, FILEINFO_NONE, $context);
$finfo->buffer($fileContents, FILEINFO_NONE, context: $context);
$finfo->buffer($fileContents, flags: FILEINFO_NONE, context: $context);
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector\Fixture;

finfo_file($finfo, $fileContents, FILEINFO_NONE, $context);
finfo_file($finfo, $fileContents, FILEINFO_NONE, context: $context);
finfo_file($finfo, $fileContents, flags: FILEINFO_NONE, context: $context);

function foo(\finfo $finfo): void
{
$finfo->file($fileContents, FILEINFO_NONE, $context);
$finfo->file($fileContents, FILEINFO_NONE, context: $context);
$finfo->file($fileContents, flags: FILEINFO_NONE, context: $context);
}

function bar($otherObject): void
{
$finfo->buffer($fileContents, FILEINFO_NONE, $context);
$finfo->buffer($fileContents, FILEINFO_NONE, context: $context);
$finfo->buffer($fileContents, flags: FILEINFO_NONE, context: $context);
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;

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

final class RemoveFinfoBufferContextArgRectorTest 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,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\ValueObject\PhpVersion;

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

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

declare(strict_types=1);

namespace Rector\Php85\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_context_parameter_for_finfo_buffer
* @see \Rector\Tests\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector\RemoveFinfoBufferContextArgRectorTest
*/
final class RemoveFinfoBufferContextArgRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove argument by position by function name', [
new CodeSample(
<<<'CODE_SAMPLE'
finfo_buffer($finfo, $fileContents, FILEINFO_NONE, []);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
finfo_buffer($finfo, $fileContents, FILEINFO_NONE);
CODE_SAMPLE
,
),
]);
}

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

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->name instanceof Expr) {
return null;
}

if ($node instanceof FuncCall && ! $this->isName($node->name, 'finfo_buffer')) {
return null;
}

$objectType = new ObjectType('finfo');
if ($node instanceof MethodCall && (! $this->nodeTypeResolver->isObjectType(
$node->var,
$objectType
) || ! $this->isName($node->name, 'buffer'))) {
return null;
}

if ($this->removeContextArg($node)) {
return $node;
}

return null;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_FINFO_BUFFER_CONTEXT;
}

/**
* @param FuncCall|MethodCall $funcCall
*/
private function removeContextArg(CallLike $funcCall): bool
{
// In `finfo::buffer` method calls, the first parameter, compared to `finfo_buffer`, does not exist.
$methodArgCorrection = 0;
if ($funcCall instanceof MethodCall) {
$methodArgCorrection = -1;
}

if (count($funcCall->args) <= 2 + $methodArgCorrection) {
return false;
}

// Cannot handle variadic args
foreach ($funcCall->args as $position => $arg) {
if (! $arg instanceof Arg) {
return false;
}
}

/** @var array<Arg> $args */
$args = $funcCall->args;

// Argument 3 ($flags) and argument 4 ($context) are optional, thus named parameters must be considered
if (! $this->argsAnalyzer->hasNamedArg($args)) {
if (count($args) < 4 + $methodArgCorrection) {
return false;
}

unset($funcCall->args[3 + $methodArgCorrection]);

return true;
}

foreach ($args as $position => $arg) {
if ($arg->name instanceof Identifier && $arg->name->name === 'context') {
unset($funcCall->args[$position]);

return true;
}
}

return false;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,10 @@ final class PhpVersionFeature
* @var int
*/
public const ARRAY_ANY = PhpVersion::PHP_84;

/**
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_context_parameter_for_finfo_buffer
* @var int
*/
public const DEPRECATE_FINFO_BUFFER_CONTEXT = PhpVersion::PHP_85;
}
Loading