From 9897377b46332e36e9c72a65122e8630dcd0a54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 15 Oct 2023 23:31:58 +0000 Subject: [PATCH 1/3] Prepare PHPStan --- .github/workflows/back-end.yml | 5 ++++- phpstan.neon.dist | 16 ++++++++++++++++ src/Actions/Actions.php | 3 +++ src/Columns/Columns.php | 3 +++ src/Fields/Fields.php | 3 +++ src/Filters/Filters.php | 3 +++ src/Models/Medium.php | 2 ++ src/Navigation/Item.php | 6 ++++++ src/Widgets/Widgets.php | 3 +++ 9 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.github/workflows/back-end.yml b/.github/workflows/back-end.yml index bbf0080db..e3f432763 100644 --- a/.github/workflows/back-end.yml +++ b/.github/workflows/back-end.yml @@ -135,9 +135,12 @@ jobs: name: "Search for $this->$this typo 🐌" run: | ! git grep --line-number -e '\$this\s*->\s*\$this' -- ':!:*/back-end\.yml' + - + name: "Install orchestra/testbench" + run: "composer require --dev orchestra/testbench" - name: "Perform static analysis" - run: "true TODO || composer exec -- phpstan analyze --level=5 src/" + run: "composer exec -- phpstan || true 'Annotate only'" coding_standards: name: "4️⃣ Coding Standards" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 96ad334f8..9787b6313 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -10,5 +10,21 @@ parameters: - database/ - routes/ level: 5 + checkAlwaysTrueCheckTypeFunctionCall: true + checkAlwaysTrueInstanceof: true + checkAlwaysTrueStrictComparison: true + checkAlwaysTrueLooseComparison: true + checkClassCaseSensitivity: false + checkDynamicProperties: true + checkExplicitMixed: false + checkImplicitMixed: false + checkExplicitMixedMissingReturn: true + checkFunctionNameCase: true + checkInternalClassCaseSensitivity: true ignoreErrors: - '#^Unsafe usage of new static#' + # Tricky readonlys + - '#Assign it in the constructor\.$#' + - '#is assigned outside of the constructor\.$#' + # View vs. View contract + - '#render\(\) should return Illuminate\\View\\View but#' diff --git a/src/Actions/Actions.php b/src/Actions/Actions.php index 297bf2e8b..51a552e29 100644 --- a/src/Actions/Actions.php +++ b/src/Actions/Actions.php @@ -7,6 +7,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\Traits\ForwardsCalls; +/** + * @mixin \Illuminate\Support\Collection + */ class Actions { use ForwardsCalls; diff --git a/src/Columns/Columns.php b/src/Columns/Columns.php index a33db1d79..fc44b7774 100644 --- a/src/Columns/Columns.php +++ b/src/Columns/Columns.php @@ -8,6 +8,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\Traits\ForwardsCalls; +/** + * @mixin \Illuminate\Support\Collection + */ class Columns { use ForwardsCalls; diff --git a/src/Fields/Fields.php b/src/Fields/Fields.php index fbb7e2a2f..2ec3d2baf 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -8,6 +8,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\Traits\ForwardsCalls; +/** + * @mixin \Illuminate\Support\Collection + */ class Fields { use ForwardsCalls; diff --git a/src/Filters/Filters.php b/src/Filters/Filters.php index b7f11d15d..a7dfae7b6 100644 --- a/src/Filters/Filters.php +++ b/src/Filters/Filters.php @@ -8,6 +8,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\Traits\ForwardsCalls; +/** + * @mixin \Illuminate\Support\Collection + */ class Filters { use ForwardsCalls; diff --git a/src/Models/Medium.php b/src/Models/Medium.php index f1f392db2..681b04ecb 100644 --- a/src/Models/Medium.php +++ b/src/Models/Medium.php @@ -152,6 +152,8 @@ public function user(): BelongsTo /** * Determine if the file is image. + * + * @return \Illuminate\Database\Eloquent\Casts\Attribute */ protected function isImage(): Attribute { diff --git a/src/Navigation/Item.php b/src/Navigation/Item.php index 76e68d8ab..3d83c9450 100644 --- a/src/Navigation/Item.php +++ b/src/Navigation/Item.php @@ -6,6 +6,12 @@ use Cone\Root\Traits\Makeable; use Illuminate\Support\Facades\URL; +/** + * @property string $url + * @property string $label + * @property string $icon + * @property string $group + */ class Item { use HasAttributes; diff --git a/src/Widgets/Widgets.php b/src/Widgets/Widgets.php index bff732c21..6bf2a6f49 100644 --- a/src/Widgets/Widgets.php +++ b/src/Widgets/Widgets.php @@ -6,6 +6,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\Traits\ForwardsCalls; +/** + * @mixin \Illuminate\Support\Collection + */ class Widgets { use ForwardsCalls; From 27eaa632174ff18ee5d7ef986fc716b4a7a7f4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Mon, 16 Oct 2023 00:23:04 +0000 Subject: [PATCH 2/3] Fix many problems --- phpstan.neon.dist | 10 +++++++--- src/Fields/BelongsTo.php | 4 ++++ src/Fields/BelongsToMany.php | 4 ++++ src/Fields/Boolean.php | 2 +- src/Fields/Fieldset.php | 10 +++++----- src/Fields/HasMany.php | 3 +++ src/Fields/HasOne.php | 3 +++ src/Fields/HasOneOrMany.php | 4 ++++ src/Fields/Meta.php | 3 +++ src/Fields/MorphMany.php | 3 +++ src/Fields/MorphOne.php | 4 ++++ src/Fields/MorphOneOrMany.php | 4 ++++ src/Fields/MorphTo.php | 3 +++ src/Fields/MorphToMany.php | 3 +++ src/Fields/Relation.php | 5 +++++ src/Resources/Resources.php | 3 +++ src/Support/Slug.php | 2 +- 17 files changed, 60 insertions(+), 10 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9787b6313..9312cf752 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -7,7 +7,8 @@ parameters: paths: - src/ - config/ - - database/ +# TODO +# - database/ - routes/ level: 5 checkAlwaysTrueCheckTypeFunctionCall: true @@ -23,8 +24,11 @@ parameters: checkInternalClassCaseSensitivity: true ignoreErrors: - '#^Unsafe usage of new static#' + # --- TODO-s --- # Tricky readonlys - '#Assign it in the constructor\.$#' - '#is assigned outside of the constructor\.$#' - # View vs. View contract - - '#render\(\) should return Illuminate\\View\\View but#' + # X vs. X contract + - '#but returns Illuminate\\Contracts\\#' + # SoftDeletes + - '#(\$forceDeleting|::withTrashed|::onlyTrashed|::trashed|::restore)#' diff --git a/src/Fields/BelongsTo.php b/src/Fields/BelongsTo.php index 4fd871e0e..b8b738525 100644 --- a/src/Fields/BelongsTo.php +++ b/src/Fields/BelongsTo.php @@ -6,6 +6,10 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo as EloquentRelation; use Illuminate\Http\Request; +/** + * @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsTo + * @extends \Cone\Root\Fields\Relation + */ class BelongsTo extends Relation { /** diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index a87749eb6..0da2da62d 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -9,6 +9,10 @@ use Illuminate\Http\Request; use Illuminate\Support\Arr; +/** + * @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @extends \Cone\Root\Fields\Relation + */ class BelongsToMany extends Relation { use ResolvesFields; diff --git a/src/Fields/Boolean.php b/src/Fields/Boolean.php index 7fe4b5e4a..91ceac618 100644 --- a/src/Fields/Boolean.php +++ b/src/Fields/Boolean.php @@ -27,7 +27,7 @@ public function __construct(string $label, string $modelAttribute = null) */ public function getValueForHydrate(Request $request): mixed { - return $request->boolean([$this->getRequestKey()]); + return $request->boolean($this->getRequestKey()); } /** diff --git a/src/Fields/Fieldset.php b/src/Fields/Fieldset.php index 694be62d7..141d9cf39 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -31,8 +31,8 @@ protected function resolveField(Request $request, Field $field): void */ public function persist(Request $request, Model $model, mixed $value): void { - $this->resolveFields($request)->each(static function (Field $field) use ($request): void { - $field->persist($request, $field->getValueForHydrate($request)); + $this->resolveFields($request)->each(static function (Field $field) use ($request, $model): void { + $field->persist($request, $model, $field->getValueForHydrate($request)); }); } @@ -41,8 +41,8 @@ public function persist(Request $request, Model $model, mixed $value): void */ public function resolveHydrate(Request $request, Model $model, mixed $value): void { - $this->resolveFields($request)->each(static function (Field $field) use ($request): void { - $field->resolveHydrate($request, $field->getValueForHydrate($request)); + $this->resolveFields($request)->each(static function (Field $field) use ($request, $model): void { + $field->resolveHydrate($request, $model, $field->getValueForHydrate($request)); }); } @@ -77,7 +77,7 @@ public function toValidate(Request $request, Model $model): array { return array_merge( parent::toValidate($request, $model), - $this->resolveFields($request)->mapToValidate($request) + $this->resolveFields($request)->mapToValidate($request, $model) ); } } diff --git a/src/Fields/HasMany.php b/src/Fields/HasMany.php index 1ff452803..8ac39e6a3 100644 --- a/src/Fields/HasMany.php +++ b/src/Fields/HasMany.php @@ -6,6 +6,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany as EloquentRelation; +/** + * @extends \Cone\Root\Fields\HasOneOrMany<\Illuminate\Database\Eloquent\Relations\HasMany> + */ class HasMany extends HasOneOrMany { /** diff --git a/src/Fields/HasOne.php b/src/Fields/HasOne.php index cd071702f..e6bda9b29 100644 --- a/src/Fields/HasOne.php +++ b/src/Fields/HasOne.php @@ -5,6 +5,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne as EloquentRelation; +/** + * @extends \Cone\Root\Fields\HasOneOrMany<\Illuminate\Database\Eloquent\Relations\HasOne> + */ class HasOne extends HasOneOrMany { /** diff --git a/src/Fields/HasOneOrMany.php b/src/Fields/HasOneOrMany.php index 01a6c894a..478082d7c 100644 --- a/src/Fields/HasOneOrMany.php +++ b/src/Fields/HasOneOrMany.php @@ -7,6 +7,10 @@ use Illuminate\Http\Request; use Illuminate\Support\Arr; +/** + * @template TRelation of \Illuminate\Database\Eloquent\Relations\HasOneOrMany + * @extends \Cone\Root\Fields\Relation + */ abstract class HasOneOrMany extends Relation { /** diff --git a/src/Fields/Meta.php b/src/Fields/Meta.php index 37e72eecf..83339a9a4 100644 --- a/src/Fields/Meta.php +++ b/src/Fields/Meta.php @@ -9,6 +9,9 @@ use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation; use Illuminate\Http\Request; +/** + * @extends \Cone\Root\Fields\MorphOne<\Illuminate\Database\Eloquent\Relations\MorphOne> + */ class Meta extends MorphOne { /** diff --git a/src/Fields/MorphMany.php b/src/Fields/MorphMany.php index 2fa0385e2..7da69d4e3 100644 --- a/src/Fields/MorphMany.php +++ b/src/Fields/MorphMany.php @@ -5,6 +5,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentRelation; +/** + * @extends \Cone\Root\Fields\MorphOneOrMany<\Illuminate\Database\Eloquent\Relations\MorphMany> + */ class MorphMany extends MorphOneOrMany { /** diff --git a/src/Fields/MorphOne.php b/src/Fields/MorphOne.php index d17f7ace5..dfb4babda 100644 --- a/src/Fields/MorphOne.php +++ b/src/Fields/MorphOne.php @@ -5,6 +5,10 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation; +/** + * @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOne + * @extends \Cone\Root\Fields\MorphOneOrMany + */ class MorphOne extends MorphOneOrMany { /** diff --git a/src/Fields/MorphOneOrMany.php b/src/Fields/MorphOneOrMany.php index ab928ed59..77a1290ed 100644 --- a/src/Fields/MorphOneOrMany.php +++ b/src/Fields/MorphOneOrMany.php @@ -5,6 +5,10 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany as EloquentRelation; +/** + * @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOneOrMany + * @extends \Cone\Root\Fields\HasOneOrMany + */ abstract class MorphOneOrMany extends HasOneOrMany { /** diff --git a/src/Fields/MorphTo.php b/src/Fields/MorphTo.php index 36a155e6d..afb094bb9 100644 --- a/src/Fields/MorphTo.php +++ b/src/Fields/MorphTo.php @@ -5,6 +5,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentRelation; +/** + * @extends \Cone\Root\Fields\BelongsTo<\Illuminate\Database\Eloquent\Relations\MorphTo> + */ class MorphTo extends BelongsTo { /** diff --git a/src/Fields/MorphToMany.php b/src/Fields/MorphToMany.php index 8e3581438..5c98ffa25 100644 --- a/src/Fields/MorphToMany.php +++ b/src/Fields/MorphToMany.php @@ -5,6 +5,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphToMany as EloquentRelation; +/** + * @extends \Cone\Root\Fields\BelongsToMany<\Illuminate\Database\Eloquent\Relations\MorphToMany> + */ class MorphToMany extends BelongsToMany { /** diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index 3bea00693..fdd80a23a 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -10,6 +10,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; +/** + * @template TRelation of \Illuminate\Database\Eloquent\Relations\Relation + */ abstract class Relation extends Field { /** @@ -72,6 +75,8 @@ public static function scopeQuery(Closure $callback): void /** * Get the relation instance. + * + * @phpstan-return TRelation */ public function getRelation(Model $model): EloquentRelation { diff --git a/src/Resources/Resources.php b/src/Resources/Resources.php index c62d96063..fb50cb44c 100644 --- a/src/Resources/Resources.php +++ b/src/Resources/Resources.php @@ -9,6 +9,9 @@ use Illuminate\Support\Traits\ForwardsCalls; use Throwable; +/** + * @mixin \Illuminate\Support\Collection + */ class Resources { use ForwardsCalls; diff --git a/src/Support/Slug.php b/src/Support/Slug.php index 6768d4010..cc3be40ac 100644 --- a/src/Support/Slug.php +++ b/src/Support/Slug.php @@ -152,7 +152,7 @@ static function (Builder $query): Builder { $value = is_null($match) ? $value : preg_replace_callback( sprintf('/%s([\d]+)?$/', preg_quote($this->separator)), static function (array $match): string { - return str_replace($match[1], ((int) $match[1]) + 1, $match[0]); + return str_replace($match[1], (string)(((int) $match[1]) + 1), $match[0]); }, $match ); From 277d790f2cb8024253c4308e16af342160f6971c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Mon, 16 Oct 2023 00:26:19 +0000 Subject: [PATCH 3/3] Fix CS --- src/Fields/BelongsTo.php | 1 + src/Fields/BelongsToMany.php | 1 + src/Fields/HasOneOrMany.php | 1 + src/Fields/MorphOne.php | 1 + src/Fields/MorphOneOrMany.php | 1 + src/Support/Slug.php | 2 +- 6 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Fields/BelongsTo.php b/src/Fields/BelongsTo.php index b8b738525..c247595eb 100644 --- a/src/Fields/BelongsTo.php +++ b/src/Fields/BelongsTo.php @@ -8,6 +8,7 @@ /** * @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsTo + * * @extends \Cone\Root\Fields\Relation */ class BelongsTo extends Relation diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 0da2da62d..a3fbf2e27 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -11,6 +11,7 @@ /** * @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsToMany + * * @extends \Cone\Root\Fields\Relation */ class BelongsToMany extends Relation diff --git a/src/Fields/HasOneOrMany.php b/src/Fields/HasOneOrMany.php index 478082d7c..f58d318b1 100644 --- a/src/Fields/HasOneOrMany.php +++ b/src/Fields/HasOneOrMany.php @@ -9,6 +9,7 @@ /** * @template TRelation of \Illuminate\Database\Eloquent\Relations\HasOneOrMany + * * @extends \Cone\Root\Fields\Relation */ abstract class HasOneOrMany extends Relation diff --git a/src/Fields/MorphOne.php b/src/Fields/MorphOne.php index dfb4babda..e2776f772 100644 --- a/src/Fields/MorphOne.php +++ b/src/Fields/MorphOne.php @@ -7,6 +7,7 @@ /** * @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOne + * * @extends \Cone\Root\Fields\MorphOneOrMany */ class MorphOne extends MorphOneOrMany diff --git a/src/Fields/MorphOneOrMany.php b/src/Fields/MorphOneOrMany.php index 77a1290ed..9ad8d3c48 100644 --- a/src/Fields/MorphOneOrMany.php +++ b/src/Fields/MorphOneOrMany.php @@ -7,6 +7,7 @@ /** * @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOneOrMany + * * @extends \Cone\Root\Fields\HasOneOrMany */ abstract class MorphOneOrMany extends HasOneOrMany diff --git a/src/Support/Slug.php b/src/Support/Slug.php index cc3be40ac..7e9822ba3 100644 --- a/src/Support/Slug.php +++ b/src/Support/Slug.php @@ -152,7 +152,7 @@ static function (Builder $query): Builder { $value = is_null($match) ? $value : preg_replace_callback( sprintf('/%s([\d]+)?$/', preg_quote($this->separator)), static function (array $match): string { - return str_replace($match[1], (string)(((int) $match[1]) + 1), $match[0]); + return str_replace($match[1], (string) (((int) $match[1]) + 1), $match[0]); }, $match );