Field item stubs should allow null for stored properties
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 84/100
Research direction
Start with the affected field item stubs and matching assertions in tests/src/Type/data/field-types.php, using the table and core behavior described in the issue. Update each listed property to its nullable type and include |null in the matching assertions. Done when all listed cases are reflected in the stubs and assertions.
Written by the indexing model from the issue text.
Description
Several field item stubs type stored properties as non-nullable, but core expects them to be NULL sometimes. StringItemBase is the clearest case:
/**
* @property string $value
*/
abstract class StringItemBase extends FieldItemBase {
}
// Core's StringItemBase::isEmpty()
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
An item from appendItem(), or one on an unsaved entity, returns NULL from ->value. setRequired(TRUE) on a property definition only adds a validation constraint, and none of these columns are 'not null' in the schema. With the current stubs, PHPStan reports $item->value ?? '' as unnecessary, and $item->value = NULL; fails with "does not accept null."
The PathItem stub from #1007 already uses nullable types. Found while reviewing #1073, which adds a TelephoneItem stub.
Affected stubs
| Stub | Property | Change to | Evidence in core |
|---|---|---|---|
StringItemBase |
value |
?string |
isEmpty() checks === NULL. Also covers StringItem, StringLongItem, UriItem, UuidItem, PasswordItem. |
EmailItem |
value |
?string |
isEmpty() checks === NULL. |
DecimalItem |
value |
?string |
NumericItemBase::isEmpty() uses empty($value). |
IntegerItem |
value |
?int |
Same as DecimalItem. |
FloatItem |
value |
?float |
Same as DecimalItem. |
BooleanItem |
value |
?int |
No isEmpty() override. Map::isEmpty() treats NULL as empty. |
TimestampItem |
value |
?int |
Same as BooleanItem. |
LanguageItem |
value |
?string |
Same as BooleanItem. |
LinkItem |
uri |
?string |
isEmpty() checks uri === NULL. |
LinkItem |
title |
?string |
Not required. getTitle() returns $this->title ?: NULL. |
FileItem |
description |
?string |
Not required. Only set when the description field is enabled. |
FileItem |
display |
?bool |
Not required. Only set when the display field is enabled. |
Not affected
FileUriItem::$urlis computed.ComputedFileUrl::getValue()always returns a string.TextItemBase::$processedandTextWithSummaryItem::$summary_processedare computed and read-only.
Needs a closer look
These probably can be NULL too, but I didn't confirm it:
LanguageItem::$languageis a computed reference built fromvalue. WhenvalueisNULL, there is no language to load.LinkItem::$optionsis aMap.Map::$valueshas no default, so an item with no options set may returnNULLinstead of[].PasswordItem::$pre_hashedis a boolean property with no default value.MapItem::$valuehas its own storage handling.
To do
- Update each stub in the table above to the nullable type.
- Update the matching assertions in
tests/src/Type/data/field-types.phpto include|null.
- Dominant language
- PHP
- Stars
- 208
- Forks
- 88
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 15
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mglaman/phpstan-drupal
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
mglaman/phpstan-drupal#1079 ·
Maintainers usually reply within 1 day
-
Report constraint plugins that break on Drupal 12 because they have no named-arguments constructorPossibly taken @goba claimed this 1 day ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
mglaman/phpstan-drupal#1075 ·
Maintainers usually reply within 1 day
-
Magic properties: original on entities, name on UserSession and password on User are not reported as deprecatedPossibly taken @goba claimed this 1 day ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
mglaman/phpstan-drupal#1074 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 25/100
mglaman/phpstan-drupal#1051 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
mglaman/phpstan-drupal#1047 ·
Maintainers usually reply within 1 day
All issues in mglaman/phpstan-drupal
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
getgrav/grav-plugin-sitemap#125 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
nikic/PHP-Parser#1184 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
api-platform/core#8835 ·
Maintainers usually reply within 2 days
-
14.3.x
Difficulty 1/5 1-3 hours Newbie friendliness 82/100