Resource icon

xf-typegen 1.0.1

No permission to download

wh1teend

Member
Licensed customer
wh1teend submitted a new resource:

xf-typegen - Real PHP types for XenForo's runtime magic — finder/repository/find() and entity properties autocomp

What it does​

XenForo wires most of itself together at runtime out of short string names. Convenient to write, but your IDE has no idea what comes back:

PHP:
\XF::finder('XF:User')->fetchOne();   // your IDE sees: mixed
\XF::repository('XF:User');           // mixed
\XF::em()->find('XF:User', 1);        // mixed
$user->username;                      // unknown property
foreach ($finder->fetch() as $u) { } // $u is... who knows

So you either keep every entity...

Read more about this resource...
 
This is wholly unnecessary in XF 2.3.

PHP:
\XF::repository('XF:User');

Should be:

PHP:
\XF::repository(\XF\Repository\UserRepository::class);

Your IDE sees: \XF\Repository\UserRepository

XenForo 2.3 uses template generics to handle the resolution in PHPDoc blocks automatically.

If you're trying to support both XF 2.2 and XF 2.3, my strong suggestion is simply, don't. Most customers are not using XF 2.2.
 
You're right, and digging in properly: XF ships xf-dev:generate-phpstorm-meta on both 2.2 and 2.3, which already resolves the string-style call sites (finder/repository/service/find and the $this-> helpers), entities ship full @property, and on 2.3 generics handle the ::class style and the finder chain. So for standard 2.3 code, core has it covered — fair, I was overselling it.

What's left that core doesn't touch, either version:
  • Board options — \XF::options()->boardTitle is mixed (bare \ArrayObject on 2.2, XF\Options with #[AllowDynamicProperties] but no per-key @property on 2.3).
  • The runtime XFCP_* proxies — no file, so the IDE flags class Foo extends XFCP_Foo as undefined.
  • Columns another add-on adds to an entity via XFCP — not in that entity's shipped @property, which is generated against the bare class.
And one 2.2-only gap: without generics, finder('XF:User')->fetchOne() resolves to the base Entity rather than the concrete one.

That's a much narrower scope than I pitched. I'll rewrite the description around just those gaps rather than implying it types things core already does.

This is wholly unnecessary in XF 2.3.

PHP:
\XF::repository('XF:User');

Should be:

PHP:
\XF::repository(\XF\Repository\UserRepository::class);

Your IDE sees: \XF\Repository\UserRepository

XenForo 2.3 uses template generics to handle the resolution in PHPDoc blocks automatically.

If you're trying to support both XF 2.2 and XF 2.3, my strong suggestion is simply, don't. Most customers are not using XF 2.2.
 
Last edited:
My StandardLib add-on has a bunch of wrapper methods in \SV\StandardLib\Helper which implement a pile of the type hinting for XF2.2 (and XF2.1 for that matter).

It doesn't touch \XF::options(), but that requires an XF2.3 code change to use \XF\Options class, and you can largely fix that by just writing against XF2.3 or manually backporting patching that part.
 
Back
Top Bottom