It seems like you code against a concrete implementation and not the interface Drupal\Core\TypedData\Tr...sableTypedDataInterface as the method getSettings() does only exist in the following implementations of said interface: Drupal\Core\Field\ChangedFieldItemList, Drupal\Core\Field\EntityReferenceFieldItemList, Drupal\Core\Field\FieldItemBase, Drupal\Core\Field\FieldItemList, Drupal\Core\Field\MapFieldItemList, Drupal\Core\Field\Plugin...d\FieldType\BooleanItem, Drupal\Core\Field\Plugin...d\FieldType\ChangedItem, Drupal\Core\Field\Plugin...d\FieldType\CreatedItem, Drupal\Core\Field\Plugin...d\FieldType\DecimalItem, Drupal\Core\Field\Plugin\Field\FieldType\EmailItem, Drupal\Core\Field\Plugin...ype\EntityReferenceItem, Drupal\Core\Field\Plugin\Field\FieldType\FloatItem, Drupal\Core\Field\Plugin...d\FieldType\IntegerItem, Drupal\Core\Field\Plugin...\FieldType\LanguageItem, Drupal\Core\Field\Plugin\Field\FieldType\MapItem, Drupal\Core\Field\Plugin...eldType\NumericItemBase, Drupal\Core\Field\Plugin...\FieldType\PasswordItem, Drupal\Core\Field\Plugin...ld\FieldType\StringItem, Drupal\Core\Field\Plugin...ieldType\StringItemBase, Drupal\Core\Field\Plugin...ieldType\StringLongItem, Drupal\Core\Field\Plugin...FieldType\TimestampItem, Drupal\Core\Field\Plugin\Field\FieldType\UriItem, Drupal\Core\Field\Plugin\Field\FieldType\UuidItem, Drupal\color_field\Plugi...ieldType\ColorFieldType, Drupal\comment\CommentFieldItemList, Drupal\comment\Plugin\Field\FieldType\CommentItem, Drupal\content_moderatio...ationStateFieldItemList, Drupal\datetime\Plugin\F...e\DateTimeFieldItemList, Drupal\datetime\Plugin\F...\FieldType\DateTimeItem, Drupal\datetime_range\Pl...\DateRangeFieldItemList, Drupal\datetime_range\Pl...FieldType\DateRangeItem, Drupal\entity_reference\...ableEntityReferenceItem, Drupal\entity_test\Plugi...mputedTestFieldItemList, Drupal\entity_test\Plugi...utoIncrementingTestItem, Drupal\entity_test\Plugi...eldType\ChangedTestItem, Drupal\entity_test\Plugi...FieldType\FieldTestItem, Drupal\entity_test\Plugi...alPropertyTestFieldItem, Drupal\entity_test\Plugi...eld\FieldType\ShapeItem, Drupal\entity_test\Plugi...dType\ShapeItemRequired, Drupal\entity_test_updat...Type\MultiValueTestItem, Drupal\field_collection\...eldType\FieldCollection, Drupal\field_test\Plugin...ieldType\HiddenTestItem, Drupal\field_test\Plugin\Field\FieldType\TestItem, Drupal\field_test\Plugin...estItemWithDependencies, Drupal\field_test\Plugin...ithPreconfiguredOptions, Drupal\field_test\Plugin...ieldType\TestObjectItem, Drupal\file\Plugin\Field...dType\FileFieldItemList, Drupal\file\Plugin\Field\FieldType\FileItem, Drupal\file\Plugin\Field\FieldType\FileUriItem, Drupal\image\Plugin\Field\FieldType\ImageItem, Drupal\language\DefaultLanguageItem, Drupal\layout_builder\Field\LayoutSectionItemList, Drupal\layout_builder\Pl...dType\LayoutSectionItem, Drupal\link\Plugin\Field\FieldType\LinkItem, Drupal\options\Plugin\Fi...FieldType\ListFloatItem, Drupal\options\Plugin\Fi...eldType\ListIntegerItem, Drupal\options\Plugin\Field\FieldType\ListItemBase, Drupal\options\Plugin\Fi...ieldType\ListStringItem, Drupal\path\Plugin\Field...dType\PathFieldItemList, Drupal\path\Plugin\Field\FieldType\PathItem, Drupal\telephone\Plugin\...FieldType\TelephoneItem, Drupal\text\Plugin\Field\FieldType\TextItem, Drupal\text\Plugin\Field\FieldType\TextItemBase, Drupal\text\Plugin\Field\FieldType\TextLongItem, Drupal\text\Plugin\Field...ype\TextWithSummaryItem, Drupal\user\StatusItem, Drupal\user\TimeZoneItem, Drupal\user\UserNameItem.
Let’s take a look at an example:
interfaceUser{/** @return string */publicfunctiongetPassword();}classMyUserimplementsUser{publicfunctiongetPassword(){// return something}publicfunctiongetDisplayName(){// return some name.}}classAuthSystem{publicfunctionauthenticate(User$user){$this->logger->info(sprintf('Authenticating %s.',$user->getDisplayName()));// do something.}}
In the above example, the authenticate() method works fine as long as you just pass
instances of MyUser. However, if you now also want to pass a different implementation
of User which does not have a getDisplayName() method, the code will break.
classAuthSystem{publicfunctionauthenticate(User$user){if($userinstanceofMyUser){$this->logger->info(/** ... */);}// or alternativelyif(!$userinstanceofMyUser){thrownew\LogicException('$user must be an instance of MyUser, '.'other instances are not supported.');}}}
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
It seems like $node defined by $this->entityManager->ge...etValue()['target_id']) on line 44 can be null; however, Drupal\easy_entity_reader\EntityWrapper::wrap() does not accept null, maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of
other conditions, we strongly recommend to add an additional type check to your
code:
/** @return stdClass|null */functionmayReturnNull(){}functiondoesNotAcceptNull(stdClass$x){}// With potential error.functionwithoutCheck(){$x=mayReturnNull();doesNotAcceptNull($x);// Potential error here.}// Safe - Alternative 1functionwithCheck1(){$x=mayReturnNull();if(!$xinstanceofstdClass){thrownew\LogicException('$x must be defined.');}doesNotAcceptNull($x);}// Safe - Alternative 2functionwithCheck2(){$x=mayReturnNull();if($xinstanceofstdClass){doesNotAcceptNull($x);}}
Loading history...
47
}
48
49
/**
50
* Returns whether the FieldItemInterface can be converted or not.
51
*
52
* @param FieldItemInterface $value
53
*
54
* @return bool
55
*/
56
public function canConvert(FieldItemInterface $value) : bool
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: