Skip to content

Commit aab93e0

Browse files
authored
Merge pull request #1178 from discord-php/part-optimize-fill-mutator
Optimize Part fill and mutator
2 parents 4551864 + 6fab41b commit aab93e0

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/Discord/Parts/Part.php

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,29 +160,57 @@ public function fetch(): ExtendedPromiseInterface
160160
* Fills the parts attributes from an array.
161161
*
162162
* @param array $attributes An array of attributes to build the part.
163+
*
164+
* @see self::setAttribute()
163165
*/
164166
public function fill(array $attributes): void
165167
{
166168
foreach ($this->fillable as $key) {
167169
if (array_key_exists($key, $attributes)) {
168-
$this->setAttribute($key, $attributes[$key]);
170+
// This is like setAttribute() but without in_array() checks on fillable
171+
if ($str = $this->checkForSetMutator($key)) {
172+
$this->{$str}($attributes[$key]);
173+
} else {
174+
$this->attributes[$key] = $attributes[$key];
175+
}
169176
}
170177
}
171178
}
172179

173180
/**
174-
* Checks if there is a mutator present.
181+
* Checks if there is a get mutator present.
182+
*
183+
* @param string $key The attribute name to check.
184+
*
185+
* @since 10.0.0 Replaces checkForMutator($key, 'get')
186+
*
187+
* @return string|false Either a string if it is a method or false.
188+
*/
189+
private function checkForGetMutator(string $key)
190+
{
191+
$str = 'get' . self::studly($key) . 'Attribute';
192+
193+
if (method_exists($this, $str)) {
194+
return $str;
195+
}
196+
197+
return false;
198+
}
199+
200+
/**
201+
* Checks if there is a set mutator present.
202+
*
203+
* @param string $key The attribute name to check.
175204
*
176-
* @param string $key The attribute name to check.
177-
* @param string $type Either get or set.
205+
* @since 10.0.0 Replaces checkForMutator($key, 'set')
178206
*
179-
* @return string|false Either a string if it is callable or false.
207+
* @return string|false Either a string if it is a method or false.
180208
*/
181-
private function checkForMutator(string $key, string $type)
209+
private function checkForSetMutator(string $key)
182210
{
183-
$str = $type.static::studly($key).'Attribute';
211+
$str = 'set' . self::studly($key) . 'Attribute';
184212

185-
if (is_callable([$this, $str])) {
213+
if (method_exists($this, $str)) {
186214
return $str;
187215
}
188216

@@ -207,7 +235,7 @@ private function getAttribute(string $key)
207235
return $this->repositories_cache[$key];
208236
}
209237

210-
if ($str = $this->checkForMutator($key, 'get')) {
238+
if ($str = $this->checkForGetMutator($key)) {
211239
return $this->{$str}();
212240
}
213241

@@ -226,7 +254,7 @@ private function getAttribute(string $key)
226254
*/
227255
private function setAttribute(string $key, $value): void
228256
{
229-
if ($str = $this->checkForMutator($key, 'set')) {
257+
if ($str = $this->checkForSetMutator($key)) {
230258
$this->{$str}($value);
231259

232260
return;
@@ -480,17 +508,16 @@ public function createOf(string $class, array|object $data): Part
480508
private static function studly(string $string): string
481509
{
482510
static $studlyCache = [];
483-
$key = $string;
484511

485-
if (isset($studlyCache[$key])) {
486-
return $studlyCache[$key];
512+
if (isset($studlyCache[$string])) {
513+
return $studlyCache[$string];
487514
}
488515

489516
$words = explode(' ', str_replace(['-', '_'], ' ', $string));
490517

491-
$studlyWords = array_map(fn ($word) => ucfirst($word), $words);
518+
$studlyWords = array_map('ucfirst', $words);
492519

493-
return $studlyCache[$key] = implode($studlyWords);
520+
return $studlyCache[$string] = implode($studlyWords);
494521
}
495522

496523
/**

0 commit comments

Comments
 (0)