Skip to content

Commit 3528720

Browse files
committed
Resolve Merge Conflicts
1 parent e404389 commit 3528720

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/PhpSpreadsheet/Cell/DefaultValueBinder.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ public static function dataTypeForValue(mixed $value): string
6161
if ($value instanceof RichText) {
6262
return DataType::TYPE_INLINE;
6363
}
64-
if (is_string($value) && strlen($value) > 1 && $value[0] === '=') {
64+
if ($value instanceof Stringable) {
65+
$value = (string) $value;
66+
}
67+
if (!is_string($value)) {
68+
$gettype = is_object($value) ? get_class($value) : gettype($value);
69+
70+
throw new SpreadsheetException("unusable type $gettype");
71+
}
72+
if (strlen($value) > 1 && $value[0] === '=') {
6573
$calculation = new Calculation();
6674
$calculation->disableBranchPruning();
6775

@@ -93,11 +101,9 @@ public static function dataTypeForValue(mixed $value): string
93101

94102
return DataType::TYPE_NUMERIC;
95103
}
96-
if (is_string($value)) {
97-
$errorCodes = DataType::getErrorCodes();
98-
if (isset($errorCodes[$value])) {
99-
return DataType::TYPE_ERROR;
100-
}
104+
$errorCodes = DataType::getErrorCodes();
105+
if (isset($errorCodes[$value])) {
106+
return DataType::TYPE_ERROR;
101107
}
102108

103109
return DataType::TYPE_STRING;

tests/PhpSpreadsheetTests/Cell/DefaultValueBinderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,27 @@ public function testCanOverrideStaticMethodWithoutOverridingBindValue(): void
108108
self::assertTrue($binder::$called);
109109
$spreadsheet->disconnectWorksheets();
110110
}
111+
112+
public function testDataTypeForValueExceptions(): void
113+
{
114+
try {
115+
self::assertSame('s', DefaultValueBinder::dataTypeForValue(new SpreadsheetException()));
116+
} catch (SpreadsheetException $e) {
117+
self::fail('Should not have failed for stringable');
118+
}
119+
120+
try {
121+
DefaultValueBinder::dataTypeForValue([]);
122+
self::fail('Should have failed for array');
123+
} catch (SpreadsheetException $e) {
124+
self::assertStringContainsString('unusable type array', $e->getMessage());
125+
}
126+
127+
try {
128+
DefaultValueBinder::dataTypeForValue(new DateTime());
129+
self::fail('Should have failed for DateTime');
130+
} catch (SpreadsheetException $e) {
131+
self::assertStringContainsString('unusable type DateTime', $e->getMessage());
132+
}
133+
}
111134
}

0 commit comments

Comments
 (0)