示例#1
0
 /**
  * Adds a method to a class/trait.
  *
  * @param \Pharborist\Functions\FunctionDeclarationNode|\Pharborist\Objects\ClassMethodNode|string $method
  *  The method to append. Can either be an existing method, a function (which
  *  will be converted to a public method), or a string (a new public method
  *  will be created with that name).
  *
  * @return $this
  */
 public function appendMethod($method)
 {
     if ($method instanceof FunctionDeclarationNode) {
         $method = ClassMethodNode::fromFunction($method);
     } elseif (is_string($method)) {
         $method = ClassMethodNode::create($method);
     }
     $this->statements->lastChild()->before($method);
     FormatterFactory::format($this);
     return $this;
 }
    public function testClassMethodNode()
    {
        $method = ClassMethodNode::create('foo');
        $method->setDocComment(DocCommentNode::create('{@inheritdoc}'));
        $expected = <<<'END'
/**
 * {@inheritdoc}
 */
public function foo() {}
END;
        $this->assertEquals($expected, $method->getText());
    }
 public function execute()
 {
     /** @var \Pharborist\Objects\ClassNode $class */
     $class = $this->target->getIndexer('class')->get($this->configuration['target']);
     // Use reflection to get the method definition.
     list($interface, $method) = explode('::', $this->configuration['definition']);
     $interface = new \ReflectionClass($interface);
     $method = $interface->getMethod($method);
     $node = ClassMethodNode::create($method->getName());
     $node->setDocComment(DocCommentNode::create('@inheritdoc'));
     $class->appendMethod($node);
     $node->matchReflector($method);
     // @TODO There needs to be a way to implement the method body!
     $this->target->save($class);
 }
 public function testClassMethod()
 {
     $method = ClassMethodNode::create('someMethod');
     $this->assertEquals('public function someMethod() {}', $method->getText());
     $method->setVisibility(Token::_protected());
     $this->assertEquals('protected function someMethod() {}', $method->getText());
     $method->setFinal(TRUE);
     $this->assertEquals('final protected function someMethod() {}', $method->getText());
     $method->setFinal(FALSE);
     $this->assertEquals('protected function someMethod() {}', $method->getText());
     $method->setStatic(TRUE);
     $this->assertEquals('protected static function someMethod() {}', $method->getText());
     $method->setStatic(FALSE);
     $this->assertEquals('protected function someMethod() {}', $method->getText());
 }
 /**
  * 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();
 }