Skip to content

Commit

Permalink
perf!: improve model performance (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine authored Nov 14, 2024
1 parent 176b772 commit 63cbeac
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 113 deletions.
4 changes: 3 additions & 1 deletion src/App/Order/Model/PdkProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function toStorableArray(): array

/**
* @return \MyParcelNL\Pdk\Settings\Model\ProductSettings
* @noinspection PhpUnused
*/
protected function getMergedSettingsAttribute(): ProductSettings
{
Expand Down Expand Up @@ -121,12 +122,13 @@ private function resolveMergedSettings(): ProductSettings
return $settings;
}

/** @var \MyParcelNL\Pdk\Types\Contract\TriStateServiceInterface $triStateService */
$triStateService = Pdk::get(TriStateServiceInterface::class);

foreach ($settings->getAttributes() as $key => $value) {
$coerced = $triStateService->coerce($settings->getAttribute($key));

if ($coerced === '-1') {
if ((string) TriStateService::INHERIT === $coerced) {
$coerced = (int) $coerced;
}

Expand Down
18 changes: 9 additions & 9 deletions src/Base/Concern/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use MyParcelNL\Pdk\Base\Exception\InvalidCastException;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Base\Support\Str;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Types\Contract\TriStateServiceInterface;
use MyParcelNL\Pdk\Types\Service\TriStateService;
use MyParcelNL\Pdk\Base\Support\Str;
use Throwable;

/**
Expand Down Expand Up @@ -183,7 +183,7 @@ public function getAttribute(string $key)
return null;
}

$key = Utils::changeCase($key);
$key = Str::changeCase($key);

if ($this->isGuarded($key)) {
return $this->guarded[$key];
Expand Down Expand Up @@ -269,7 +269,7 @@ public function only($attributes, ?int $flags = null): array
*/
public function setAttribute(string $key, $value): self
{
$key = $this->convertDeprecatedKey(Utils::changeCase($key));
$key = $this->convertDeprecatedKey(Str::changeCase($key));

if ($this->isGuarded($key)) {
return $this;
Expand Down Expand Up @@ -301,8 +301,8 @@ public function setAttribute(string $key, $value): self
protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes, ?int $flags): array
{
foreach ($this->getCasts() as $key => $value) {
$originalKey = Utils::changeCase($key);
$key = Utils::changeCase($key, $flags);
$originalKey = Str::changeCase($key);
$key = Str::changeCase($key, $flags);

if (! array_key_exists($key, $attributes) || in_array($key, $mutatedAttributes, true)) {
continue;
Expand Down Expand Up @@ -358,8 +358,8 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
protected function addMutatedAttributesToArray(array $attributes, array $mutatedAttributes, ?int $flags): array
{
foreach ($mutatedAttributes as $key) {
$originalKey = Utils::changeCase($key);
$key = Utils::changeCase($key, $flags);
$originalKey = Str::changeCase($key);
$key = Str::changeCase($key, $flags);

if (! array_key_exists($key, $attributes)) {
continue;
Expand Down Expand Up @@ -585,7 +585,7 @@ protected function getCastAttributeValue(string $string)
protected function getCastType(string $key): ?string
{
$casts = $this->getCasts();
$normalizedKey = Utils::changeCase($key);
$normalizedKey = Str::changeCase($key);

return $casts[$normalizedKey];
}
Expand All @@ -597,7 +597,7 @@ protected function getCastType(string $key): ?string
*/
protected function getCasts(): array
{
return Utils::changeArrayKeysCase($this->casts);
return $this->casts;
}

/**
Expand Down
35 changes: 28 additions & 7 deletions src/Base/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Contract\ModelInterface;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Str;
use MyParcelNL\Pdk\Base\Support\Utils;

/**
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
Expand All @@ -36,21 +37,31 @@ class Model implements StorableArrayable, ArrayAccess, ModelInterface
*/
protected $cloned = false;

/**
* @var array
*/
protected $lazy = [];

/**
* @param null|array $data
*/
public function __construct(?array $data = null)
{
$this->bootIfNotBooted();

$this->guarded = Utils::changeArrayKeysCase($this->guarded);
$this->attributes = $this->guarded + Utils::changeArrayKeysCase($this->attributes);
$this->attributes = $this->guarded + $this->attributes;

$this->initializeTraits();

$convertedData = Utils::changeArrayKeysCase($data ?? []);
// filter out the items that are in $this->lazy
$attributes = Utils::changeArrayKeysCase($data ?? []) + $this->attributes;

$this->fill($convertedData + $this->attributes);
$filteredAttributes = count($this->lazy)
? Arr::where($attributes, function ($value, $key) {
return $value !== null || ! $this->isLazy($key);
}) : $attributes;

$this->fill($filteredAttributes);
}

public static function isBooted(): bool
Expand Down Expand Up @@ -215,7 +226,7 @@ public function offsetSet($offset, $value): void
*/
public function offsetUnset($offset): void
{
unset($this->attributes[(Utils::changeCase($offset))]);
unset($this->attributes[(Str::changeCase($offset))]);
}

/**
Expand Down Expand Up @@ -293,6 +304,16 @@ protected function initializeTraits(): void
}
}

/**
* @param string $key
*
* @return bool
*/
private function isLazy(string $key): bool
{
return in_array($key, $this->lazy, true);
}

/**
* @param array $attributes
*
Expand All @@ -303,7 +324,7 @@ private function normalizeAttributes(array $attributes): array
$normalizedAttributes = [];

foreach ($attributes as $initialKey => $value) {
$caseKey = Utils::changeCase($initialKey);
$caseKey = Str::changeCase($initialKey);
$key = $this->convertDeprecatedKey($caseKey);

if (array_key_exists($key, $normalizedAttributes)) {
Expand Down
48 changes: 48 additions & 0 deletions src/Base/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@

namespace MyParcelNL\Pdk\Base\Support;

use MyParcelNL\Pdk\Base\Contract\Arrayable;

class Str extends \Illuminate\Support\Str
{
/**
* @var array
*/
private static $flagCaseCache = [];

/**
* @param string $string
* @param null|int $flags
*
* @return string
*/
public static function changeCase(string $string, ?int $flags = null): string
{
$case = self::getFlagCase($flags);

return self::{$case}($string);
}

/**
* @param string $value
* @param int $limit
Expand All @@ -17,4 +37,32 @@ public static function limit($value, $limit = 100, $end = '...'): string
{
return parent::limit($value, $limit - strlen($end), $end);
}

/**
* @param null|int $flags
*
* @return string
*/
private static function getFlagCase(?int $flags = null): string
{
if (! isset(self::$flagCaseCache[$flags])) {
$case = 'camel';

if ($flags & Arrayable::CASE_SNAKE) {
$case = 'snake';
}

if ($flags & Arrayable::CASE_KEBAB) {
$case = 'kebab';
}

if ($flags & Arrayable::CASE_STUDLY) {
$case = 'studly';
}

self::$flagCaseCache[$flags] = $case;
}

return self::$flagCaseCache[$flags];
}
}
37 changes: 1 addition & 36 deletions src/Base/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,13 @@ public static function changeArrayKeysCase(array $array, ?int $flags = null): ar
$value = self::changeArrayKeysCase($value, $flags);
}

$newKey = self::changeCase($key, $flags);
$newKey = Str::changeCase($key, $flags);
$newArray[$newKey] = $value;
}

return $newArray;
}

/**
* @param string $string
* @param null|int $flags
*
* @return string
*/
public static function changeCase(string $string, ?int $flags = null): string
{
$case = self::getFlagCase($flags);

return Str::{$case}($string);
}

/**
* Get the class "basename" of the given object / class.
*
Expand Down Expand Up @@ -264,26 +251,4 @@ public static function toRecursiveCollection(array $array): Collection

return $collection;
}

/**
* @param null|int $flags
*
* @return string
*/
private static function getFlagCase(?int $flags = null): string
{
if ($flags & Arrayable::CASE_SNAKE) {
return 'snake';
}

if ($flags & Arrayable::CASE_KEBAB) {
return 'kebab';
}

if ($flags & Arrayable::CASE_STUDLY) {
return 'studly';
}

return 'camel';
}
}
15 changes: 15 additions & 0 deletions src/Context/Model/ContextBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ class ContextBag extends Model
Context::ID_PRODUCT_DATA => ProductDataContextCollection::class,
Context::ID_PRODUCT_SETTINGS_VIEW => ProductSettingsViewContext::class,
];

public $lazy = [
Context::ID_GLOBAL,
Context::ID_DYNAMIC,
Context::ID_CHECKOUT,
Context::ID_ORDER_DATA,
Context::ID_PLUGIN_SETTINGS_VIEW,
Context::ID_PRODUCT_DATA,
Context::ID_PRODUCT_SETTINGS_VIEW,
];

public function __construct(?array $data = null)
{
parent::__construct($data);
}
}
8 changes: 4 additions & 4 deletions src/Fulfilment/Model/ShippedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
class ShippedItem extends Model
{
public $attributes = [
'order_line_identifier' => null,
'quantity' => 1,
'orderLineIdentifier' => null,
'quantity' => 1,
];

protected $casts = [
'order_line_identifier' => 'string',
'quantity' => 'int',
'orderLineIdentifier' => 'string',
'quantity' => 'int',
];
}
2 changes: 1 addition & 1 deletion src/Shipment/Repository/ShipmentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function fetchLabelPdf(
*/
public function getByReferenceIdentifiers(array $referenceIdentifiers, ?int $size = null): ShipmentCollection
{
return $this->query(['reference_identifier' => $referenceIdentifiers, 'size' => $size]);
return $this->query(['referenceIdentifier' => $referenceIdentifiers, 'size' => $size]);
}

/**
Expand Down
Loading

0 comments on commit 63cbeac

Please sign in to comment.