You are here

public function ClassCodeGenerator::generate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCodeGenerator.php \Prophecy\Doubler\Generator\ClassCodeGenerator::generate()

Generates PHP code for class node.

Parameters

string $classname:

Node\ClassNode $class:

Return value

string

File

vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCodeGenerator.php, line 30

Class

ClassCodeGenerator
Class code creator. Generates PHP code for specific class node tree.

Namespace

Prophecy\Doubler\Generator

Code

public function generate($classname, Node\ClassNode $class) {
  $parts = explode('\\', $classname);
  $classname = array_pop($parts);
  $namespace = implode('\\', $parts);
  $code = sprintf("class %s extends \\%s implements %s {\n", $classname, $class
    ->getParentClass(), implode(', ', array_map(function ($interface) {
    return '\\' . $interface;
  }, $class
    ->getInterfaces())));
  foreach ($class
    ->getProperties() as $name => $visibility) {
    $code .= sprintf("%s \$%s;\n", $visibility, $name);
  }
  $code .= "\n";
  foreach ($class
    ->getMethods() as $method) {
    $code .= $this
      ->generateMethod($method) . "\n";
  }
  $code .= "\n}";
  return sprintf("namespace %s {\n%s\n}", $namespace, $code);
}