[BUGFIX] Check if argument is string (#146)

Resolves: https://github.com/rectorphp/rector/issues/6482
This commit is contained in:
Sebastian Schreiber 2021-06-04 00:43:15 +02:00 committed by GitHub
parent 1913747f01
commit df07500a76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 2 deletions

View File

@ -4,7 +4,7 @@ namespace Rector\Tests\PHPOffice\Rector\StaticCall\ChangePdfWriterRector\Fixture
final class Fixture
{
public function run(): void
public function run(string $format): void
{
\PHPExcel_Settings::setPdfRendererName(PHPExcel_Settings::PDF_RENDERER_MPDF);
\PHPExcel_Settings::setPdfRenderer($somePath);
@ -20,7 +20,7 @@ namespace Rector\Tests\PHPOffice\Rector\StaticCall\ChangePdfWriterRector\Fixture
final class Fixture
{
public function run(): void
public function run(string $format): void
{
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Tests\PHPOffice\Rector\StaticCall\ChangePdfWriterRector\Fixture;
final class VariableFormatFixture
{
public function run(string $format): void
{
$writer = \PHPExcel_IOFactory::createWriter($spreadsheet, $format);
}
}
?>
-----
<?php
namespace Rector\Tests\PHPOffice\Rector\StaticCall\ChangePdfWriterRector\Fixture;
final class VariableFormatFixture
{
public function run(string $format): void
{
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, $format);
}
}
?>

View File

@ -3,9 +3,18 @@
declare(strict_types=1);
use Rector\PHPOffice\Rector\StaticCall\ChangePdfWriterRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ChangePdfWriterRector::class);
$services->set(RenameClassRector::class)
->call('configure', [[
RenameClassRector::OLD_TO_NEW_CLASSES => [
'PHPExcel_IOFactory' => 'PhpOffice\PhpSpreadsheet\IOFactory',
]
]
]);
};

View File

@ -80,6 +80,11 @@ CODE_SAMPLE
}
$secondArgValue = $this->valueResolver->getValue($node->args[1]->value);
if(! is_string($secondArgValue)) {
return null;
}
if (Strings::match($secondArgValue, '#pdf#i')) {
return new New_(new FullyQualified('PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf'), [$node->args[0]]);
}