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

Error parsing Job class with uninitialized typed properties #1002

Closed
hackel opened this issue Dec 22, 2020 · 3 comments · Fixed by #1005
Closed

Error parsing Job class with uninitialized typed properties #1002

hackel opened this issue Dec 22, 2020 · 3 comments · Fixed by #1005

Comments

@hackel
Copy link

hackel commented Dec 22, 2020

  • Telescope Version: 4.4.0
  • Laravel Version: 8.19.0
  • PHP Version: 7.4.13

Description:

When a Job class contains typed properties that are not initialized with a default value nor set in the constructor, an error is thrown: "Typed property x must not be accessed before initialization".

I believe to solve this in a backward compatible way, it would require something like this in Laravel\Telescope\ExtractProperties::from:

if (version_compare(phpversion(), '7.4', '>=') && !$property->isInitialized($target)) {
   return [$property->getName() => null];
} else {
	...

Not sure if you would rather return some type of UninitializedProperty object instead so that this could actually be distinguished from null in the UI.

Steps To Reproduce:

    public function test_extract_properties_from_class_with_uninitialized_typed_properties()
    {
        $properties = \Laravel\Telescope\ExtractProperties::from(
            new class {
                private string $uninitialized;
            },
        );

        $this->assertEquals(['uninitialized' => null], $properties);
    }
@driesvints
Copy link
Member

This isn't a Telescope issue. Provide a default value for the property if you aren't setting it when initialising it in your app.

https://stackoverflow.com/questions/59265625/why-i-am-suddenly-getting-a-typed-property-must-not-be-accessed-before-initiali

@hackel
Copy link
Author

hackel commented Dec 30, 2020

@driesvints It is absolutely a Telescope issue. Telescope is supposed to be passive and not dictate how my code must be written. Telescope is what was responsible for accessing a variable before it has been initialized, not me.

More info on at least one situation where this comes up with Laravel: in Job classes, only the handle method receives injected dependencies, not the constructor, so they must be assigned later. If you try to do it in the constructor by e.g. using resolve(Dependency::class), then a representation of that dependency will get stored when the job is queued, which really breaks things. Making the types of these dependencies nullable when they shouldn't be is bad design, particularly when the fix is so simple.

@driesvints
Copy link
Member

@hackel it's not. Please read the stackoverflow link I added above.

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

Successfully merging a pull request may close this issue.

2 participants