Skip to content

Flarum\Extend\Model::cast not working when used inside conditional extender #4808

Description

@DavideIadeluca

Current Behavior

When Flarum\Extend\Model::cast is used to add a custom attribute cast inside a Flarum\Extend\Conditional extender, Static Code Analysis doesn't pick up on it

Steps to Reproduce

Add a cast in extend.php:

 (new Extend\Conditional())
        ->whenExtensionEnabled('acme-foobar', fn () => [
            (new Extend\Model(Foo::class))
                ->cast('bar', 'boolean'),
        ]),

And try to use it somewhere:

$foo->bar = true
// Access to an undefined property ACME\Foobar\Foo::$bar

Expected Behavior

Static code analysis should pick up on casts inside the conditional extender or cast() should throw an error when used inside a conditional extender to prevent it unexpectedly not working

Screenshots

No response

Environment

  • Flarum version: 1.8.16
  • Website URL: n.a.
  • Webserver: n.a.
  • Hosting environment: n.a.
  • PHP version: 8.3.31
  • Browser: n.a.

Output of php flarum info

No response

Possible Solution

No response

Additional Context

Workaround now is to just always add the casts outside of conditional extenders which has no negative sideffect on runtime logic, however the intent when reading code gets clouded.

I didn't confirm that the issue also happens on Flarum 2.x however I don't recall any changes being made in this area which could address this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions