Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking change in v0.1.15, classes not constructed the same? #23

Closed
ChrisHardie opened this issue Apr 19, 2023 · 2 comments
Closed

Breaking change in v0.1.15, classes not constructed the same? #23

ChrisHardie opened this issue Apr 19, 2023 · 2 comments

Comments

@ChrisHardie
Copy link
Contributor

The change in v0.1.15 introduced in #18 created a fatal error for me when deployed to production. It may be my code, but here's what I saw:

The webhook event handling stopped happening synchronously, which was succeeding, and was moved to a queued job, which failed.

The code that ran to handle the webhook event is

<?php

namespace App\Http\Handlers\Woocommerce;

class UserCreated extends AbstractUserEvent
{
    public function handle()
    {
        $this->createContactFromUser();
    }
}

and the parent class has this construction:

<?php

namespace App\Http\Handlers\Woocommerce;

use App\Data\WoocommerceCustomer;
use App\Data\WordpressUser;
use App\Helpers\WoocommerceHelpers;
use App\Models\Contact;
use Illuminate\Support\Facades\Log;

abstract class AbstractUserEvent extends AbstractWoocommerceEvent
{
    private WoocommerceHelpers $wooHelper;

    public function __construct(public string $event, public array $data)
    {
        parent::__construct($event, $data);

        // Create a Woo customer object we can work with.
        $this->wooHelper = new WoocommerceHelpers();

        ...
    }

    protected function createContactFromUser(): Contact|null
    {
        $newContact = $this->wooHelper->createLocalFromWpUser($this->user);
        return $newContact;
    }

    ...
}

The error I started getting was:

local.ERROR: Typed property App\Http\Handlers\Woocommerce\AbstractCustomerEvent::$wooHelper must not be accessed before initialization {"exception":"[object] (Error(code: 0): Typed property App\\Http\\Handlers\\Woocommerce\\AbstractCustomerEvent::$wooHelper must not be accessed before initialization at <path>/app/Http/Handlers/Woocommerce/AbstractCustomerEvent.php:51)

So it appears that the __construct method in AbstractUserEvent is not run following this release. Downgrading to 0.1.14 fixed it.

Please let me know if there's any other information that would be helpful to understand or advise on what's happening. Thank you.

@hotmeteor
Copy link
Owner

The change was made from dispatchAfterResponse to dispatch because the former cannot go to a queue, which is what is required for some handlers.

As for your examples, I'm not sure what code is yours or another package's, but I think you just need to make your object nullable? Seems like more of a PHP 8 thing.

abstract class AbstractUserEvent extends AbstractWoocommerceEvent
{
    private WoocommerceHelpers|null $wooHelper = null;

    public function __construct(public string $event, public array $data)
    {

...

@ChrisHardie
Copy link
Contributor Author

(Closing this until/unless I can provide more details showing it's actually an issue and not my misunderstanding.)

@ChrisHardie ChrisHardie closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants