PhpGenerator and PSR output

4 months ago by David Grudl     edit

Do you need to generate beautifully formatted PHP code? The best way is to use the Nette PhpGenerator library, which has a truly understandable API.

Example:

$class = (new Nette\PhpGenerator\ClassType('Demo'))
	->setFinal()
	->setExtends('ParentClass');

$class->addProperty('id', 1)
	->setVisibility('private')
	->addComment('@var int');

$class->addMethod('getId')
	->addComment('Returns identifier.')
	->setReturnType('int')
	->setBody('return $this->id;');

echo $class;

The output follows the coding standard used in Nette, which is identical to PSR-12 except that it uses tabs instead of spaces for indentation and separates the methods with two blank lines for better readability:

final class Demo extends ParentClass
{
→   /** @var int */
→   private $id = 1;


→   /**
→	* Returns identifier.
→	*/
→   public function getId(): int
→   {
→   →   return $this->id;
→   }
}

PhpGenerator, however, can generate code completely compliant with PSR-12. How to do it?

The expression echo $class is actually an abbreviation that asks the printer Nette\PhpGenerator\Printer and its method printClass($class) to generate code. And the output from this printer conforms to the Nette standard. So it will be enough to use another renderer, specifically PsrPrinter:

$printer = new Nette\PhpGenerator\PsrPrinter;
echo $printer->printClass($class);

Result:

final class Demo extends ParentClass
{
	/** @var int */
	private $id = 1;

	/**
	 * Returns identifier.
	 */
	public function getId(): int
	{
		return $this->id;
	}
}

Using the printer, you can generate code not only for classes, but also for functions (printFunction(), printClosure() and printArrowFunction()), methods (printMethod()), namespaces (printNamespace()), and PHP files (printFile()).