Migrator only replaced "PHPExcel"
This commit is contained in:
parent
46eabbad60
commit
98c55b0f88
|
@ -4,6 +4,22 @@ namespace PhpOffice\PhpSpreadsheet\Helper;
|
|||
|
||||
class Migrator
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $from;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $to;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->from = array_keys($this->getMapping());
|
||||
$this->to = array_values($this->getMapping());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ordered mapping from old PHPExcel class names to new PhpSpreadsheet one.
|
||||
*
|
||||
|
@ -258,9 +274,6 @@ class Migrator
|
|||
'/*.phtml',
|
||||
];
|
||||
|
||||
$from = array_keys($this->getMapping());
|
||||
$to = array_values($this->getMapping());
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
foreach (glob($path . $pattern) as $file) {
|
||||
if (strpos($path, '/vendor/') !== false) {
|
||||
|
@ -269,12 +282,7 @@ class Migrator
|
|||
continue;
|
||||
}
|
||||
$original = file_get_contents($file);
|
||||
$converted = str_replace($from, $to, $original);
|
||||
|
||||
// The string "PHPExcel" gets special treatment because of how common it might be.
|
||||
// This regex requires a word boundary around the string, and it can't be
|
||||
// preceded by $ or -> (goal is to filter out cases where a variable is named $PHPExcel or similar)
|
||||
$converted = preg_replace('/(?<!\$|->)\bPHPExcel\b/', \PhpOffice\PhpSpreadsheet\Spreadsheet::class, $original);
|
||||
$converted = $this->replace($original);
|
||||
|
||||
if ($original !== $converted) {
|
||||
echo $file . " converted\n";
|
||||
|
@ -303,4 +311,23 @@ class Migrator
|
|||
$this->recursiveReplace($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate the given code from PHPExcel to PhpSpreadsheet.
|
||||
*
|
||||
* @param string $original
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function replace($original)
|
||||
{
|
||||
$converted = str_replace($this->from, $this->to, $original);
|
||||
|
||||
// The string "PHPExcel" gets special treatment because of how common it might be.
|
||||
// This regex requires a word boundary around the string, and it can't be
|
||||
// preceded by $ or -> (goal is to filter out cases where a variable is named $PHPExcel or similar)
|
||||
$converted = preg_replace('~(?<!\$|->)(\b|\\\\)PHPExcel\b~', '\\' . \PhpOffice\PhpSpreadsheet\Spreadsheet::class, $converted);
|
||||
|
||||
return $converted;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,4 +17,72 @@ class MigratorTest extends TestCase
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testReplace()
|
||||
{
|
||||
$input = <<<'STRING'
|
||||
<?php
|
||||
|
||||
namespace Foo;
|
||||
|
||||
use PHPExcel;
|
||||
use PHPExcel_Worksheet;
|
||||
|
||||
class Bar
|
||||
{
|
||||
/**
|
||||
* @param PHPExcel $workbook
|
||||
* @param PHPExcel_Worksheet $sheet
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function baz(PHPExcel $workbook, PHPExcel_Worksheet $sheet)
|
||||
{
|
||||
PHPExcel::class;
|
||||
\PHPExcel::class;
|
||||
$PHPExcel->do();
|
||||
$fooobjPHPExcel->do();
|
||||
$objPHPExcel->do();
|
||||
$this->objPHPExcel->do();
|
||||
$this->PHPExcel->do();
|
||||
|
||||
return \PHPExcel_Cell::stringFromColumnIndex(9);
|
||||
}
|
||||
}
|
||||
STRING;
|
||||
|
||||
$expected = <<<'STRING'
|
||||
<?php
|
||||
|
||||
namespace Foo;
|
||||
|
||||
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class Bar
|
||||
{
|
||||
/**
|
||||
* @param \PhpOffice\PhpSpreadsheet\Spreadsheet $workbook
|
||||
* @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function baz(\PhpOffice\PhpSpreadsheet\Spreadsheet $workbook, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet)
|
||||
{
|
||||
\PhpOffice\PhpSpreadsheet\Spreadsheet::class;
|
||||
\PhpOffice\PhpSpreadsheet\Spreadsheet::class;
|
||||
$PHPExcel->do();
|
||||
$fooobjPHPExcel->do();
|
||||
$objPHPExcel->do();
|
||||
$this->objPHPExcel->do();
|
||||
$this->PHPExcel->do();
|
||||
|
||||
return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(9);
|
||||
}
|
||||
}
|
||||
STRING;
|
||||
|
||||
$migrator = new Migrator();
|
||||
self::assertSame($expected, $migrator->replace($input));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue