Skip to content

Commit ac0e585

Browse files
committed
Fix TypeError in getValueAttribute and add type validation
- Fixed trim() being called on array values (PHPStan error) - Added type validation with exceptions for array, string, and regex types - Throw UnexpectedValueException when decoded value doesn't match expected type
1 parent b382ef2 commit ac0e585

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Setting.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static function isEditable(string $key): bool
110110
*
111111
* @param mixed $value
112112
* @return mixed
113+
* @throws \UnexpectedValueException
113114
*/
114115
public function getValueAttribute($value)
115116
{
@@ -120,7 +121,11 @@ public function getValueAttribute($value)
120121
switch ($this->type) {
121122
case 'arr':
122123
case 'array':
123-
return json_decode($value, true);
124+
$decoded = json_decode($value, true);
125+
if (!is_array($decoded)) {
126+
throw new \UnexpectedValueException("Expected array for key '{$this->key}', got " . gettype($decoded));
127+
}
128+
return $decoded;
124129
case 'int':
125130
case 'integer':
126131
return intval($value);
@@ -131,12 +136,22 @@ public function getValueAttribute($value)
131136
}
132137
return boolval($value);
133138
case 'regex':
139+
if (!is_string($value)) {
140+
throw new \UnexpectedValueException("Expected string for regex key '{$this->key}', got " . gettype($value));
141+
}
134142
$value = str_replace('ç€π', '\\', $value);
135143
$value = str_replace('@ƻ', '/', $value);
136144
return trim($value, '"');
137145
case 'string':
138-
$value = json_decode($value, true);
146+
$decoded = json_decode($value, true);
147+
if (!is_string($decoded) && $decoded !== null) {
148+
throw new \UnexpectedValueException("Expected string for key '{$this->key}', got " . gettype($decoded));
149+
}
150+
return is_string($decoded) ? trim($decoded, '"') : $decoded;
139151
default:
152+
if (!is_string($value)) {
153+
throw new \UnexpectedValueException("Expected string for key '{$this->key}', got " . gettype($value));
154+
}
140155
return trim($value, '"');
141156
}
142157
}

0 commit comments

Comments
 (0)