Skip to content

Commit 294b650

Browse files
authored
Fix regex issues PHPStan identified (#44)
1 parent a5240a8 commit 294b650

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

Spreadsheet/Excel/Writer/Worksheet.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,14 +1455,14 @@ protected function _substituteCellref($cell)
14551455
}
14561456

14571457
// Convert a cell range: 'A1:B7'
1458-
if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/", $cell, $match)) {
1458+
if (preg_match("/\\$?([A-I]?[A-Z]\\$?\\d+):\\$?([A-I]?[A-Z]\\$?\\d+)/", $cell, $match)) {
14591459
list($row1, $col1) = $this->_cellToRowcol($match[1]);
14601460
list($row2, $col2) = $this->_cellToRowcol($match[2]);
14611461
return (array($row1, $col1, $row2, $col2));
14621462
}
14631463

14641464
// Convert a cell reference: 'A1' or 'AD2000'
1465-
if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) {
1465+
if (preg_match("/\\$?([A-I]?[A-Z]\\$?\\d+)/", $cell, $match)) {
14661466
list($row1, $col1) = $this->_cellToRowcol($match[1]);
14671467
return (array($row1, $col1));
14681468
}
@@ -1481,12 +1481,12 @@ protected function _substituteCellref($cell)
14811481
*/
14821482
protected function _cellToRowcol($cell)
14831483
{
1484-
preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
1484+
preg_match("/\\$?([A-I]?[A-Z])\\$?(\\d+)/",$cell,$match);
14851485
$col = $match[1];
14861486
$row = $match[2];
14871487

14881488
// Convert base26 column string to number
1489-
$chars = explode('', $col);
1489+
$chars = str_split($col);
14901490
$expn = 0;
14911491
$col = 0;
14921492

test/Test/Spreadsheet/Excel/Writer/WorksheetTest.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,68 @@ public function doTearDown()
3434
parent::doTearDown();
3535
}
3636

37-
public function testIt()
37+
/**
38+
* Test that _substituteCellref handles regex with dollar signs correctly
39+
*/
40+
public function testSubstituteCellrefWithDollarSigns()
3841
{
39-
$this->assertInstanceOf('Spreadsheet_Excel_Writer_Worksheet', $this->worksheet);
42+
$method = new \ReflectionMethod($this->worksheet, '_substituteCellref');
43+
$method->setAccessible(true);
44+
45+
// Test absolute cell reference
46+
$result = $method->invoke($this->worksheet, '$A$1');
47+
$this->assertEquals(array(0, 0), $result);
48+
49+
// Test mixed references
50+
$result = $method->invoke($this->worksheet, 'A$1');
51+
$this->assertEquals(array(0, 0), $result);
52+
53+
$result = $method->invoke($this->worksheet, '$A1');
54+
$this->assertEquals(array(0, 0), $result);
55+
56+
// Test cell range with dollar signs
57+
$result = $method->invoke($this->worksheet, '$A$1:$B$2');
58+
$this->assertEquals(array(0, 0, 1, 1), $result);
59+
}
60+
61+
/**
62+
* Test that _cellToRowcol handles regex with dollar signs correctly
63+
*/
64+
public function testCellToRowcolWithDollarSigns()
65+
{
66+
$method = new \ReflectionMethod($this->worksheet, '_cellToRowcol');
67+
$method->setAccessible(true);
68+
69+
// Test with absolute reference
70+
$result = $method->invoke($this->worksheet, '$A$1');
71+
$this->assertEquals(array(0, 0), $result);
72+
73+
// Test with relative reference
74+
$result = $method->invoke($this->worksheet, 'B2');
75+
$this->assertEquals(array(1, 1), $result);
76+
77+
// Test with mixed reference
78+
$result = $method->invoke($this->worksheet, '$C3');
79+
$this->assertEquals(array(2, 2), $result);
80+
}
81+
82+
/**
83+
* Test that getData properly handles clearing _data property
84+
*/
85+
public function testGetDataClearsDataProperty()
86+
{
87+
// Access protected property
88+
$property = new \ReflectionProperty($this->worksheet, '_data');
89+
$property->setAccessible(true);
90+
91+
// Set some data
92+
$testData = 'test data';
93+
$property->setValue($this->worksheet, $testData);
94+
95+
// Call getData
96+
$result = $this->worksheet->getData();
97+
98+
// Check that data was returned
99+
$this->assertEquals($testData, $result);
40100
}
41101
}

0 commit comments

Comments
 (0)