It seems like you code against a concrete implementation and not the interface Drupal\Core\Entity\EntityInterface as the method getFields() does only exist in the following implementations of said interface: Drupal\Core\Entity\ContentEntityBase, Drupal\Core\Entity\EditorialContentEntityBase, Drupal\Core\Entity\RevisionableContentEntityBase, Drupal\Tests\Core\Entity\EntityManagerTestEntity, Drupal\aggregator\Entity\Feed, Drupal\aggregator\Entity\Item, Drupal\block_content\Entity\BlockContent, Drupal\comment\Entity\Comment, Drupal\contact\Entity\Message, Drupal\content_moderatio...\ContentModerationState, Drupal\content_translati...estTranslatableNoUISkip, Drupal\content_translati...yTestTranslatableUISkip, Drupal\entity_test\Entity\EntityTest, Drupal\entity_test\Entity\EntityTestAdminRoutes, Drupal\entity_test\Entit...ityTestBaseFieldDisplay, Drupal\entity_test\Entity\EntityTestCache, Drupal\entity_test\Entit...TestCompositeConstraint, Drupal\entity_test\Entity\EntityTestComputedField, Drupal\entity_test\Entit...TestConstraintViolation, Drupal\entity_test\Entity\EntityTestConstraints, Drupal\entity_test\Entity\EntityTestDefaultAccess, Drupal\entity_test\Entity\EntityTestDefaultValue, Drupal\entity_test\Entity\EntityTestFieldMethods, Drupal\entity_test\Entity\EntityTestFieldOverride, Drupal\entity_test\Entity\EntityTestLabel, Drupal\entity_test\Entity\EntityTestLabelCallback, Drupal\entity_test\Entity\EntityTestMul, Drupal\entity_test\Entity\EntityTestMulChanged, Drupal\entity_test\Entit...tityTestMulDefaultValue, Drupal\entity_test\Entity\EntityTestMulLangcodeKey, Drupal\entity_test\Entity\EntityTestMulRev, Drupal\entity_test\Entity\EntityTestMulRevChanged, Drupal\entity_test\Entit...vChangedWithRevisionLog, Drupal\entity_test\Entity\EntityTestMulRevPub, Drupal\entity_test\Entit...TestMultiValueBasefield, Drupal\entity_test\Entity\EntityTestNew, Drupal\entity_test\Entity\EntityTestNoBundle, Drupal\entity_test\Entity\EntityTestNoId, Drupal\entity_test\Entity\EntityTestNoLabel, Drupal\entity_test\Entity\EntityTestNoUuid, Drupal\entity_test\Entity\EntityTestRev, Drupal\entity_test\Entity\EntityTestStringId, Drupal\entity_test\Entity\EntityTestViewBuilder, Drupal\entity_test\Entity\EntityTestWithBundle, Drupal\entity_test_revlo...yTestMulWithRevisionLog, Drupal\entity_test_revlo...tityTestWithRevisionLog, Drupal\entity_test_update\Entity\EntityTestUpdate, Drupal\field_collection\Entity\FieldCollectionItem, Drupal\file\Entity\File, Drupal\language_test\Entity\NoLanguageEntityTest, Drupal\media\Entity\Media, Drupal\menu_link_content\Entity\MenuLinkContent, Drupal\migrate_entity_te...tity\StringIdEntityTest, Drupal\node\Entity\Node, Drupal\shortcut\Entity\Shortcut, Drupal\taxonomy\Entity\Term, Drupal\user\Entity\User.
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.
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: