/**
  * Creates a PHP parameter from reflection
  * 
  * @param \ReflectionParameter $ref
  * @return PhpParameter
  */
 public static function fromReflection(\ReflectionParameter $ref)
 {
     $parameter = new static();
     $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
     if ($ref->isDefaultValueAvailable()) {
         $parameter->setDefaultValue($ref->getDefaultValue());
     }
     // find type and description in docblock
     $docblock = new Docblock($ref->getDeclaringFunction());
     $params = $docblock->getTags('param');
     $tag = $params->find($ref->name, function (ParamTag $t, $name) {
         return $t->getVariable() == '$' . $name;
     });
     if ($tag !== null) {
         $parameter->setType($tag->getType(), $tag->getDescription());
     }
     // set type if not found in comment
     if ($parameter->getType() === null) {
         if ($ref->isArray()) {
             $parameter->setType('array');
         } elseif ($class = $ref->getClass()) {
             $parameter->setType($class->getName());
         } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
             $parameter->setType('callable');
         }
     }
     return $parameter;
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // recuperation du controller
     $controller = $this->argument('controller');
     // création de la méthode
     $method = camel_case($this->argument('name'));
     $method = PhpMethod::create($method);
     $method->setStatic(true);
     // Gestion du body
     $body = file_get_contents(__DIR__ . '/stubs/table.stub');
     $method->setBody($body);
     // block de commentaire
     $dockblock = new Docblock();
     $dockblock->appendTag(TagFactory::create('name', 'Artisan'));
     $dockblock->appendTag(TagFactory::create('see', 'php artisan ffmake:table'));
     $dockblock->appendTag(TagFactory::create('generated', Carbon::now()));
     $method->setDocblock($dockblock);
     // Récupération du controller à mettre à jour
     $controller = ucfirst(camel_case($controller . '_controller'));
     $controller = new \ReflectionClass('App\\Http\\Controllers\\' . $controller);
     $class = PhpClass::fromReflection($controller)->setMethod($method);
     $class->setParentClassName('Controller');
     // fix la gestion des namespaec pour la parent class
     // Génration du code
     $generator = new CodeGenerator();
     $class = '<?php ' . $generator->generate($class);
     // inscription du code dans la classe
     file_put_contents($controller->getFileName(), $class);
     $this->info('Action generated dans le fichier : ' . $controller->getFileName());
 }
    public function testDocblock()
    {
        $expected = '/**
 * @var mixed $foo bar
 */';
        $docblock = new Docblock();
        $var = VarTag::create()->setType('mixed')->setVariable('foo')->setDescription('bar');
        $docblock->appendTag($var);
        $this->assertEquals($expected, $docblock->toString());
    }
 /**
  * @param Node\Stmt\ClassMethod $node
  * @param string                $text
  *
  * @return string
  */
 public function addParams(Node\Stmt\ClassMethod $node, $text)
 {
     $docBlock = new Docblock($text);
     foreach ($node->getParams() as $param) {
         $type = $this->getFullyQualifiedParamType($param);
         if (null === $type) {
             continue;
         }
         $docBlock->appendTag(new ParamTag((string) $type . ' $' . $param->name));
     }
     return str_replace("* \n", "*\n", $docBlock->toString());
     //TODO remove once https://github.com/gossi/docblock/pull/2 is merged
 }
 /**
  * 
  * @param mixed $value
  * @return Docblock|null
  */
 private function toDocblock($value)
 {
     if (is_string($value)) {
         $value = Docblock::create()->setLongDescription($value);
     }
     return $value;
 }
Example #6
0
 /**
  * @param Node\Stmt\ClassMethod $node
  * @param string                $text
  *
  * @return string
  */
 public function addParams(Node\Stmt\ClassMethod $node, $text)
 {
     $docBlock = new Docblock($text);
     foreach ($node->getParams() as $param) {
         if ($param->type && $param->type instanceof Node\Name) {
             $type = $this->resolver->resolve(implode('\\', $param->type->parts), $this->context);
             $type = str_replace('\\\\', '\\', $type);
         } elseif ($param->type && is_string($param->type)) {
             $type = $param->type;
         }
         if (!isset($type)) {
             continue;
         }
         $docBlock->appendTag(new ParamTag((string) $type . ' $' . $param->name));
     }
     $string = $docBlock->toString();
     return str_replace("* \n", "*\n", $string);
     //TODO remove once https://github.com/gossi/docblock/pull/2 is merged
 }
 /**
  * {@inheritDoc}
  */
 public function generate(GenerateableInterface $model)
 {
     $content = "<?php\n";
     $comment = $this->config->getHeaderComment();
     if (!empty($comment)) {
         $docblock = new Docblock();
         $docblock->setLongDescription($comment);
         $content .= str_replace('/**', '/*', $docblock->toString()) . "\n";
     }
     if ($this->config->getHeaderDocblock() instanceof Docblock) {
         $content .= $this->config->getHeaderDocblock()->toString() . "\n";
     }
     if ($this->config->getDeclareStrictTypes()) {
         $content .= "declare(strict_types=1);\n\n";
     }
     $content .= parent::generate($model);
     if ($this->config->getBlankLineAtEnd()) {
         $content .= "\n";
     }
     return $content;
 }
 public function testEmptyDocblock()
 {
     $docblock = new Docblock();
     $this->assertEquals("/**\n */", $docblock->toString());
 }
 protected function visitDocblock(Docblock $docblock)
 {
     if (!$docblock->isEmpty() || $this->config->getGenerateEmptyDocblock()) {
         $this->writeDocblock($docblock);
     }
 }
 public function testEmpty()
 {
     $doc = new Docblock();
     $this->assertTrue($doc->isEmpty());
     $doc->setLongDescription('bla');
     $this->assertFalse($doc->isEmpty());
     $doc->setLongDescription(null);
     $this->assertTrue($doc->isEmpty());
     $doc->setShortDescription('bla');
     $this->assertFalse($doc->isEmpty());
     $doc->setShortDescription(null);
     $this->assertTrue($doc->isEmpty());
     $doc->appendTag(new SeeTag());
     $this->assertFalse($doc->isEmpty());
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $controller = $this->argument('controller');
     // création de la méthode
     $method = camel_case($this->argument('method') . '_' . $this->argument('name'));
     $method = PhpMethod::create($method);
     // PARAMS
     $this->generateParams($method);
     // BODY
     $body = '';
     $body .= $this->generateAcl();
     // TEMPLATE
     $template = camel_case('template_' . $this->option('template'));
     if (!method_exists($this, $template)) {
         exc('Impossible de trouver le template : ' . $template);
     }
     $body .= call_user_func([$this, $template]);
     $method->setBody($body);
     // DOCKBOCK
     $dockblock = new Docblock();
     $dockblock->appendTag(TagFactory::create('name', 'Artisan'));
     $dockblock->appendTag(TagFactory::create('see', 'php artisan ffmake:action'));
     $dockblock->appendTag(TagFactory::create('generated', Carbon::now()));
     $method->setDocblock($dockblock);
     // CONTROLLER
     $controller = ucfirst(camel_case($controller . '_controller'));
     $controller = new \ReflectionClass('App\\Http\\Controllers\\' . $controller);
     $class = PhpClass::fromReflection($controller)->setMethod($method);
     $class->setParentClassName('Controller');
     // fix la gestion des namespaec pour la parent class
     // GENERATION
     $generator = new CodeGenerator();
     $class = '<?php ' . $generator->generate($class);
     // inscription du code dans la classe
     file_put_contents($controller->getFileName(), $class);
     $this->info('Action generated dans le fichier : ' . $controller->getFileName());
 }