Getting better code completion in your editor with Kirby

Looking at the forum FAQ it looks like linking to your own article is okay if it’s relevant to the community, so here goes. I’ll try to summarize the article here so busy readers don’t have to click through!

Kirby 3 is using PHP 7’s type annotations extensively in its source code, but oftentimes your code editor or IDE cannot show you that information because it doesn’t know what $page or $kirby means in different contexts, like templates and controllers.

I wrote an article on type hints for Kirby which explains the explains the problem — basically, Kirby is a bit too magical for your IDE’s taste — and some solutions I found.

Type hints for templates

We can use PHPDoc syntax to declare the type of the variables injected by Kirby or by your controllers:

<?php // site/templates/article.php

/** @var Kirby\Cms\Page $page */
/** @var Kirby\Cms\Site $site */

$htmlTitle = $page->title() . ' – ' . $site->title();

Now we can work with the $page variable and a good PHP IDE will offer autocompletions, parameter names and types, etc.

Type annotations for controllers

Since controllers are functions, we can use type annotations in the function signature:

<?php // site/controllers/article.php

use Kirby\Cms\Page;
use Kirby\Cms\Site;

return function (Page $page, Site $site) {
  return [
    'htmlTitle' => $page->title() . ' – ' . $site->title()
  ];
}

Okay but what’s a good code editor for this?

I’ve mostly used PhpStorm, which is great but not cheap. Other IDEs like Netbeans or Visual Studio might work well too.

VS Code doesn’t have much PHP support out of the box, but I tried the PHP Intelephense extension and it seems to work well (make sure to follow the “Quick Start” steps).

5 Likes

Hi!

I just tried this for Visual Code v1.71.2 and PHP Intelephense v1.8.2 with PHP8. It can resolve some Kirby 3.7 helpers but variables like $page are not resolved in my controllers or templates within the PHP open/end tags. Is there nowadays a way to get this to work without adding Type hints or Type Annotations by hand in your custom code? If I do that, resolving the variable works but of course it adds additional, repetetive work. Would it help to have a PHP Intelephense license for the full feature set?

Thank you for your thread and thanks to everyone who helps!

Best Regards