Skip to content

Commit

Permalink
refactor: phpstan (3 lvl WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Sep 16, 2024
1 parent 1559176 commit cd26d36
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 36 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ includes:
- ./vendor/larastan/larastan/extension.neon

parameters:
editorUrl: 'anything'
editorUrlTitle: "\nat packages/moonshine/%%relFile%%:%%line%%"
paths:
- src/
level: 2
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/src/UI/HasFieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
interface HasFieldsContract
{
/**
* @param T|Closure|array $fields
* @param T|Closure(T $ctx): T|iterable $fields
*
* @return static
*/
public function fields(FieldsContract|Closure|array $fields): static;
public function fields(FieldsContract|Closure|iterable $fields): static;

public function hasFields(): bool;

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Traits/WithViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function render(): Renderable|Closure|string
$this->prepareBeforeRender();

if (! is_null($this->onBeforeRenderCallback)) {
value($this->onBeforeRenderCallback, $this);
call_user_func($this->onBeforeRenderCallback, $this);
}

$view = $this->resolveRender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function toFormattedValue(): mixed

if ($this->isToOne() && ! is_null($this->getFormattedValueCallback())) {
$this->setFormattedValue(
value(
call_user_func(
$this->getFormattedValueCallback(),
$value ?? $this->getRelation()?->getModel(),
$this->getRowIndex(),
Expand Down
7 changes: 5 additions & 2 deletions src/UI/src/Collections/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,15 @@ public function withoutHasFields(): static
}

/**
* @param ?callable(FieldContract,FieldContract): FieldsContract $before
* @param ?callable(string,FieldContract,FieldContract): string $performName
*
* @throws Throwable
*/
public function prepareReindexNames(?FieldContract $parent = null, ?callable $before = null, ?callable $performName = null): static
{
return $this->map(static function (FieldContract $field) use ($parent, $before, $performName): FieldContract {
$modifyField = value($before, $parent, $field);
$modifyField = !is_null($before) ? $before($parent, $field) : $field;

if ($modifyField instanceof FieldContract) {
$field = $modifyField;
Expand Down Expand Up @@ -245,7 +248,7 @@ public function prepareReindexNames(?FieldContract $parent = null, ?callable $be

return $field
->setNameAttribute(
is_null($performName) ? $name : value($performName, $name, $parent, $field)
is_null($performName) ? $name : $performName($name, $parent, $field)
)
->iterableAttributes($level);
})
Expand Down
6 changes: 4 additions & 2 deletions src/UI/src/Components/ActionButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ public function getData(): ?DataWrapperContract
public function setData(?DataWrapperContract $data = null): static
{
if (! is_null($this->onBeforeSetCallback)) {
$data = value($this->onBeforeSetCallback, $data, $this);
$data = call_user_func($this->onBeforeSetCallback, $data, $this);
}

$this->data = $data;

value($this->onAfterSetCallback, $data, $this);
if (! is_null($this->onAfterSetCallback)) {
call_user_func($this->onAfterSetCallback, $data, $this);
}

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Components/CardsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getComponents(): Collection
$fields = $this->getFilledFields($casted->toArray(), $casted, $index, $fields);

if (! is_null($this->customComponent)) {
return value($this->customComponent, $data, $index, $this);
return call_user_func($this->customComponent, $data, $index, $this);
}

$buttons = $this->getButtons($casted);
Expand Down
1 change: 1 addition & 0 deletions src/UI/src/Components/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
final class FormBuilder extends MoonShineComponent implements FormBuilderContract
{
use HasAsync;
/** @use WithAdditionalFields<TFields> */
use WithAdditionalFields;
use HasDataCast;
/** @use WithFields<TFields> */
Expand Down
18 changes: 9 additions & 9 deletions src/UI/src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ public function beforeRender(Closure $callback): static

public function getBeforeRender(): Renderable|string
{
return is_null($this->beforeRender)
? ''
: value($this->beforeRender, $this);
return !is_null($this->beforeRender)
? call_user_func($this->beforeRender, $this)
: '';
}

/**
Expand All @@ -300,9 +300,9 @@ public function afterRender(Closure $callback): static

public function getAfterRender(): Renderable|string
{
return is_null($this->afterRender)
? ''
: value($this->afterRender, $this);
return !is_null($this->afterRender)
? call_user_func($this->afterRender, $this)
: '';
}

/**
Expand All @@ -312,7 +312,7 @@ public function getAfterRender(): Renderable|string
protected function prepareBeforeRender(): void
{
if (! is_null($this->onChangeUrl) && $this->isOnChangeCondition()) {
$onChangeUrl = value($this->onChangeUrl, $this->getData(), $this->toValue(), $this);
$onChangeUrl = call_user_func($this->onChangeUrl, $this->getData(), $this->toValue(), $this);

$this->customAttributes(
$this->getOnChangeEventAttributes($onChangeUrl),
Expand Down Expand Up @@ -353,7 +353,7 @@ public function preview(): Renderable|string
}

if ($this->isPreviewChanged()) {
return (string) value(
return (string) call_user_func(
$this->previewCallback,
$this->toValue(),
$this,
Expand Down Expand Up @@ -419,7 +419,7 @@ protected function resolveRender(): Renderable|Closure|string
}

if ($this->isRenderChanged()) {
return value(
return call_user_func(
$this->renderCallback,
$this->toValue(),
$this,
Expand Down
4 changes: 2 additions & 2 deletions src/UI/src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ protected function getFiles(): Collection
$index => new FileItem(
fullPath: $path,
rawValue: data_get($this->toValue(), $index, $this->toValue()),
name: (string) value($this->resolveNames(), $path, $index, $this),
attributes: value($this->resolveItemAttributes(), $path, $index, $this),
name: (string) call_user_func($this->resolveNames(), $path, $index, $this),
attributes: call_user_func($this->resolveItemAttributes(), $path, $index, $this),
),
]);
}
Expand Down
10 changes: 5 additions & 5 deletions src/UI/src/Fields/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function getVirtualColumn(): string
protected function prepareFill(array $raw = [], ?DataWrapperContract $casted = null): mixed
{
if ($this->isFillChanged()) {
return value(
return call_user_func(
$this->fillCallback,
is_null($casted) ? $raw : $casted->getOriginal(),
$this
Expand Down Expand Up @@ -272,7 +272,7 @@ public function fill(mixed $value = null, ?DataWrapperContract $casted = null, i
public function toRawValue(): mixed
{
if ($this->isRawValueModified()) {
return value($this->rawValueCallback, $this->rawValue, $this->getData()?->getOriginal(), $this);
return call_user_func($this->rawValueCallback, $this->rawValue, $this->getData()?->getOriginal(), $this);
}

return $this->resolveRawValue();
Expand Down Expand Up @@ -375,7 +375,7 @@ public function toFormattedValue(): mixed
{
if (! is_null($this->getFormattedValueCallback())) {
$this->setFormattedValue(
value(
call_user_func(
$this->getFormattedValueCallback(),
$this->getData()?->getOriginal(),
$this->getRowIndex(),
Expand Down Expand Up @@ -459,7 +459,7 @@ public function getValueFromRaw(mixed $raw): mixed
return $raw;
}

return value($this->fromRaw, $raw, $this);
return call_user_func($this->fromRaw, $raw, $this);
}

public function getDefaultIfExists(): mixed
Expand Down Expand Up @@ -529,7 +529,7 @@ public static function requestValueResolver(Closure $resolver): void
public function getRequestValue(string|int|null $index = null): mixed
{
if (! is_null(static::$requestValueResolver)) {
return value(static::$requestValueResolver, $index, $this->getDefaultIfExists(), $this);
return call_user_func(static::$requestValueResolver, $index, $this->getDefaultIfExists(), $this);
}

return $this->prepareRequestValue(
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Fields/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function getButtons(): array
->showInLine();

if (! is_null($this->modifyRemoveButton)) {
$button = value($this->modifyRemoveButton, $button, $this);
$button = call_user_func($this->modifyRemoveButton, $button, $this);
}

$buttons[] = $button;
Expand Down
8 changes: 6 additions & 2 deletions src/UI/src/Fields/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function resolvePreview(): string|Renderable
protected function prepareFill(array $raw = [], ?DataWrapperContract $casted = null): mixed
{
if ($this->isFillChanged()) {
return value(
return call_user_func(
$this->fillCallback,
is_null($casted) ? $raw : $casted->getOriginal(),
$this
Expand All @@ -61,7 +61,11 @@ public function changeRender(Closure $callback): static

public function render(): string
{
return (string) value($this->renderCallback, $this->toValue(), $this);
if(is_null($this->renderCallback)) {
return '';
}

return (string) call_user_func($this->renderCallback, $this->toValue(), $this);
}

protected function resolveOnApply(): ?Closure
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Fields/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function resolvePreview(): Renderable|string
href: $value,
value: is_null($this->titleCallback)
? $title
: (string) value($this->titleCallback, $title, $this),
: (string) call_user_func($this->titleCallback, $title, $this),
blank: $this->blank
)->render();
}
Expand Down
8 changes: 4 additions & 4 deletions src/UI/src/Traits/Fields/Applies.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function isCanApply(): bool
return true;
}

return (bool) value($this->canApply, $this);
return (bool) call_user_func($this->canApply, $this);
}

protected function resolveOnApply(): ?Closure
Expand Down Expand Up @@ -97,7 +97,7 @@ public function beforeApply(mixed $data): mixed

return is_null($this->onBeforeApply)
? $this->resolveBeforeApply($data)
: value($this->onBeforeApply, $data, $this->getRequestValue(), $this);
: call_user_func($this->onBeforeApply, $data, $this->getRequestValue(), $this);
}

public function afterApply(mixed $data): mixed
Expand All @@ -108,14 +108,14 @@ public function afterApply(mixed $data): mixed

return is_null($this->onAfterApply)
? $this->resolveAfterApply($data)
: value($this->onAfterApply, $data, $this->getRequestValue(), $this);
: call_user_func($this->onAfterApply, $data, $this->getRequestValue(), $this);
}

public function afterDestroy(mixed $data): mixed
{
return is_null($this->onAfterDestroy)
? $this->resolveAfterDestroy($data)
: value($this->onAfterDestroy, $data, $this->getRequestValue(), $this);
: call_user_func($this->onAfterDestroy, $data, $this->getRequestValue(), $this);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/UI/src/Traits/Fields/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function names(Closure $callback): static
return $this;
}

/** @return Closure(string, int): string */
public function resolveNames(): Closure
{
return function (string $filename, int $index = 0): string {
Expand All @@ -65,6 +66,9 @@ public function itemAttributes(Closure $callback): static
return $this;
}

/**
* @return Closure(string $filename, int $index): ComponentAttributesBagContract
*/
public function resolveItemAttributes(): Closure
{
return function (string $filename, int $index = 0): ComponentAttributesBagContract {
Expand All @@ -73,7 +77,7 @@ public function resolveItemAttributes(): Closure
}

return new MoonShineComponentAttributeBag(
(array) value($this->itemAttributes, $filename, $index)
(array) call_user_func($this->itemAttributes, $filename, $index)
);
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/UI/src/Traits/Fields/WithAdditionalFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
use MoonShine\Contracts\Core\DependencyInjection\FieldsContract;
use Throwable;

/**
* @template T of FieldsContract
*/
trait WithAdditionalFields
{
protected array $additionalFields = [];

/**
* @return T
* @throws Throwable
*/
public function getAdditionalFields(): FieldsContract
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Traits/Fields/WithBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getBadgeColor(mixed $value = null): string
{
$color = is_null($this->badgeColorCallback)
? $this->badgeColor
: value($this->badgeColorCallback, $value ?? $this->toValue(withDefault: false), $this);
: call_user_func($this->badgeColorCallback, $value ?? $this->toValue(withDefault: false), $this);


return $color instanceof Color ? $color->value : $color;
Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Traits/HasCanSee.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function isSee(): bool
$this,
];

return (bool) value(
return (bool) call_user_func(
$this->canSeeCallback,
...$params,
);
Expand Down
7 changes: 7 additions & 0 deletions src/UI/src/Traits/WithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
*/
trait WithFields
{
/**
* @param iterable|Closure(T $ctx): T $fields
*/
protected iterable|Closure $fields = [];

/**
* @var ?T $preparedFields
*/
protected ?FieldsContract $preparedFields = null;

public function resetPreparedFields(): static
Expand Down Expand Up @@ -95,6 +101,7 @@ public function fields(FieldsContract|Closure|iterable $fields): static
}

/**
* @param ?T $preparedFields
* @return T
* @throws Throwable
*/
Expand Down

0 comments on commit cd26d36

Please sign in to comment.