Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
78.95% |
15 / 19 |
CRAP | |
89.66% |
52 / 58 |
CodeGenerator | |
0.00% |
0 / 1 |
|
78.95% |
15 / 19 |
27.81 | |
89.66% |
52 / 58 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
addLine | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
newLine | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
addNamespace | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
addUse | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
addDocComment | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
addClassStart | |
0.00% |
0 / 1 |
3.01 | |
88.89% |
8 / 9 |
|||
addClassEnd | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
addConstant | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
addProtectedMember | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
addStaticProtectedMember | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
addMember | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
|||
newMethod | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
newPublicMethod | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
newPublicStaticMethod | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
newPublicConstructor | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
writeTo | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
incrementIndent | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
decrementIndex | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
<?php | |
declare(strict_types = 1); | |
namespace Siesta\CodeGenerator; | |
use Siesta\Util\File; | |
/** | |
* @author Gregor Müller | |
*/ | |
class CodeGenerator | |
{ | |
const IDENT = " "; | |
/** | |
* @var string | |
*/ | |
protected $code; | |
/** | |
* @var int | |
*/ | |
protected $currentIndent; | |
/** | |
* CodeGenerator constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->currentIndent = 0; | |
$this->code = "<?php" . PHP_EOL . PHP_EOL . 'declare(strict_types = 1);' . PHP_EOL . PHP_EOL; | |
} | |
/** | |
* @param $content | |
* @param int $linebreaks | |
*/ | |
public function addLine(string $content, int $linebreaks = 1) | |
{ | |
for ($i = 0; $i < $this->currentIndent; $i++) { | |
$this->code .= self::IDENT; | |
} | |
$this->code .= $content; | |
$this->newLine($linebreaks); | |
} | |
/** | |
* @param int $count | |
*/ | |
public function newLine($count = 1) | |
{ | |
for ($i = 0; $i < $count; $i++) { | |
$this->code .= PHP_EOL; | |
} | |
} | |
/** | |
* @param string $namespace | |
*/ | |
public function addNamespace(string $namespace) | |
{ | |
$this->addLine("namespace " . $namespace . ";", 2); | |
} | |
/** | |
* @param string $className | |
*/ | |
public function addUse(string $className) | |
{ | |
$this->addLine("use " . $className . ";"); | |
} | |
/** | |
* @param array $lineList | |
*/ | |
public function addDocComment(array $lineList) | |
{ | |
$this->addLine("/**"); | |
foreach ($lineList as $lineItem) { | |
$this->addLine(" * " . $lineItem); | |
} | |
$this->addLine(" */"); | |
} | |
/** | |
* @param string $classShortName | |
* @param string $extends | |
* @param string $implements | |
*/ | |
public function addClassStart(string $classShortName, string $extends = null, string $implements = null) | |
{ | |
$line = "class $classShortName"; | |
if ($extends !== null) { | |
$line .= " extends $extends"; | |
} | |
if ($implements !== null) { | |
$line .= " implements $implements"; | |
} | |
$this->addLine($line); | |
$this->addLine("{"); | |
$this->currentIndent++; | |
} | |
/** | |
* | |
*/ | |
public function addClassEnd() | |
{ | |
$this->currentIndent--; | |
$this->addLine("}", 0); | |
} | |
/** | |
* @param string $name | |
* @param string $value | |
* @param bool $quote | |
*/ | |
public function addConstant(string $name, string $value, $quote = true) | |
{ | |
if ($quote) { | |
$value = '"' . $value . '"'; | |
} | |
$this->addLine("const " . strtoupper($name) . ' = ' . $value . ";", 2); | |
} | |
/** | |
* @param string $memberName | |
* @param string $memberType | |
*/ | |
public function addProtectedMember(string $memberName, string $memberType = null) | |
{ | |
$this->addMember("protected", false, $memberName, $memberType); | |
} | |
/** | |
* @param string $memberName | |
* @param string|null $memberType | |
*/ | |
public function addStaticProtectedMember(string $memberName, string $memberType = null) | |
{ | |
$this->addMember("protected", true, $memberName, $memberType); | |
} | |
/** | |
* @param string $modifier | |
* @param bool $static | |
* @param string $memberName | |
* @param string|null $memberType | |
*/ | |
public function addMember(string $modifier, bool $static, string $memberName, string $memberType = null) | |
{ | |
if ($memberType !== null) { | |
$this->addDocComment(['@var ' . $memberType]); | |
} | |
if ($static) { | |
$this->addLine($modifier . ' static $' . $memberName . ";", 2); | |
} else { | |
$this->addLine($modifier . ' $' . $memberName . ";", 2); | |
} | |
} | |
/** | |
* @param string $name | |
* @param string $modifier | |
* @param bool $static | |
* | |
* @return MethodGenerator | |
*/ | |
public function newMethod(string $name, string $modifier, bool $static) | |
{ | |
return new MethodGenerator($this, $name, $modifier, $static); | |
} | |
/** | |
* @param string $name | |
* | |
* @return MethodGenerator | |
*/ | |
public function newPublicMethod(string $name) : MethodGenerator | |
{ | |
return new MethodGenerator($this, $name, "public", false); | |
} | |
/** | |
* @param string $name | |
* | |
* @return MethodGenerator | |
*/ | |
public function newPublicStaticMethod(string $name) : MethodGenerator | |
{ | |
return new MethodGenerator($this, $name, "public", true); | |
} | |
/** | |
* @return MethodGenerator | |
*/ | |
public function newPublicConstructor() | |
{ | |
$methodGenerator = new MethodGenerator($this, '__construct', "public", false); | |
$methodGenerator->setIsConstructor(true); | |
return $methodGenerator; | |
} | |
/** | |
* @param File $file | |
*/ | |
public function writeTo(File $file) | |
{ | |
$file->putContents($this->code); | |
} | |
/** | |
* | |
*/ | |
public function incrementIndent() | |
{ | |
$this->currentIndent++; | |
} | |
/** | |
* | |
*/ | |
public function decrementIndex() | |
{ | |
$this->currentIndent--; | |
} | |
} |