If I implement a class, which gets some services injected, I have to write this bulk of code:
<?php
namespace Hn\AssetDbBundle\Services;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Class SomeNewService
* @package Hn\AssetDbBundle\Services
*/
class SomeNewService {
/**
* @var TwigEngine
*/
private $engine;
/**
* @var KernelInterface
*/
private $kernel;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(TwigEngine $engine, KernelInterface $kernel, LoggerInterface $logger) {
$this->engine = $engine;
$this->kernel = $kernel;
$this->logger = $logger;
}
}
This seems redundant. Is there a way I can reduce the amount of code I have to write? Is there a live template for initializing the fields or can I autogenerate the bulk of this block otherwise?
You can use ⌘N (macOS), or Alt+Insert (Windows/Linux) for the Generate menu and then select Constructor , Getter , Setter or Getter and Setter .
As you type the argument name, hit Alt-Enter and choose Add 'field' to constructor , then type the next argument. Or later, put the cursor on each argument and invoke it. PyCharm will create the assignment for you.
You can also do the other way around, defining the properties first, and then in the "Generate" menu (Cmd+N), use "Constructor".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With