Skip to content

Commit

Permalink
DataValidation Doesn't Need __construct nor __clone
Browse files Browse the repository at this point in the history
Constructor does nothing. Class doesn't require deep clone since all properties are bool or string.
  • Loading branch information
oleibman committed Jan 2, 2025
1 parent 6632117 commit 441abf0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
22 changes: 0 additions & 22 deletions src/PhpSpreadsheet/Cell/DataValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ class DataValidation
*/
private string $prompt = '';

/**
* Create a new DataValidation.
*/
public function __construct()
{
}

/**
* Get Formula 1.
*/
Expand Down Expand Up @@ -390,21 +383,6 @@ public function getHashCode(): string
);
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if (is_object($value)) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}
}
}

private ?string $sqref = null;

public function getSqref(): ?string
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpSpreadsheetTests/Cell/DataValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function testNoValidation(): void
$testCell = $sheet->getCell('A1');

self::assertTrue($testCell->hasValidValue(), 'a cell without any validation data is always valid');
$spreadsheet->disconnectWorksheets();
}

public function testUnsupportedType(): void
Expand All @@ -30,6 +31,7 @@ public function testUnsupportedType(): void
$validation->setAllowBlank(true);

self::assertFalse($testCell->hasValidValue(), 'cannot assert that value is valid when the validation type is not supported');
$spreadsheet->disconnectWorksheets();
}

public function testList(): void
Expand Down Expand Up @@ -71,5 +73,24 @@ public function testList(): void
$validation->setFormula1('broken : cell : coordinates');

self::assertFalse($testCell->hasValidValue(), 'invalid formula should not throw exceptions');
$spreadsheet->disconnectWorksheets();
}

public function testInvalidNumeric(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$validation = $sheet->getCell('A1')->getDataValidation();
$validation->setType(DataValidation::TYPE_WHOLE)
->setOperator(DataValidation::OPERATOR_EQUAL)
->setFormula1('broken : cell : coordinates');
$sheet->getCell('A1')->setValue(0);
self::assertFalse($sheet->getCell('A1')->hasValidValue(), 'invalid formula should return false');
$validation->setOperator('invalid operator')
->setFormula1('0');
self::assertFalse($sheet->getCell('A1')->hasValidValue(), 'invalid operator should return false');

$spreadsheet->disconnectWorksheets();
}
}

0 comments on commit 441abf0

Please sign in to comment.