gm314 / nitria
PHP 7 Code Generator
Installs: 10 175
Dependents: 5
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.0
- gm314/common: *
Requires (Dev)
README
Class generator for PHP7 code. It will take care of indentation, use statements, php doc creation.
Installation
{ "require": { "gm314/nitria": "*" } }
Usage
Create a class and set extends and implements
$classGenerator = new ClassGenerator("Generated\\MyClass", true); // add extends statement, a use statement will be added automatically $classGenerator->setExtends("BaseClass\\ClassName"); // add implement statement $classGenerator->addImplements("\\Serializable"); // add a constant $classGenerator->addConstant("CONSTANT_STRING", '"hello"');
Properties
Properties have a name, a type, a modifier and an optional default value. The example
will generate a static
private
property with the name myName
and the default
value 19.08
.
$classGenerator->addProperty("myName","float","private", false, '19.08', 'doc bloc comment');
This will result in the following code
/** * @var float doc bloc comment */ private $myName = 19.08;
Or you can use the short version for non static properties.
// add property (for classes use statement will be added - unless the class is in the same namespace) $classGenerator->addPrivateProperty("iAmPrivat", 'MyPackage\MyClass'); $classGenerator->addProtectedProperty("iAmProtected", "array", []); $classGenerator->addPublicProperty("iAmPublic", "float");
And for the static properties.
// add static property $classGenerator->addPrivateStaticProperty("iAmPrivatStatic", "int"); $classGenerator->addProtectedStaticProperty("iAmProtectedStatic", "bool"); $classGenerator->addPublicStaticProperty("iAmPublicStatic", "array");
Methods
$method = $classGenerator->addPublicMethod("myFunction"); $method->addParameter("string", "parameterName", '"defaultValue!"'); $method->addParameter("\\DateTime", "datetime"); // the method will have a return type string that is not nullable $method->setReturnType("string", false); $method->addCodeLine('return $parameterName;');
the above code will generate the following method
/** * @param string $parameterName * @param \DateTime $datetime * @return string */ public function myFunction(string $parameterName = "defaultValue!", \DateTime $datetime) : string { return $parameterName; }
// method generation $classGenerator->addPrivateMethod("iAmPrivate"); $classGenerator->addProtectedMethod("iAmProtected"); $classGenerator->addPublicMethod("iAmPublic"); // static method generation $classGenerator->addPrivateStaticMethod("iAmPrivateStatic"); $classGenerator->addProtectedStaticMethod("iAmProtectedStatic"); $classGenerator->addPublicStaticMethod("iAmPublicStatic");
Method Content generation
Code
$method = $classGenerator->addPublicMethod("sayIf"); $method->addParameter("int", "intParam"); $method->setReturnType("int", false); // add a simple line of code $method->addCodeLine('return $intParam * $intParam;');
If Statement
$method = $classGenerator->addPublicMethod("sayIf"); $method->addParameter("int", "int"); $method->setReturnType("int", false); // start if statement >> if ($int ===1) { $method->addIfStart('$int === 1'); $method->addCodeLine('return 1;'); // add if else statement >> } else if ($int === 2) { $method->addIfElseIf('$int === 2'); $method->addCodeLine('return 2;'); // add else statement >> } else { $method->addIfElse(); $method->addCodeLine('return 3;'); // close if statement >> } $method->addIfEnd();
While Statement
$method = $classGenerator->addPublicMethod("sayWhile"); $method->addParameter("int", "int"); $method->setReturnType("string", false); $method->addCodeLine('$string = "";'); // start while statement >> while($int++ < 10) { $method->addWhileStart('$int++ < 10'); $method->addCodeLine('$string .= "x";'); // end while statement >> } $method->addWhileEnd(); $method->addCodeLine('return $string;');
Foreach Statement
$method = $classGenerator->addPublicMethod("sayForeach"); $method->addParameter("array", "list"); $method->setReturnType("string", false); $method->addCodeLine('$string = "";'); // start foreach >> foreach($list as $item) { $method->addForeachStart('$list as $item'); $method->addCodeLine('$string .= $item;'); // end foreach >> } $method->addForeachEnd(); $method->addCodeLine('return $string;');
Switch Statement
$method = $classGenerator->addPublicMethod("saySwitch"); $method->addParameter("string", "value"); $method->setReturnType("string", false); // start switch statement >> switch($value) { $method->addSwitch('$value'); // case statement >> case "a": $method->addSwitchCase('"a"'); $method->addCodeLine('return "a";'); // case break >> break; $method->addSwitchBreak(); // default >> default: $method->addSwitchDefault(); $method->addCodeLine('return "c";'); $method->addSwitchBreak(); $method->addSwitchEnd();
Tests / More examples
See under tests/End2End
- GeneratorTest
- CodeTest
License
MIT