Fix PHPStan L2 error "Cannot call method Foo() on Bar."

Created on 24 November 2022, almost 3 years ago
Updated 13 November 2023, almost 2 years ago

Problem/Motivation

Amongst the "new" errors found when running PHPStan on level 2 is: Cannot call method Foo() on Bar.

This child-issue exists to fix all of those.

Steps to reproduce

- Run PHPStan on level 2 and see the above error amongst all others.

Proposed resolution

- Solve all of the the above mentioned reported errors.
- Run PHPStan on level 2 and don't see the above mentioned error any more.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ“Œ Task
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Baseย  โ†’

Last updated about 14 hours ago

Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Updated steps to reproduce. There are currently 181 of these to fix.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    I think we should split this in to errors related to prophecy and errors not related to prophecy. Let's keep this one for not related, I've opened ๐Ÿ“Œ Fix PHPStan L2 error "Cannot call method Foo() on Bar" related to Prophecy Active for related. Note that jangregor/phpstan-prophecy doesn't resolve all of them, hence the need for another issue.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    Hi @mstrelan,
    Before I start working on the issue, I wanted to double-check something. I ran the following command:

    ./vendor/bin/phpstan analyse -c core/phpstan.neon.dist --error-format=raw --level=2 | sed "s|$PWD/||" | grep -P 'Cannot call method (?!(expects|shouldBeCalled|shouldNotBeCalled|shouldHaveBeenCalled|will|willReturn|willThrow)\(\))'

    I got below errors.

    From my understanding, Prophecy is used to:

    1. say what methods should be called (shouldBeCalled())
    2. say what values they should return (willReturn())
    3. verify interactions (shouldHaveBeenCalled())

    Based on the errors below, I donโ€™t believe they are related to Prophecy issues.
    Could you please confirm?

    core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php:269:Cannot call method get() on Drupal\Core\Entity\EntityInterface|false.
    core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php:444:Cannot call method get() on Drupal\Core\Entity\EntityInterface|false.
    core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php:461:Cannot call method get() on Drupal\Core\Entity\EntityInterface|false.
    core/lib/Drupal/Core/Entity/EntityAutocompleteMatcher.php:47:Cannot call method getReferenceableEntities() on object|false.
    core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php:358:Cannot call method getReferenceableEntities() on object|false.
    core/lib/Drupal/Core/File/FileSystem.php:274:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/lib/Drupal/Core/Updater/Updater.php:414:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/block/src/BlockAccessControlHandler.php:148:Cannot call method addCacheableDependency() on bool|Drupal\Core\Access\AccessResultInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php:58:Cannot call method getCKEditor5Config() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php:116:Cannot call method getElements() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php:118:Cannot call method getCKEditor5Config() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php:101:Cannot call method getCKEditor5Config() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php:205:Cannot call method getElements() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php:207:Cannot call method getCKEditor5Config() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/ListPlugin.php:91:Cannot call method getElements() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php:196:Cannot call method getElements() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php:477:Cannot call method hasConditions() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php:478:Cannot call method getConditions() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/config/tests/src/Functional/ConfigInstallProfileOverrideTest.php:135:Cannot call method label() on null.
    core/modules/config/tests/src/Functional/ConfigInstallProfileOverrideTest.php:137:Cannot call method label() on null.
    core/modules/config/tests/src/Functional/ConfigInstallProfileOverrideTest.php:138:Cannot call method delete() on null.
    core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php:87:Cannot call method label() on null.
    core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php:88:Cannot call method id() on null.
    core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php:89:Cannot call method status() on null.
    core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php:195:Cannot call method count() on null.
    core/modules/content_translation/src/ContentTranslationHandler.php:682:Cannot call method getAccountName() on null.
    core/modules/field/src/Entity/FieldConfig.php:242:Cannot call method isSyncing() on Drupal\Core\Entity\EntityInterface|false.
    core/modules/field/tests/src/Functional/FormTest.php:565:Cannot call method setDefaultValue() on array.
    core/modules/field/tests/src/Functional/FormTest.php:566:Cannot call method save() on array.
    core/modules/field/tests/src/Functional/FormTest.php:568:Cannot call method getTargetBundle() on array.
    core/modules/field/tests/src/Functional/FormTest.php:569:Cannot call method getName() on array.
    core/modules/field/tests/src/Functional/FormTest.php:588:Cannot call method getTargetBundle() on array.
    core/modules/field/tests/src/Functional/FormTest.php:589:Cannot call method getName() on array.
    core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php:152:Cannot call method label() on array<string>.
    core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php:153:Cannot call method label() on array<string>.
    core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php:154:Cannot call method label() on array<string>.
    core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php:171:Cannot call method bundle() on array<string>.
    core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php:172:Cannot call method label() on array<string>.
    core/modules/field/tests/src/Kernel/FieldImportCreateTest.php:55:Cannot call method getTargetBundle() on null.
    core/modules/field/tests/src/Kernel/FieldImportCreateTest.php:57:Cannot call method getTargetBundle() on null.
    core/modules/file/tests/file_test/src/Hook/FileTestHooks.php:114:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/file/tests/file_test/src/Hook/FileTestHooks.php:140:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/file/tests/file_test/src/Hook/FileTestHooks.php:159:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/file/tests/src/Functional/DownloadTest.php:58:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/file/tests/src/Functional/DownloadTest.php:172:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php:121:Cannot call method getFileUri() on null.
    core/modules/image/src/Entity/ImageStyle.php:257:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/image/src/PathProcessor/PathProcessorImageStyles.php:47:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/image/src/Routing/ImageStyleRoutes.php:53:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/jsonapi/src/Controller/EntityResource.php:290:Cannot call method save() on array.
    core/modules/jsonapi/src/ResourceType/ResourceType.php:162:Cannot call method getPublicName() on string.
    core/modules/jsonapi/src/ResourceType/ResourceType.php:255:Cannot call method isFieldEnabled() on string.
    core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Plugin/Field/FieldType/EntityReferenceUuidItem.php:220:Cannot call method getReferenceableEntities() on object|false.
    core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php:387:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php:91:Cannot call method withCacheableDependency() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php:143:Cannot call method getCacheMaxAge() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php:157:Cannot call method getCacheMaxAge() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php:165:Cannot call method getCacheMaxAge() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php:173:Cannot call method getCacheMaxAge() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:343:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:400:Cannot call method getCacheTags() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:404:Cannot call method getCacheMaxAge() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:432:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:445:Cannot call method getCacheTags() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:453:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:503:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:557:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:563:Cannot call method getCacheTags() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:816:Cannot call method getCacheTags() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:819:Cannot call method getCacheContexts() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:821:Cannot call method getCacheMaxAge() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:879:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:902:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php:927:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Unit/Normalizer/HttpExceptionNormalizerTest.php:44:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/jsonapi/tests/src/Unit/Normalizer/HttpExceptionNormalizerTest.php:59:Cannot call method getNormalization() on array|ArrayObject|bool|float|int|string.
    core/modules/language/src/Entity/ConfigurableLanguage.php:197:Cannot call method isSyncing() on Drupal\Core\Entity\EntityInterface|false.
    core/modules/language/src/Entity/ConfigurableLanguage.php:197:Cannot call method isUninstalling() on Drupal\Core\Entity\EntityInterface|false.
    core/modules/layout_builder/src/Controller/ChooseSectionController.php:74:Cannot call method getIcon() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/layout_builder/src/Controller/ChooseSectionController.php:77:Cannot call method getLabel() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php:84:Cannot call method addContextDefinition() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php:174:Cannot call method willReturnArgument() on array.
    core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php:243:Cannot call method willReturnArgument() on array.
    core/modules/link/tests/src/Kernel/LinkItemTest.php:165:Cannot call method getUrl() on string.
    core/modules/link/tests/src/Kernel/LinkItemTest.php:170:Cannot call method getValue() on null.
    core/modules/media_library/tests/src/Functional/MediaLibraryDisplayModeTest.php:196:Cannot call method delete() on null.
    core/modules/media_library/tests/src/Functional/MediaLibraryDisplayModeTest.php:197:Cannot call method delete() on null.
    core/modules/mysql/tests/src/Unit/InstallTasksTest.php:121:Cannot call method shouldBeCalledTimes() on bool.
    core/modules/node/src/NodeAccessControlHandler.php:103:Cannot call method cachePerPermissions() on bool|Drupal\Core\Access\AccessResultInterface.
    core/modules/node/src/NodeAccessControlHandler.php:123:Cannot call method cachePerPermissions() on bool|Drupal\Core\Access\AccessResultInterface.
    core/modules/search/src/Plugin/views/argument/Search.php:108:Cannot call method conditions() on array.
    core/modules/search/src/Plugin/views/filter/Search.php:182:Cannot call method conditions() on array.
    core/modules/search/src/SearchQuery.php:325:Cannot call method condition() on array<Drupal\Core\Database\Query\ConditionInterface>.
    core/modules/search/src/SearchQuery.php:334:Cannot call method condition() on array<Drupal\Core\Database\Query\ConditionInterface>.
    core/modules/search/src/SearchQuery.php:348:Cannot call method condition() on array<Drupal\Core\Database\Query\ConditionInterface>.
    core/modules/system/src/Routing/AssetRoutes.php:45:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/modules/system/tests/src/Functional/FileTransfer/TestFileTransfer.php:66:Cannot call method run() on object|false.
    core/modules/system/tests/src/Functional/FileTransfer/TestFileTransfer.php:74:Cannot call method run() on object|false.
    core/modules/system/tests/src/Functional/FileTransfer/TestFileTransfer.php:82:Cannot call method run() on object|false.
    core/modules/system/tests/src/Functional/FileTransfer/TestFileTransfer.php:90:Cannot call method run() on object|false.
    core/modules/taxonomy/src/Entity/Vocabulary.php:146:Cannot call method isSyncing() on Drupal\Core\Entity\EntityInterface|false.
    core/modules/views/tests/src/Functional/Handler/FieldWebTest.php:207:Cannot call method xpath() on array.
    core/modules/views/tests/src/Kernel/Entity/ViewSelectionEntityReferenceTest.php:62:Cannot call method getReferenceableEntities() on object|false.
    core/modules/views_ui/tests/src/Functional/CachedDataUITest.php:41:Cannot call method getOwnerId() on null.
    core/modules/views_ui/tests/src/Functional/CachedDataUITest.php:47:Cannot call method getOwnerId() on null.
    core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php:270:Cannot call method getValue() on array|Countable.
    core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php:366:Cannot call method filterEmptyItems() on array<int, string|null>.
    core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php:373:Cannot call method toArray() on string.
    core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php:114:Cannot call method getReferenceableEntities() on object|false.
    core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php:127:Cannot call method getReferenceableEntities() on object|false.
    core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php:92:Cannot call method access() on string.
    core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php:93:Cannot call method access() on string.
    core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php:96:Cannot call method access() on string.
    core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php:97:Cannot call method access() on string.
    core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php:131:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php:174:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php:214:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php:94:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php:97:Cannot call method getDirectoryPath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php:68:Cannot call method get() on array.
    core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php:96:Cannot call method set() on array.
    core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php:103:Cannot call method get() on array.
    core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginTraitTest.php:91:Cannot call method addContextDefinition() on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
    core/tests/Drupal/KernelTests/Core/Test/AssertMailTraitTest.php:44:Cannot call method mail() on object|false.
    core/tests/Drupal/KernelTests/Core/Test/AssertMailTraitTest.php:71:Cannot call method mail() on object|false.
    core/tests/Drupal/KernelTests/Core/Test/AssertMailTraitTest.php:94:Cannot call method mail() on object|false.
    core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php:169:Cannot call method getArguments() on array|string|Stringable.
    core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php:174:Cannot call method getArguments() on array|string|Stringable.
    core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php:175:Cannot call method getArguments() on array|string|Stringable.
    core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php:518:Cannot call method get() on array.
    core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php:521:Cannot call method get() on array.
    core/tests/Drupal/Tests/Core/Render/RendererTest.php:54:Cannot call method bindTo() on callable.
    core/tests/Drupal/Tests/DrupalTestBrowser.php:46:Cannot call method getPath() on false.
    core/tests/Drupal/Tests/DrupalTestBrowser.php:51:Cannot call method getScheme() on false.
    core/tests/Drupal/Tests/DrupalTestBrowser.php:55:Cannot call method getHost() on false.
    core/tests/Drupal/Tests/DrupalTestBrowser.php:56:Cannot call method getPort() on false.
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Yeah the command in the issue summary is filtering out prophecy errors already.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    Okay, thanks! Working on it.

  • Merge request !13098Resolve #3323650 "Fix phpstan l2" โ†’ (Open) created by libbna
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    Iโ€™ve resolved many of the PHPStan errors, but there are still three issues remaining that I wasnโ€™t able to fix. Iโ€™m changing the status to NW and unassigning myself so that someone else can continue working on this.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India
Production build 0.71.5 2024