Skip to content

Commit 98c55b0

Browse files
committed
Migrator only replaced "PHPExcel"
1 parent 46eabba commit 98c55b0

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

src/PhpSpreadsheet/Helper/Migrator.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44

55
class Migrator
66
{
7+
/**
8+
* @var string[]
9+
*/
10+
private $from;
11+
12+
/**
13+
* @var string[]
14+
*/
15+
private $to;
16+
17+
public function __construct()
18+
{
19+
$this->from = array_keys($this->getMapping());
20+
$this->to = array_values($this->getMapping());
21+
}
22+
723
/**
824
* Return the ordered mapping from old PHPExcel class names to new PhpSpreadsheet one.
925
*
@@ -258,9 +274,6 @@ private function recursiveReplace($path)
258274
'/*.phtml',
259275
];
260276

261-
$from = array_keys($this->getMapping());
262-
$to = array_values($this->getMapping());
263-
264277
foreach ($patterns as $pattern) {
265278
foreach (glob($path . $pattern) as $file) {
266279
if (strpos($path, '/vendor/') !== false) {
@@ -269,12 +282,7 @@ private function recursiveReplace($path)
269282
continue;
270283
}
271284
$original = file_get_contents($file);
272-
$converted = str_replace($from, $to, $original);
273-
274-
// The string "PHPExcel" gets special treatment because of how common it might be.
275-
// This regex requires a word boundary around the string, and it can't be
276-
// preceded by $ or -> (goal is to filter out cases where a variable is named $PHPExcel or similar)
277-
$converted = preg_replace('/(?<!\$|->)\bPHPExcel\b/', \PhpOffice\PhpSpreadsheet\Spreadsheet::class, $original);
285+
$converted = $this->replace($original);
278286

279287
if ($original !== $converted) {
280288
echo $file . " converted\n";
@@ -303,4 +311,23 @@ public function migrate()
303311
$this->recursiveReplace($path);
304312
}
305313
}
314+
315+
/**
316+
* Migrate the given code from PHPExcel to PhpSpreadsheet.
317+
*
318+
* @param string $original
319+
*
320+
* @return string
321+
*/
322+
public function replace($original)
323+
{
324+
$converted = str_replace($this->from, $this->to, $original);
325+
326+
// The string "PHPExcel" gets special treatment because of how common it might be.
327+
// This regex requires a word boundary around the string, and it can't be
328+
// preceded by $ or -> (goal is to filter out cases where a variable is named $PHPExcel or similar)
329+
$converted = preg_replace('~(?<!\$|->)(\b|\\\\)PHPExcel\b~', '\\' . \PhpOffice\PhpSpreadsheet\Spreadsheet::class, $converted);
330+
331+
return $converted;
332+
}
306333
}

tests/PhpSpreadsheetTests/Helper/MigratorTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,72 @@ public function testMappingOnlyContainExistingClasses()
1717
}
1818
}
1919
}
20+
21+
public function testReplace()
22+
{
23+
$input = <<<'STRING'
24+
<?php
25+
26+
namespace Foo;
27+
28+
use PHPExcel;
29+
use PHPExcel_Worksheet;
30+
31+
class Bar
32+
{
33+
/**
34+
* @param PHPExcel $workbook
35+
* @param PHPExcel_Worksheet $sheet
36+
*
37+
* @return string
38+
*/
39+
public function baz(PHPExcel $workbook, PHPExcel_Worksheet $sheet)
40+
{
41+
PHPExcel::class;
42+
\PHPExcel::class;
43+
$PHPExcel->do();
44+
$fooobjPHPExcel->do();
45+
$objPHPExcel->do();
46+
$this->objPHPExcel->do();
47+
$this->PHPExcel->do();
48+
49+
return \PHPExcel_Cell::stringFromColumnIndex(9);
50+
}
51+
}
52+
STRING;
53+
54+
$expected = <<<'STRING'
55+
<?php
56+
57+
namespace Foo;
58+
59+
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
60+
use \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
61+
62+
class Bar
63+
{
64+
/**
65+
* @param \PhpOffice\PhpSpreadsheet\Spreadsheet $workbook
66+
* @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet
67+
*
68+
* @return string
69+
*/
70+
public function baz(\PhpOffice\PhpSpreadsheet\Spreadsheet $workbook, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet)
71+
{
72+
\PhpOffice\PhpSpreadsheet\Spreadsheet::class;
73+
\PhpOffice\PhpSpreadsheet\Spreadsheet::class;
74+
$PHPExcel->do();
75+
$fooobjPHPExcel->do();
76+
$objPHPExcel->do();
77+
$this->objPHPExcel->do();
78+
$this->PHPExcel->do();
79+
80+
return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(9);
81+
}
82+
}
83+
STRING;
84+
85+
$migrator = new Migrator();
86+
self::assertSame($expected, $migrator->replace($input));
87+
}
2088
}

0 commit comments

Comments
 (0)