/** @test */ public function can_parse_param_docblocks() { $docblock = '/** @param Foo $foo */'; $this->assertEquals('Foo', DocblockParser::getParamType($docblock, '$foo')); $this->assertEquals(null, DocblockParser::getParamType($docblock, '$bar')); $docblock = '/** * @param Foo $foo * @param Bar $bar */'; $this->assertEquals('Foo', DocblockParser::getParamType($docblock, '$foo')); $this->assertEquals('Bar', DocblockParser::getParamType($docblock, '$bar')); }
private function guessType(Node $node) { // used for $this if ($node instanceof \PhpParser\Node\Stmt\Class_) { $className = $this->getNamespace() . '\\' . $node->name; $className = ltrim($className, '\\'); return $className; } if ($node instanceof \PhpParser\Node\Param) { $docblockType = $hintType = null; $reflFunc = $this->getReflectionFunction(); if ($reflFunc) { $docblock = $reflFunc->getDocComment(); } if (isset($docblock) && $docblock) { $docblockType = DocblockParser::getParamType($docblock, $node->name); if ($docblockType) { $docblockType = $this->parseDocblockType($docblockType); } } if ($node->type instanceof \PhpParser\Node\Name) { $hintType = $this->getClassName($node->type); } if ($hintType && $docblockType && !in_array($hintType, $docblockType)) { $msg = "@param docblock and type-hint mismatch for argument \${$node->name}"; $this->addError(new Error($msg, $node)); } return $hintType ?: $docblockType; } if ($node instanceof \PhpParser\Node\Expr\New_) { if ($node->class instanceof \PhpParser\Node\Name) { return $this->getClassName($node->class); } else { return 'object'; } } if ($node instanceof \PhpParser\Node\Expr\Variable) { $var = $this->getVariable($node->name); return $var ? $var->getType() : null; } if ($node instanceof \PhpParser\Node\Scalar\String_ || $node instanceof \PhpParser\Node\Scalar\Encapsed) { return 'string'; } if ($node instanceof \PhpParser\Node\Scalar\DNumber) { return 'float'; } if ($node instanceof \PhpParser\Node\Scalar\LNumber) { return 'integer'; } }