We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

IDE autocomplete for custom services?

Given

<?php
$di = new Phalcon\Di\FactoryDefault\Cli;

$di->setShared('myCustomService', new MyCustomService);

$console = new Phalcon\Cli\Console;
$console->setDI($di);

$arguments = [];
foreach ($argv as $k => $arg) {
    if ($k === 1) {
        $arguments["task"] = $arg;
    } elseif ($k === 2) {
        $arguments["action"] = $arg;
    } elseif ($k >= 3) {
        $arguments["params"][] = $arg;
    }
}

$di->setShared('console', $console);

// Handle incoming arguments
$console->handle($arguments);

How does one get IDE hinting/autocomplete for $this->myCustomService->doSomething()? I have the ide stubs in place and things like $this->getDI() and the like already work. It's just the things that I add that aren't getting hinted and would be nice.

edited Aug '20

My best practice for this is to create a ServicesAwareTrait which includes all the definitions for your services:


/**
 * @property-read Config $config
 * @property-read Adapter $db
 * @property-read MailFactory $mailer
 * @property-read Logger $log
 * @property-read \Phalcon\Mvc\View\Engine\Volt $voltService
 * @property-read Redis $redis
 * @property-read SimplePay $simplepay
 * @property-read InvoiceService $invoice
 * @property-read StatsdClient $metrics
 */
trait ServicesAwareTrait
{
    // body was intentionally left empty
}

Then you either use this trait in all your controllers, or just create a base controller which uses it, then inherit from there. The pro for this is that you can use this trait in any context where there is DI, not just controllers.