Skip to content

Product Attributes are not typed correctly #39886

Open
@gwharton

Description

@gwharton

Preconditions and environment

Tested on

  • 2.4.8
  • 2.4.7
  • PHP 8.2
  • PHP 8.3

Steps to reproduce

  • Deploy vanilla magento and install sample data.
  • Run the following test code in the root which demonstrates the problem by getting the status of the product (enabled/disabled) and printing the return type of the getStatus() call.
use Magento\Framework\App\Bootstrap;
use Magento\Catalog\Api\ProductRepositoryInterface;

require __DIR__ . '/app/bootstrap.php';

$productRepository = Bootstrap::create(BP, $_SERVER)
    ->getObjectManager()
    ->create(ProductRepositoryInterface::class);

$product = $productRepository->get("24-MB01");
$status = $product->getStatus();
echo "Return type of getStatus() is : " . gettype($status) . PHP_EOL;

return;

Expected result

Return type of getStatus() is : int

Actual result

Return type of getStatus() is : string

Additional information

The Status Product Attribute is defined in core Magento as an integer.

When products are created, the Attribute "status" is stored in the catalog_product_entity_int table confirming it is typed as integer.

The Magento ProductInterface defines the call getStatus() as returning an int or null.

/**
* Product status
*
* @return int|null
*/
public function getStatus();

Yet calls to $product->getStatus() return a string.

The same thing happens when retrieving float values, for example $product->getPrice() returns a string despite the Interface mandating float or null.

/**
* Product price
*
* @return float|null
*/
public function getPrice();

EDIT : I'm willing to concede on getPrice as Price is stored as a DECIMAL type in MySQL which is a fixed precision string representation of a floating point number, and not a float.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue: ready for confirmationReported on 2.4.8Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    Status

    Ready for Confirmation

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions