Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PHP-Parser 4.0 #104

Merged
merged 1 commit into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for PHP-Parser 4.0
  • Loading branch information
nikic committed Mar 11, 2018
commit e318bf1f0166b70832b61986a2140c05d1a02f28
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ matrix:
env: PARSER_VERSION=^2.0
- php: 7.0
env: PARSER_VERSION=^3.0
- php: 7.0
env: PARSER_VERSION=^4.0
- php: 7.1
env: PARSER_VERSION=^3.0
- php: 7.1
env: PARSER_VERSION=^4.0
- php: hhvm
env: PARSER_VERSION=^1.2
dist: trusty
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": ">=5.4",
"nikic/php-parser": "^1.2|^2.0|^3.0",
"nikic/php-parser": "^1.2|^2.0|^3.0|^4.0",
"symfony/polyfill-php56": "^1.0"
},
"require-dev": {
Expand Down
9 changes: 8 additions & 1 deletion src/Analyzer/AstAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\NodeTraverser;
use PhpParser\PrettyPrinter\Standard as NodePrinter;
use PhpParser\Error as ParserError;
use PhpParser\Node\Expr\Variable as VariableNode;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser as CodeParser;
use PhpParser\ParserFactory;
Expand Down Expand Up @@ -92,7 +93,13 @@ protected function determineContext(array &$data)
if ($node->byRef) {
$refs++;
}
return $node->var;
if ($node->var instanceof VariableNode) {
// For PHP-Parser >=4.0
return $node->var->name;
} else {
// For PHP-Parser <4.0
return $node->var;
}
}, $data['ast']->uses);
$data['hasRefs'] = ($refs > 0);

Expand Down
8 changes: 4 additions & 4 deletions src/Analyzer/Visitor/ClosureLocatorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ public function enterNode(AstNode $node)
// Determine information about the closure's location
if (!$this->closureNode) {
if ($node instanceof NamespaceNode) {
$namespace = ($node->name && is_array($node->name->parts))
? implode('\\', $node->name->parts)
$namespace = $node->name !== null
? $node->name->toString()
: null;
$this->location['namespace'] = $namespace;
}
if ($node instanceof TraitNode) {
$this->location['trait'] = $node->name;
$this->location['trait'] = (string) $node->name;
$this->location['class'] = null;
} elseif ($node instanceof ClassNode) {
$this->location['class'] = $node->name;
$this->location['class'] = (string) $node->name;
$this->location['trait'] = null;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Analyzer/Visitor/MagicConstantVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public function leaveNode(AstNode $node)
{
switch ($node->getType()) {
case 'Scalar_MagicConst_Class' :
return new StringNode($this->location['class']);
return new StringNode($this->location['class'] ?: '');
case 'Scalar_MagicConst_Dir' :
return new StringNode($this->location['directory']);
return new StringNode($this->location['directory'] ?: '');
case 'Scalar_MagicConst_File' :
return new StringNode($this->location['file']);
return new StringNode($this->location['file'] ?: '');
case 'Scalar_MagicConst_Function' :
return new StringNode($this->location['function']);
return new StringNode($this->location['function'] ?: '');
case 'Scalar_MagicConst_Line' :
return new NumberNode($node->getAttribute('startLine'));
return new NumberNode($node->getAttribute('startLine') ?: 0);
case 'Scalar_MagicConst_Method' :
return new StringNode($this->location['method']);
return new StringNode($this->location['method'] ?: '');
case 'Scalar_MagicConst_Namespace' :
return new StringNode($this->location['namespace']);
return new StringNode($this->location['namespace'] ?: '');
case 'Scalar_MagicConst_Trait' :
return new StringNode($this->location['trait']);
return new StringNode($this->location['trait'] ?: '');
}
}
}