rector: Method `sprintf()` is not replaced by `\Safe\sprintf()`

Bug Report

Subject Details
Rector version v0.9.21 with PHP 8.0.1
Installed as prefixed Rector

Method sprintf() is not replaced by \Safe\sprintf(), a testcase already exists especially for this method ๐Ÿค”

Minimal PHP Code Causing Issue

this is my file:

<?php

declare(strict_types=1);

namespace App\Exception;

use App\Domain\Identifier\UserId;

/**
 * @author Oskar Stark <oskarstark@googlemail.com>
 */
final class UserNotFoundException extends NotFoundException
{
    public static function withUserId(UserId $userId): self
    {
        return new self(sprintf(
            'Cannot find User with UserId: %s',
            $userId->toString()
        ));
    }

    public static function withEmail(string $email): self
    {
        return new self(sprintf(
            'Cannot find User with email: %s',
            $email
        ));
    }
}

This is my config:

<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    // get parameters
    $parameters = $containerConfigurator->parameters();

    $parameters->set(Option::PATHS, [
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ]);

    $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80);

    $parameters->set(Option::AUTO_IMPORT_NAMES, true);
    $parameters->set(Option::IMPORT_SHORT_CLASSES, false);

    $parameters->set(Option::ENABLE_CACHE, false);

    $parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, getcwd() . '/phpstan-default.neon');

    // Define what rule sets will be applied
    $parameters->set(Option::SETS, [
        SetList::DEAD_DOC_BLOCK,
        SetList::PHP_80,
        SetList::PHPUNIT_91,
        SetList::SYMFONY_50_TYPES,
        SetList::SYMFONY_52,
    ]);

    // get services (needed for register a single rule)
    $services = $containerConfigurator->services();

    // register single rules
    /**
     * @see https://github.com/rectorphp/rector/blob/master/docs/rector_rules_overview.md#genericsphpstormmethodannotationrector
     */
    $services->set(GenericsPHPStormMethodAnnotationRector::class);

    /**
     * @see https://github.com/thecodingmachine/safe/blob/master/rector-migrate-0.7.php
     */
    $services->set(RenameFunctionRector::class)
        ->call('configure', [[
            RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
                'sprintf' => 'Safe\sprintf',
            ],
        ]]);
};

Expected Behaviour

diff --git a/src/Exception/UserNotFoundException.php b/src/Exception/UserNotFoundException.php
index 354c948..4393177 100644
--- a/src/Exception/UserNotFoundException.php
+++ b/src/Exception/UserNotFoundException.php
@@ -13,7 +13,7 @@ final class UserNotFoundException extends NotFoundException
 {
     public static function withUserId(UserId $userId): self
     {
-        return new self(sprintf(
+        return new self(\Safe\sprintf(
             'Cannot find User with UserId: %s',
             $userId->toString()
         ));
@@ -21,7 +21,7 @@ final class UserNotFoundException extends NotFoundException

     public static function withEmail(string $email): self
     {
-        return new self(sprintf(
+        return new self(\Safe\sprintf(
             'Cannot find User with email: %s',
             $email
         ));

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

It seems already fixed in dev-master https://getrector.org/demo/13114bca-1a55-4fc9-82a9-51eadaab3cbd

Closing.

It seems already included in latest release, please try latest version.

When can we expect a release please? Thanks! ๐Ÿ™

DI tried to add a demo, but something looks wrong:

Seems like mail() is called on the background because of series of fatal error. The provided code canโ€™t work, as the parent class is missing.

Could you narrow it to 1 PHP line?