Skip to content

Commit 95b83f4

Browse files
authored
Merge pull request #4717 from oleibman/explicit
Prepare to Deprecate Optionality of Second Arg to Cell::setValueExplicit
2 parents 98206d9 + aa719cd commit 95b83f4

File tree

5 files changed

+144
-1
lines changed

5 files changed

+144
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a
2828

2929
### Deprecated
3030

31-
- Nothing yet.
31+
- $dataType, the second parameter of Cell::setValueExplicit, is currently optional. Omitting it is deprecated, and it will be required in a future release.
3232

3333
### Fixed
3434

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ public function setValue(mixed $value, ?IValueBinder $binder = null): self
247247
*
248248
* @param mixed $value Value
249249
* @param string $dataType Explicit data type, see DataType::TYPE_*
250+
* This parameter is currently optional (default = string).
251+
* Omitting it is ***DEPRECATED***, and the default will be removed in a future release.
250252
* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this
251253
* method, then it is your responsibility as an end-user developer to validate that the value and
252254
* the datatype match.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Cell;
6+
7+
use ArgumentCountError;
8+
use PhpOffice\PhpSpreadsheet\Cell\Cell;
9+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use PHPUnit\Framework\TestCase;
12+
use ReflectionMethod;
13+
14+
class SetValueExplicitCellTest extends TestCase
15+
{
16+
protected string $method = 'setValueExplicit';
17+
18+
protected int $requiredParameters = 1;
19+
20+
public function testRequired(): void
21+
{
22+
$reflectionMethod = new ReflectionMethod(Cell::class, $this->method);
23+
$requiredParameters = $reflectionMethod->getNumberOfRequiredParameters();
24+
self::assertSame($this->requiredParameters, $requiredParameters);
25+
}
26+
27+
public static function setValueExplicitTypeArgumentProvider(): array
28+
{
29+
return require 'tests/data/Cell/SetValueExplicitTypeArguments.php';
30+
}
31+
32+
#[DataProvider('setValueExplicitTypeArgumentProvider')]
33+
public function testSetValueExplicitTypeArgumentHandling(
34+
mixed $value,
35+
?string $dataType,
36+
mixed $expectedValue,
37+
string $expectedDataType
38+
): void {
39+
$spreadsheet = new Spreadsheet();
40+
$worksheet = $spreadsheet->getActiveSheet();
41+
$coordinate = 'A1';
42+
$cell = $worksheet->getCell($coordinate);
43+
44+
try {
45+
if ($dataType !== null) {
46+
$cell->{$this->method}($value, $dataType);
47+
} else {
48+
$cell->{$this->method}($value);
49+
self::assertSame(1, $this->requiredParameters);
50+
}
51+
self::assertSame($expectedValue, $cell->getValue());
52+
self::assertSame($expectedDataType, $cell->getDataType());
53+
} catch (ArgumentCountError) {
54+
self::assertSame(2, $this->requiredParameters);
55+
self::assertNull($dataType);
56+
}
57+
58+
$spreadsheet->disconnectWorksheets();
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use ArgumentCountError;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use PHPUnit\Framework\TestCase;
12+
use ReflectionMethod;
13+
14+
class SetValueExplicitWorksheetTest extends TestCase
15+
{
16+
protected string $method = 'setCellValueExplicit';
17+
18+
protected int $requiredParameters = 3;
19+
20+
public function testRequired(): void
21+
{
22+
$reflectionMethod = new ReflectionMethod(Worksheet::class, $this->method);
23+
$requiredParameters = $reflectionMethod->getNumberOfRequiredParameters();
24+
self::assertSame($this->requiredParameters, $requiredParameters);
25+
}
26+
27+
public static function setCellValueExplicitTypeArgumentProvider(): array
28+
{
29+
return require 'tests/data/Cell/SetValueExplicitTypeArguments.php';
30+
}
31+
32+
#[DataProvider('setCellValueExplicitTypeArgumentProvider')]
33+
public function testSetCellValueExplicitTypeArgumentHandling(
34+
mixed $value,
35+
?string $dataType,
36+
mixed $expectedValue,
37+
string $expectedDataType
38+
): void {
39+
$spreadsheet = new Spreadsheet();
40+
$worksheet = $spreadsheet->getActiveSheet();
41+
$coordinate = 'A1';
42+
$cell = $worksheet->getCell($coordinate);
43+
44+
try {
45+
if ($dataType) {
46+
$worksheet->{$this->method}($coordinate, $value, $dataType);
47+
} else {
48+
$worksheet->{$this->method}($coordinate, $value);
49+
self::assertSame(2, $this->requiredParameters);
50+
}
51+
self::assertSame($expectedValue, $cell->getValue());
52+
self::assertSame($expectedDataType, $cell->getDataType());
53+
} catch (ArgumentCountError) {
54+
self::assertSame(3, $this->requiredParameters);
55+
self::assertNull($dataType);
56+
}
57+
58+
$spreadsheet->disconnectWorksheets();
59+
}
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6+
7+
// This member will be used to validate both:
8+
// Cell::setValueExplicit
9+
// Worksheet::setCellValueExplicit
10+
11+
return [
12+
'string with default type' => ['default string', null, 'default string', DataType::TYPE_STRING],
13+
'integer with default type' => [42, null, '42', DataType::TYPE_STRING],
14+
'string with string' => ['explicit string', DataType::TYPE_STRING, 'explicit string', DataType::TYPE_STRING],
15+
'integer with string type' => [123, DataType::TYPE_STRING, '123', DataType::TYPE_STRING],
16+
'numeric string with string type' => ['496', DataType::TYPE_STRING, '496', DataType::TYPE_STRING],
17+
'integer with numeric type' => [591, DataType::TYPE_NUMERIC, 591, DataType::TYPE_NUMERIC],
18+
'numeric string with numeric type' => ['1887', DataType::TYPE_NUMERIC, 1887, DataType::TYPE_NUMERIC],
19+
'true with bool type' => [true, DataType::TYPE_BOOL, true, DataType::TYPE_BOOL],
20+
'false with bool type' => [false, DataType::TYPE_BOOL, false, DataType::TYPE_BOOL],
21+
];

0 commit comments

Comments
 (0)