Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Commit

Permalink
Update PHP parser version to 4
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-thebaud committed Mar 2, 2019
1 parent f4c1ce7 commit 4e4aae8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -26,7 +26,7 @@
"doctrine/collections": "^1.5",
"doctrine/lexer": "^1.0",
"league/flysystem": "^1.0",
"nikic/php-parser": "^3.0",
"nikic/php-parser": "^4.0",
"psr/container": "^1.0.0",
"respect/validation": "^1.1",
"slim/php-view": "^2.2",
Expand Down
14 changes: 14 additions & 0 deletions src/Model/PropertyInterface/TypeInterface.php
Expand Up @@ -77,6 +77,20 @@ interface TypeInterface extends NodeInterface
*/
public const STRING = 8;

/**
* @var string[] RESERVED_TYPE_IDENTIFIERS The reserved words for types.
*/
public const RESERVED_TYPE_IDENTIFIERS = [
'object',
'bool',
'int',
'float',
'array',
'iterable',
'callable',
'string',
];

/**
* @var int VOID A void value (used for void return type).
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/NodeParser/GroupUseNodeParser.php
Expand Up @@ -43,7 +43,7 @@ public function invoke($node, NodeInterface $parent): void
$prefix = $node->prefix->toString();
foreach ($node->uses as $use) {
if ($this->validateType($node->type)) {
$parent->addUse($use->alias, $prefix . '\\' . $use->name->toString());
$parent->addUse($use->getAlias()->name, $prefix . '\\' . $use->name->toString());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/NodeParser/ParameterNodeParser.php
Expand Up @@ -64,7 +64,7 @@ public function invoke($node, NodeInterface $parent): void

$parameter = new ParameterModel();
$parameter->setParentNode($parent);
$parameter->setName($node->name);
$parameter->setName($node->var->name);
$parameter->setIsVariadic($node->variadic);

if ($node->type !== null) {
Expand Down
29 changes: 18 additions & 11 deletions src/Parser/NodeParser/TypeNodeParser.php
Expand Up @@ -49,8 +49,14 @@ public function invoke($node, NodeInterface $parent): void
$this->invoke($node->type, $parent);
return;
}
// Handle scalar type in identifier
if ($node instanceof Node\Identifier
&& Validator::contains($node->name)->validate(TypeInterface::RESERVED_TYPE_IDENTIFIERS)
) {
$node = $node->name;
}
// If it is a class like type
if ($node instanceof Node\Name) {
if ($node instanceof Node\Name || $node instanceof Node\Identifier) {
$parent->setType(TypeInterface::CUSTOM);
$parent->setCustomType($this->getClassType($node, $parent));
return;
Expand All @@ -67,23 +73,24 @@ public function invoke($node, NodeInterface $parent): void
/**
* Get the class type hint as a string.
*
* @param Node\Name $node The name node to parse.
* @param TypeInterface $parent The parent to update.
* @param Node\Name|Node\Identifier $node The name node to parse.
* @param TypeInterface $parent The parent to update.
*
* @return string The class type hint.
*/
private function getClassType(Node\Name $node, TypeInterface $parent): string
private function getClassType($node, TypeInterface $parent): string
{
$phpFile = RootRetrieverHelper::getRoot($parent);

switch (true) {
case $node->isFullyQualified():
$name = $node->toString();
break;
case $node instanceof Node\Identifier:
case $node->isRelative():
case $node->isUnqualified():
$name = $this->getUnqualifiedClassType($node, $phpFile);
break;
case $node->isFullyQualified():
$name = $node->toString();
break;
case $node->isQualified():
$name = $this->getQualifiedClassType($node, $phpFile);
break;
Expand All @@ -101,14 +108,14 @@ private function getClassType(Node\Name $node, TypeInterface $parent): string
}

/**
* Retrieve the class type hint when it is qualified.
* Retrieve the class type hint when it is unqualified.
*
* @param Node\Name $node The name node to parse.
* @param PhpFileModelInterface $phpFile The php file.
* @param Node\Name|Node\Identifier $node The name node to parse.
* @param PhpFileModelInterface $phpFile The php file.
*
* @return string The class type hint.
*/
private function getUnqualifiedClassType(Node\Name $node, PhpFileModelInterface $phpFile): string
private function getUnqualifiedClassType($node, PhpFileModelInterface $phpFile): string
{
$name = $node->toString();
if ($phpFile->hasUse($name)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/NodeParser/UseNodeParser.php
Expand Up @@ -41,7 +41,7 @@ public function invoke($node, NodeInterface $parent): void

if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) {
foreach ($node->uses as $use) {
$parent->addUse($use->alias, $use->name->toString());
$parent->addUse($use->getAlias()->name, $use->name->toString());
}
}
}
Expand Down

0 comments on commit 4e4aae8

Please sign in to comment.