for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php declare(strict_types=1);
/**
* Created by Vitaly Iegorov <[email protected]>.
* on 04.09.16 at 10:13
*/
namespace samsonframework\generator;
* Trait for generators that can generate internal code.
*
* @author Vitaly Egorov <[email protected]>
trait CodeTrait
{
/** @var array Collection of code lines */
protected $code = [];
* Define code condition block.
* @return IfGenerator Condition generator instance
public function defIf(): IfGenerator
return (new IfGenerator($this))->setIndentation($this->indentation);
indentation
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
}
* Add function code line.
* @param string $code Code line
* @return $this
public function defLine(string $code)
$this->code[] = $code;
return $this;
* Build arguments list with types.
* @param array $arguments Arguments collection
* @param array $defaults Arguments default values collection
* @return string Arguments list
protected function buildArguments(array $arguments, array $defaults = []) : string
$argumentsString = [];
$defaults = array_filter($defaults);
foreach ($arguments as $argumentName => $argumentType) {
// Group name with type
$argumentsString[] = ($argumentType !== null ? $argumentType . ' ' : '') .
'$' . $argumentName .
(array_key_exists($argumentName, $defaults) ? ' = ' . $defaults[$argumentName] : '');
return implode(', ', $argumentsString);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: