Example #1
0
 /**
  * Create a new TrueNode.
  *
  * @param boolean $boolean
  *   Parameter is ignored.
  *
  * @return TrueNode
  */
 public static function create($boolean = TRUE)
 {
     $is_upper = FormatterFactory::getDefaultFormatter()->getConfig('boolean_null_upper');
     $node = new TrueNode();
     $node->addChild(NameNode::create($is_upper ? 'TRUE' : 'true'), 'constantName');
     return $node;
 }
function formatFile($filename)
{
    $formatter = FormatterFactory::getDefaultFormatter();
    $tree = Parser::parseFile($filename);
    $formatter->format($tree);
    file_put_contents($filename, $tree->getText());
}
Example #3
0
 /**
  * Create a new NullNode.
  *
  * @param string $name
  *   Parameter is ignored.
  *
  * @return NullNode
  */
 public static function create($name = 'null')
 {
     $is_upper = FormatterFactory::getDefaultFormatter()->getConfig('boolean_null_upper');
     $node = new NullNode();
     $node->addChild(NameNode::create($is_upper ? 'NULL' : 'null'), 'constantName');
     return $node;
 }
 public function testToLower()
 {
     $default_formatter = FormatterFactory::getDefaultFormatter();
     $formatter = new Formatter(['boolean_null_upper' => TRUE]);
     FormatterFactory::setDefaultFormatter($formatter);
     $true = BooleanNode::create(TRUE);
     $this->assertEquals('TRUE', $true->getText());
     $this->assertEquals('true', $true->toLowerCase()->getText());
     FormatterFactory::setDefaultFormatter($default_formatter);
 }
Example #5
0
 /**
  * Create method from function declaration.
  *
  * @param FunctionDeclarationNode $function_node
  * @return ClassMethodNode
  */
 public static function fromFunction(FunctionDeclarationNode $function_node)
 {
     $method_name = $function_node->getName()->getText();
     $parameters = $function_node->getParameterList()->getText();
     $body = $function_node->getBody()->getText();
     /** @var ClassNode $class_node */
     $class_node = Parser::parseSnippet("class Method {public function {$method_name}({$parameters}) {$body}}");
     FormatterFactory::format($class_node);
     $method_node = $class_node->getStatements()[0]->remove();
     return $method_node;
 }
Example #6
0
 /**
  * Add property to class.
  *
  * @param string|ClassMemberListNode $property
  * @return $this
  */
 public function appendProperty($property)
 {
     if (is_string($property)) {
         $property = ClassMemberListNode::create($property);
     }
     $properties = $this->statements->children(Filter::isInstanceOf('\\Pharborist\\ClassMemberListNode'));
     if ($properties->count() === 0) {
         $this->statements->firstChild()->after($property);
     } else {
         $properties->last()->after($property);
     }
     FormatterFactory::format($this);
     return $this;
 }
 /**
  * A helper function to ease exception catching in the __toString() method.
  *
  * @return string
  */
 protected function toString()
 {
     $doc = RootNode::create($this->getNamespace());
     $class = ClassNode::create($this->getName());
     $constructor = ClassMethodNode::create('__construct');
     $class->appendMethod($constructor);
     $constructorDocString = '';
     foreach ($this->getProperties() as $name => $info) {
         $class->createProperty($name, isset($info['default']) ? $info['default'] : NULL, 'protected');
         if (isset($info['description'])) {
             $propertyDocString = "@var {$info['type']} {$name}\n  {$info['description']}";
             $constructorDocString .= "@param {$info['type']} {$name}\n  {$info['description']}\n\n";
         } else {
             $propertyDocString = "@var {$info['type']} {$name}";
             $constructorDocString .= "@param {$info['type']} {$name}\n\n";
         }
         $class->getProperty($name)->closest(Filter::isInstanceOf('\\Pharborist\\Objects\\ClassMemberListNode'))->setDocComment(DocCommentNode::create($propertyDocString));
         $constructor->appendParameter(ParameterNode::create($name));
         $expression = Parser::parseSnippet("\$this->{$name} = \${$name};");
         $constructor->getBody()->lastChild()->before($expression);
         $getter = ClassMethodNode::create('get' . ucfirst($name));
         $class->appendMethod($getter);
         $class->getMethod('get' . ucfirst($name))->setDocComment(DocCommentNode::create("Gets the {$name} value."));
         $getter_expression = Parser::parseSnippet("return \$this->{$name};");
         $getter->getBody()->lastChild()->before($getter_expression);
     }
     $class->getMethod('__construct')->setDocComment(DocCommentNode::create($constructorDocString));
     $doc->getNamespace($this->getNamespace())->getBody()->append($class);
     /* @todo dispatch an event to allow subscribers to alter $doc */
     $formatter = FormatterFactory::getPsr2Formatter();
     $formatter->format($doc->getNamespace($this->getNamespace()));
     return $doc->getText();
 }
Example #8
0
 /**
  * Adds a method to interface.
  *
  * @param InterfaceMethodNode|string $method
  *   The method to append. Can either be an existing method, or a string (a
  *   new public method will be created with that name).
  *
  * @return $this
  */
 public function appendMethod($method)
 {
     if (is_string($method)) {
         $method = InterfaceMethodNode::create($method);
     }
     $this->statements->lastChild()->before($method);
     FormatterFactory::format($this);
     return $this;
 }