Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Uncaught Error: Cannot use object of type stdClass as array #523

Merged
merged 5 commits into from
Jun 12, 2019

Conversation

pfrenssen
Copy link
Contributor

This PR replaces #504 with a test first approach.

Original report:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in web/modules/contrib/og/src/GroupTypeManager.php on line 268

In #450 we refactored the use Drupal\Core\State\StateInterface to Drupal\Core\Cache\CacheBackendInterface but CacheBackendInterface::get() returns an object with a data property (not an array).

object(stdClass)[6055]
  public 'cid' => string 'og.group_manager.group_relation_map' (length=35)
  public 'data' => 
    array (size=1)
      'node' => 
        array (size=1)
          'subsite' => 
            array (size=2)
              ...
  public 'created' => string '1557745337.212' (length=14)
  public 'expire' => string '-1' (length=2)
  public 'serialized' => string '1' (length=1)
  public 'tags' => 
    array (size=0)
      empty
  public 'checksum' => string '0' (length=1)
  public 'valid' => boolean true

@pfrenssen
Copy link
Contributor Author

The test fails on the reported error message:

There was 1 error:

1) Drupal\Tests\og\Kernel\Entity\GetBundleByBundleTest::testGetGroupContentBundleIdsByGroupBundleUsesCachedData
Error: Cannot use object of type stdClass as array

/home/travis/build/Gizra/og/src/GroupTypeManager.php:254
/home/travis/build/Gizra/og/tests/src/Kernel/Entity/GetBundleByBundleTest.php:602

Now let's fix it by cherry-picking the solution from #504.

@pfrenssen
Copy link
Contributor Author

I cherry-picked the fix from #504 and split off the retrieval of cached data to a separate method so we can leverage PHP 7's return type declaration to alert us if we are returning a cache object rather than the expected array.

@pfrenssen
Copy link
Contributor Author

Merging, the tests were green, codeclimate is still experimental and failing on false positives.

@pfrenssen pfrenssen merged commit 5c51ea9 into 8.x-1.x Jun 12, 2019
@pfrenssen pfrenssen deleted the cannot-use-object-as-array branch June 12, 2019 12:08
@pfrenssen pfrenssen added this to the 8.x-1.0-alpha4 milestone Jul 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants