From 98c55b0f88ad16af1755037f4c6ae113040837c2 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 5 Aug 2018 23:03:04 +0900 Subject: [PATCH] Migrator only replaced "PHPExcel" --- src/PhpSpreadsheet/Helper/Migrator.php | 45 +++++++++--- .../Helper/MigratorTest.php | 68 +++++++++++++++++++ 2 files changed, 104 insertions(+), 9 deletions(-) diff --git a/src/PhpSpreadsheet/Helper/Migrator.php b/src/PhpSpreadsheet/Helper/Migrator.php index 4eb60633..26d5fcea 100644 --- a/src/PhpSpreadsheet/Helper/Migrator.php +++ b/src/PhpSpreadsheet/Helper/Migrator.php @@ -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; + } } diff --git a/tests/PhpSpreadsheetTests/Helper/MigratorTest.php b/tests/PhpSpreadsheetTests/Helper/MigratorTest.php index 3c9202c5..a64cda80 100644 --- a/tests/PhpSpreadsheetTests/Helper/MigratorTest.php +++ b/tests/PhpSpreadsheetTests/Helper/MigratorTest.php @@ -17,4 +17,72 @@ class MigratorTest extends TestCase } } } + + public function testReplace() + { + $input = <<<'STRING' +do(); + $fooobjPHPExcel->do(); + $objPHPExcel->do(); + $this->objPHPExcel->do(); + $this->PHPExcel->do(); + + return \PHPExcel_Cell::stringFromColumnIndex(9); + } +} +STRING; + + $expected = <<<'STRING' +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)); + } }