コード例 #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;
 }
コード例 #2
0
    public function testClassMethodNode()
    {
        $method = ClassMethodNode::create('foo');
        $method->setDocComment(DocCommentNode::create('{@inheritdoc}'));
        $expected = <<<'END'
/**
 * {@inheritdoc}
 */
public function foo() {}
END;
        $this->assertEquals($expected, $method->getText());
    }
コード例 #3
0
 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);
 }
コード例 #4
0
ファイル: Formatter.php プロジェクト: kidaa30/redcat
 public function visitClassMethodNode(ClassMethodNode $node)
 {
     if ($node->getBody()) {
         $close_brace = $node->getBody()->lastChild();
         $this->newlineBefore($close_brace);
     }
     $this->visitMethod($node);
     if ($node->getAbstract() && $node->firstChild() !== $node->getAbstract()) {
         $node->getAbstract()->swapWith($node->getVisibility());
     }
     if ($node->getFinal() && $node->firstChild() !== $node->getFinal()) {
         $node->getFinal()->swapWith($node->getVisibility());
     }
 }
コード例 #5
0
ファイル: Parser.php プロジェクト: kidaa30/redcat
 /**
  * Parse a class method
  * @param PartialNode|null $doc_comment DocBlock associated with method
  * @param ModifiersNode $modifiers Method modifiers
  * @return ClassMethodNode
  */
 private function classMethod($doc_comment, ModifiersNode $modifiers)
 {
     $node = new ClassMethodNode();
     $node->mergeNode($doc_comment);
     $node->mergeNode($modifiers);
     $this->mustMatch(T_FUNCTION, $node);
     $this->tryMatch('&', $node, 'reference');
     $this->mustMatch(T_STRING, $node, 'name');
     $this->parameterList($node);
     if ($modifiers->getAbstract()) {
         $this->endStatement($node);
         return $node;
     }
     $this->matchHidden($node);
     $this->body($node);
     return $node;
 }
コード例 #6
0
 /**
  * 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();
 }
コード例 #7
0
    public function testFunctionToMethod()
    {
        $class = <<<END
class DefaultController extends ControllerBase {}
END;
        $function = <<<'END'
function diff_diffs_overview($node) {
  drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
  if ($cond) {
    test();
  }
  return drupal_get_form('diff_node_revisions', $node);
}
END;
        $expected = <<<'END'
class DefaultController extends ControllerBase {

  public function diff_diffs_overview($node) {
    drupal_set_title(t('Revisions for %title', ['%title' => $node->title]), PASS_THROUGH);
    if ($cond) {
      test();
    }
    return drupal_get_form('diff_node_revisions', $node);
  }

}
END;
        /** @var ClassNode $class */
        $class = Parser::parseSnippet($class);
        $function = Parser::parseSnippet($function);
        $class->appendMethod(ClassMethodNode::fromFunction($function));
        $this->assertEquals($expected, $class->getText());
    }
コード例 #8
0
 /**
  * @return \Pharborist\Objects\ClassMethodNode
  */
 protected function addMethod(FunctionDeclarationNode $function, ClassNode $class, $alias = NULL)
 {
     $method = ClassMethodNode::fromFunction($function);
     if ($alias) {
         $method->setName($alias);
     }
     $class->appendMethod($method);
     // Add the parameters required for FormInterface conformance.
     $parameters = $method->getParameters()->toArray();
     if (empty($parameters)) {
         $parameters[0] = ParameterNode::create('$form');
         $method->appendParameter($parameters[0]);
     }
     if (sizeof($parameters) == 1) {
         $parameters[1] = ParameterNode::create('$form_state');
         $method->appendParameter($parameters[1]);
     }
     // The $form parameter must have the array type hint.
     $parameters[0]->setTypeHint('array');
     // The form state is never passed by reference.
     $parameters[1]->setReference(FALSE);
     // Additional parameters MUST have a default value of NULL in order to conform
     // to FormInterface.
     for ($i = 2; $i < sizeof($parameters); $i++) {
         $parameters[$i]->setValue(new TokenNode(T_STRING, 'NULL'));
     }
     $this->formStateRewriter->rewrite($parameters[1]);
     return $method;
 }
コード例 #9
0
 /**
  * Creates a class method from this function and add it to the given
  * class definition.
  *
  * @param \Pharborist\Objects\ClassNode $class
  *  The class to add the new method to.
  *
  * @return \Pharborist\Objects\ClassMethodNode
  *  The newly created method.
  */
 public function cloneAsMethodOf(ClassNode $class)
 {
     $clone = ClassMethodNode::fromFunction($this);
     $class->appendMethod($clone);
     return $clone;
 }