Skip to content

Commit

Permalink
perf!: improve model performance
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removes ability to write attributes and casts arrays in case other than camelcase

INT-613
  • Loading branch information
EdieLemoine committed Sep 9, 2024
1 parent 1c9a68c commit a79da45
Show file tree
Hide file tree
Showing 36 changed files with 195 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use MyParcelNL\Pdk\Console\Types\Shared\Model\ClassDefinition;
use MyParcelNL\Pdk\Console\Types\Shared\Model\ClassProperty;
use MyParcelNL\Pdk\Tests\Factory\FactoryFactory;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use Throwable;

final class ModelFactoryGeneratorService extends AbstractFactoryGeneratorService
Expand Down
2 changes: 1 addition & 1 deletion private/PhpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use MyParcelNL\Pdk\Console\Types\Shared\Concern\ReportsTiming;
use MyParcelNL\Pdk\Console\Types\Shared\Service\ParsesPhpDocs;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use Nette\Loaders\RobotLoader;

final class PhpLoader
Expand Down
2 changes: 1 addition & 1 deletion private/Types/Documentation/DocumentationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use MyParcelNL\Pdk\Console\Types\Shared\AbstractHelperGenerator;
use MyParcelNL\Pdk\Console\Types\Shared\Model\ClassDefinition;
use MyParcelNL\Pdk\Console\Types\Shared\Model\KeyValue;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use ReflectionClass;

final class DocumentationGenerator extends AbstractHelperGenerator
Expand Down
2 changes: 1 addition & 1 deletion private/Types/Php/IdeHelperGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use MyParcelNL\Pdk\Console\Types\Shared\Model\KeyValue;
use MyParcelNL\Pdk\Console\Types\Shared\Service\PhpTypeParser;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use Symfony\Component\PropertyInfo\Type;

final class IdeHelperGenerator extends AbstractHelperGenerator
Expand Down
2 changes: 1 addition & 1 deletion private/Types/Shared/Concern/UsesCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use MyParcelNL\Pdk\Console\Storage\ConsoleMemoryCacheStorage;
use MyParcelNL\Pdk\Console\Storage\Contract\ConsoleStorageInterface;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use function sprintf;

trait UsesCache
Expand Down
2 changes: 1 addition & 1 deletion private/Types/Shared/Service/PhpSourceParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use MyParcelNL\Pdk\Console\Types\Shared\Concern\ReportsTiming;
use MyParcelNL\Pdk\Console\Types\Shared\Model\ClassDefinition;
use MyParcelNL\Pdk\Console\Types\Shared\Model\KeyValue;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use ReflectionClass;
use ReflectionMethod;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
Expand Down
2 changes: 1 addition & 1 deletion private/Types/Shared/Service/PhpTypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;
use Symfony\Component\PropertyInfo\Type;

class PhpTypeParser
Expand Down
2 changes: 1 addition & 1 deletion private/Types/TypeScript/TsTypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use DateTimeImmutable;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Console\Types\Shared\Concern\UsesCache;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

class TsTypeParser
{
Expand Down
2 changes: 1 addition & 1 deletion src/App/Action/Backend/Webhook/AbstractWebhooksAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use MyParcelNL\Pdk\Webhook\Collection\WebhookSubscriptionCollection;
use MyParcelNL\Pdk\Webhook\Model\WebhookSubscription;
use MyParcelNL\Pdk\Webhook\Repository\WebhookSubscriptionRepository;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

abstract class AbstractWebhooksAction implements ActionInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;
use MyParcelNL\Pdk\Shipment\Model\ShipmentOptions;
use MyParcelNL\Pdk\Types\Service\TriStateService;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

class DeliveryOptionsFeesService implements DeliveryOptionsFeesServiceInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/App/DeliveryOptions/Service/DeliveryOptionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;
use MyParcelNL\Pdk\Validation\Repository\SchemaRepository;
use MyParcelNL\Pdk\Validation\Validator\OrderPropertiesValidator;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

class DeliveryOptionsService implements DeliveryOptionsServiceInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use MyParcelNL\Pdk\Fulfilment\Model\OrderNote;
use MyParcelNL\Pdk\Settings\Model\LabelSettings;
use MyParcelNL\Pdk\Types\Service\TriStateService;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

final class LabelDescriptionCalculator extends AbstractPdkOrderOptionCalculator
{
Expand Down
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\Sdk\src\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 @@ -582,7 +582,7 @@ protected function getCastAttribute(string $string)
protected function getCastType(string $key): ?string
{
$casts = $this->getCasts();
$normalizedKey = Utils::changeCase($key);
$normalizedKey = Str::changeCase($key);

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Base/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use MyParcelNL\Pdk\Base\Contract\ConfigInterface;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Facade\Pdk as PdkFacade;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

class Config implements ConfigInterface
{
Expand Down
32 changes: 25 additions & 7 deletions src/Base/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Contract\ModelInterface;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Str;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Sdk\src\Support\Str;

/**
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
Expand All @@ -29,6 +30,8 @@ class Model implements StorableArrayable, ArrayAccess, ModelInterface
*/
protected static $traitInitializers;

protected $lazy = [];

/**
* @var bool
*/
Expand All @@ -41,14 +44,29 @@ 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;

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

$this->fill($convertedData + $this->attributes);
$this->fill($filteredAttributes);
}

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

public static function isBooted(): bool
Expand Down Expand Up @@ -225,7 +243,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 @@ -313,7 +331,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
2 changes: 1 addition & 1 deletion src/Base/Service/CountryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use MyParcelNL\Pdk\Base\Contract\CountryServiceInterface;
use MyParcelNL\Pdk\Facade\Platform;
use MyParcelNL\Sdk\src\Support\Str;
use MyParcelNL\Pdk\Base\Support\Str;

class CountryService implements CountryServiceInterface
{
Expand Down
57 changes: 57 additions & 0 deletions src/Base/Support/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Base\Support;

use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Sdk\src\Support\Str as SdkStr;

class Str extends SdkStr
{
/**
* @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 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];
}
}
Loading

0 comments on commit a79da45

Please sign in to comment.