Skip to content
This repository has been archived by the owner on Feb 16, 2019. It is now read-only.

Fix parsing nullable types with php 7.1 #269

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions Sami/Parser/NodeVisitor.php
Expand Up @@ -14,7 +14,6 @@
use PhpParser\Node as AbstractNode;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node\Stmt as StmtNode;
use PhpParser\Node\Stmt\ClassConst as ClassConstNode;
use PhpParser\Node\Stmt\ClassMethod as ClassMethodNode;
use PhpParser\Node\Stmt\Class_ as ClassNode;
Expand All @@ -25,6 +24,7 @@
use PhpParser\Node\Stmt\TraitUse as TraitUseNode;
use PhpParser\Node\Stmt\Trait_ as TraitNode;
use PhpParser\Node\Stmt\Use_ as UseNode;
use PhpParser\Node\NullableType;
use Sami\Project;
use Sami\Reflection\ClassReflection;
use Sami\Reflection\ConstantReflection;
Expand Down Expand Up @@ -155,13 +155,25 @@ protected function addMethod(ClassMethodNode $node)
$parameter->setDefault($this->context->getPrettyPrinter()->prettyPrintExpr($param->default));
}

if ($type = (string) $param->type) {
if ($param->type instanceof FullyQualified && strpos($type, '\\') !== 0) {
$type = '\\'.$type;
}
$type = $param->type;
$typeStr = null;

if (is_string($param->type)) {
$type = $param->type;
$typeStr = (string) $param->type;
} elseif ($param->type instanceof NullableType) {
$type = $param->type->type;
$typeStr = (string) $param->type->type;
}

$parameter->setHint($this->resolveHint(array(array($type, false))));
if ($type instanceof FullyQualified && strpos($typeStr, '\\') !== 0) {
$typeStr = '\\'.$typeStr;
}

if ($typeStr !== null) {
$parameter->setHint($this->resolveHint(array(array($typeStr, false))));
}

$method->addParameter($parameter);
}

Expand Down