Skip to content

Commit

Permalink
Null Passed to AutoFilter SetRange (#2454)
Browse files Browse the repository at this point in the history
* Null Passed to AutoFilter SetRange

Fix #2281. Delete auto filter set range to null, but should set it to null string. This causes a deprecation warning in Php8.1.

* Constructor Call Also Sets Range to Null

Should set it to null string.
  • Loading branch information
oleibman authored Dec 25, 2021
1 parent 707b7d5 commit f2b2f07
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6065,16 +6065,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Worksheet/Worksheet.php

-
message: "#^Parameter \\#1 \\$range of class PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter constructor expects string, null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/Worksheet.php

-
message: "#^Parameter \\#1 \\$range of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\:\\:setRange\\(\\) expects string, null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/Worksheet.php

-
message: "#^Parameter \\#1 \\$range of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Coordinate\\:\\:rangeDimension\\(\\) expects string, string\\|false given\\.$#"
count: 1
Expand Down
12 changes: 3 additions & 9 deletions src/PhpSpreadsheet/Worksheet/AutoFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ public function setEvaluated(bool $value): void

/**
* Create a new AutoFilter.
*
* @param string $range Cell range (i.e. A1:E10)
*/
public function __construct($range = '', ?Worksheet $worksheet = null)
public function __construct(string $range = '', ?Worksheet $worksheet = null)
{
$this->range = $range;
$this->workSheet = $worksheet;
Expand Down Expand Up @@ -93,13 +91,9 @@ public function getRange()
}

/**
* Set AutoFilter Range.
*
* @param string $range Cell range (i.e. A1:E10)
*
* @return $this
* Set AutoFilter Cell Range.
*/
public function setRange($range)
public function setRange(string $range): self
{
$this->evaluated = false;
// extract coordinate
Expand Down
8 changes: 3 additions & 5 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function __construct(?Spreadsheet $parent = null, $title = 'Worksheet')
$this->defaultRowDimension = new RowDimension(null);
// Default column dimension
$this->defaultColumnDimension = new ColumnDimension(null);
$this->autoFilter = new AutoFilter(null, $this);
$this->autoFilter = new AutoFilter('', $this);
}

/**
Expand Down Expand Up @@ -1914,12 +1914,10 @@ public function setAutoFilterByColumnAndRow($columnIndex1, $row1, $columnIndex2,

/**
* Remove autofilter.
*
* @return $this
*/
public function removeAutoFilter()
public function removeAutoFilter(): self
{
$this->autoFilter->setRange(null);
$this->autoFilter->setRange('');

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter;

class DeleteAutoFilterTest extends SetupTeardown
{
public function testDelete(): void
{
// Issue 2281 - deprecation in PHP81 when deleting filter
$sheet = $this->getSheet();
$autoFilter = $sheet->getAutoFilter();
$autoFilter->setRange('H2:O256');
$sheet->removeAutoFilter();
self::assertSame('', $sheet->getAutoFilter()->getRange());
}
}

0 comments on commit f2b2f07

Please sign in to comment.