Preserve the scalar type of an extra property default value in the CQRS layer by ousamabenyounes · Pull Request #42343 · PrestaShop/PrestaShop · GitHub
Skip to content

Preserve the scalar type of an extra property default value in the CQRS layer - #42343

Closed
ousamabenyounes wants to merge 1 commit into
PrestaShop:developfrom
ousamabenyounes:fix/issue-41829
Closed

ousamabenyounes wants to merge 1 commit into
PrestaShop:developfrom
ousamabenyounes:fix/issue-41829

Conversation

@ousamabenyounes

Copy link
Copy Markdown
Questions Answers
Branch? develop
Description? See below.
Type? refacto
Category? CO
BC breaks? no
Deprecations? no
How to test? See "How to test" below. Unit tests only — no DB/UI surface changed.
UI Tests N/A — no UI behaviour changed; covered by unit tests.
Fixed issue or discussion? Fixes #41829
Related PRs None.
Sponsor company N/A

Description

ExtraPropertyDefinition types its $defaultValue as int|float|string|bool|null (docblock @param scalar|null), and the repository already casts the raw DB string back to that declared type via ExtraPropertyValueCaster::castFromDb(). But the CQRS layer narrowed the same value to ?string:

  • AddExtraPropertyDefinitionCommand::$defaultValue was ?string, so a non-string scalar passed programmatically (e.g. an integer default coming from the Admin API JSON payload) raised a TypeError under declare(strict_types=1).
  • EditableExtraPropertyDefinition::$defaultValue was ?string, and GetExtraPropertyDefinitionForEditingHandler forced a (string) cast when building it — losing the scalar type carried by the entity.

This PR aligns the CQRS layer with the entity:

  • Widen $defaultValue (constructor param + getter) to int|float|string|bool|null in AddExtraPropertyDefinitionCommand and EditableExtraPropertyDefinition.
  • Drop the lossy (string) cast in GetExtraPropertyDefinitionForEditingHandler so the edit DTO carries the real scalar.
  • Move that (string) cast to ExtraPropertyDefinitionFormDataProvider, where the back-office TextType field needs a string for display. The rendered form value is therefore unchanged — the cast simply happens at the form boundary instead of inside the query handler.

No BC break: string defaults continue to work everywhere (string is part of the widened union), and the only in-repo command caller (the BO form data handler) still passes a string.

How to test

Unit tests cover the type-preservation contract on both CQRS classes:

vendor/bin/phpunit -c tests/Unit/phpunit.xml \
  tests/Unit/Core/Domain/ExtraProperty/Command/AddExtraPropertyDefinitionCommandTest.php \
  tests/Unit/Core/Domain/ExtraProperty/QueryResult/EditableExtraPropertyDefinitionTest.php

Each test constructs the class with int, float, bool, string and null defaults and asserts (assertSame) that the getter returns the same typed value.

Test verification (RED → GREEN)

RED — new tests on develop (before the production change): the int/float/bool data sets fail because the constructor still declares ?string:

TypeError: AddExtraPropertyDefinitionCommand::__construct(): Argument #10 ($defaultValue)
must be of type ?string, int given
TypeError: EditableExtraPropertyDefinition::__construct(): Argument #10 ($defaultValue)
must be of type ?string, float given
...
Tests: 12, Assertions: 4, Errors: 8.

GREEN — after widening the types (full ExtraProperty unit suites, includes the 12 new cases):

PHPUnit 10.5.63
Tests: 491, Assertions: 1356.
OK

php-cs-fixer (dry-run) and phpstan on the changed files both report no errors.

@ousamabenyounes
ousamabenyounes requested a review from a team as a code owner August 10, 2026 16:05
@github-project-automation github-project-automation Bot moved this to Ready for review in PR Dashboard Aug 10, 2026
@ps-jarvis ps-jarvis added develop Branch Refactoring Type: Refactoring labels Aug 10, 2026
@ps-jarvis

Copy link
Copy Markdown

Hello @ousamabenyounes!

This is your first pull request on PrestaShop repository of the PrestaShop project.

Thank you, and welcome to this Open Source community!

@github-actions github-actions Bot added this to the 9.3.0 milestone Aug 10, 2026
…RS layer

ExtraPropertyDefinition types its $defaultValue as int|float|string|bool|null
so a value read back from an INT/BOOL/FLOAT column keeps its declared type. The
add command and the edit query-result DTO narrowed it to ?string, forcing a
lossy (string) cast and rejecting non-string scalars passed through the CQRS
layer (e.g. an integer default coming from the Admin API JSON payload, which
raised a TypeError under strict_types).

Widen AddExtraPropertyDefinitionCommand and EditableExtraPropertyDefinition to
the same scalar union, drop the (string) cast in the editing query handler, and
move that cast to the form data provider where the TextType field needs a string
for display. Back-office form rendering is therefore unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jolelievre

Copy link
Copy Markdown
Contributor

@jolelievre jolelievre closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

develop Branch Refactoring Type: Refactoring

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[ExtraProperties] Default value correct type

3 participants