/** * @test */ public function it_should_parse_params_with_no_type() { $comment = '/** * @param string $name * @param $__attrs * @param $__content string|null * @return string */'; $vars = DocComments::getParameters($comment); $this->assertEquals(['name' => 'string', '__attrs' => null, '__content' => ['string', 'null']], $vars); }
/** * @test */ public function it_should_return_null_if_no_header_found() { $file = '<?php $some = "PHP Code here"; // No header at all! ?> <div class="component Hello-World" <?= attrs($__attrs) ?>> <h1>Hello<br/> <small>my name is</small> </h1> <div class="content"> <?= $__content ?> </div> </div>'; $resource = fopen('php://memory', 'r+'); fputs($resource, $file); rewind($resource); $header = DocComments::getFileHeader($resource); $this->assertEquals(null, $header); @fclose($resource); }
/** * Initialize parameters array with reflection info. * Detect if there is $__content parameter -> is container * Detect if there is $__attrs parameter -> allow extra attributes */ protected function initialize() { $header = DocComments::getFileHeader($this->file); $vars = DocComments::getVars($header); $parameters = []; foreach ($vars as $name => $type) { if ($name === '__attrs') { $this->allowExtraParams = true; } elseif ($name === '__content') { $this->isContainer = true; } else { $parameters[$name] = $this->guessParameterDefinition($name, $type); } } $this->parameters = $parameters; }
/** * Initialize parameters array with reflection info. * Detect if there is $__content parameter -> is container * Detect if there is $__attrs parameter -> allow extra attributes */ protected function initialize() { $reflectionClass = new \ReflectionClass($this->class); $reflectionMethod = $reflectionClass->getMethod($this->method); $vars = DocComments::getParameters($reflectionMethod->getDocComment()); $this->parameters = []; $this->methodParameters = []; foreach ($reflectionMethod->getParameters() as $param) { $name = $param->getName(); $this->methodParameters[$name] = $param_def = new ComponentParameterDefinition($name, $this->guessParameterType($param, isset($vars[$name]) ? $vars[$name] : null) ?: 'mixed', $param->isDefaultValueAvailable(), $param->getDefaultValue()); if ($name === '__attrs') { $this->allowExtraParams = true; } elseif ($name === '__content') { $this->isContainer = true; } else { $this->parameters[$name] = $param_def; } } }
/** * @test */ public function it_should_parse_vars_of_collection_type() { $comment = '/** * @var \\Namespace\\Object[] $objects * @var $__attrs string[] * @return string */'; $vars = DocComments::getVars($comment); $this->assertEquals(['objects' => '\\Namespace\\Object[]', '__attrs' => 'string[]'], $vars); }