In Shopware lot's of PHPDoc Generics are used. One example would be a FieldCollection which extends from Collection
/**
* @template TElement
*
* @implements \IteratorAggregate<array-key, TElement>
*/
#[Package('core')]
abstract class Collection extends Struct implements \IteratorAggregate, \Countable
{
...
/**
* @param TElement $element
*/
public function add($element): void
{
$this->validateType($element);
$this->elements[] = $element;
}
...
}
/**
* @extends Collection<Field>
*/
#[Package('core')]
class FieldCollection extends Collection
{
...
}
When extending an entity you need to add fields to the the passed FieldCollection:
class SalesChannelProductExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new JsonField('base_calculated_price', 'baseCalculatedPrice'))->addFlags(new ApiAware(), new Runtime(['taxId', 'unitId', 'referenceUnit', 'purchaseUnit', 'price', 'prices'])),
);
}
public function getDefinitionClass(): string
{
return SalesChannelProductDefinition::class;
}
}
In $collection->add() I'm getting Argument '1' passed to add() is expected to be of type int|string, Shopware\Core\Framework\DataAbstractionLayer\Field\Field givenPHP(PHP0406)