Skip to content

Commit

Permalink
remove dependency package nikic/php-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyong committed Sep 6, 2018
1 parent 68ff872 commit eed87e2
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 111 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Install the\_silver\_searcher for quick searching:
Finally add the bin dir into the system $PATH variable:

$ git clone https://github.com/yaoguais/phpctags.vim.git
$ cd phpctags.vim && composer install
$ cd phpctags.vim && composer install --no-dev
$ export PATH=$(pwd)/bin:$PATH


Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
],
"type": "library",
"require": {
"nikic/php-parser": "^2.0|^3.0|^4.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/Parser/Namespace_.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public function parseType($types, $name, $namespace, $content, $line)

public function parseFunction($name, $namespace, $content, $line)
{
$types = [\PhpParser\Node\Stmt\Use_::TYPE_NORMAL, \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION];
$types = [Use_::TYPE_NORMAL, Use_::TYPE_FUNCTION];

return $this->parseType($types, $name, $namespace, $content, $line);
}

public function parseConst($name, $namespace, $content, $line)
{
$types = [\PhpParser\Node\Stmt\Use_::TYPE_CONSTANT];
$types = [Use_::TYPE_CONSTANT];

return $this->parseType($types, $name, $namespace, $content, $line);
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public function parseMethod($name, $class, $content, $line)
$alias = $parts[0];

$useFinder = new \PhpCTags\Finder\Use_();
$use = $useFinder->find($alias, [\PhpParser\Node\Stmt\Use_::TYPE_NORMAL], $content, $startLine, $line);
$use = $useFinder->find($alias, [Use_::TYPE_NORMAL], $content, $startLine, $line);

if (! $use) {
if (! $namespace) {
Expand Down
20 changes: 0 additions & 20 deletions src/Parser/Node.php

This file was deleted.

131 changes: 103 additions & 28 deletions src/Parser/Use_.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,120 @@

namespace PhpCTags\Parser;

class Use_ extends \PhpParser\NodeVisitorAbstract
class Use_
{
protected $uses;
const TYPE_NORMAL = 1;
const TYPE_FUNCTION = 2;
const TYPE_CONSTANT = 3;

public function parse($nodes)
public function parseToken($tokens)
{
$this->uses = [];
$traverser = new \PhpParser\NodeTraverser();
$traverser->addVisitor($this);
$traverser->traverse($nodes);
$uses = [];
$line = 1;
$raw = null;
$useLine = -1;
for ($i = 0, $l = count($tokens); $i < $l; ++$i) {
$token = $tokens[$i];
$name = is_array($token) ? $token[0] : null;
$data = is_array($token) ? $token[1] : $token;

return $this->uses;
}
if (\PhpCTags\Parser\Token::isNewLine($token)) {
++$line;
}

public function enterNode(\PhpParser\Node $node)
{
$class = get_class($node);
if ('PhpParser\Node\Stmt\Use_' == $class || 'PhpParser\Node\Stmt\GroupUse' == $class) {
$this->parseUse($node);
if (T_USE === $name) {
$raw = '';
$useLine = $line;
continue;
}

if (null !== $raw) {
if (';' === $data) {
foreach ($this->parseUse($raw, $useLine) as $alias => $us) {
foreach ($us as $u) {
$uses[$alias][] = $u;
}
}
$raw = null;
$useLine = -1;
} else {
$raw .= $data;
}
}
}

return $uses;
}

public function parseUse(\PhpParser\Node $node)
public function parseUse($raw, $line)
{
$line = $node->getAttribute('startLine');
$prefix = property_exists($node, 'prefix') ? implode('\\', $node->prefix->parts) : null;
foreach ($node->uses as $use) {
$parts = $use->name->parts;
if (! is_null($prefix)) {
array_unshift($parts, $prefix);
$uses = [];

$us = explode(',', $raw);
$fs = explode('{', array_shift($us));

$type = null;
$prefix = null;

$fns = preg_split('/\s/', $fs[0], -1, PREG_SPLIT_NO_EMPTY);
if (count($fns) > 1) {
$type = $this->parseType($fns[0]);
}
$fn = count($fs);
if ($fn > 1) {
$prefix = $fns[count($fns) - 1];
}
array_unshift($us, $fs[$fn - 1]);

foreach ($us as $u) {
$cs = preg_split('/\s/', $u, -1, PREG_SPLIT_NO_EMPTY);
$lt = array_pop($cs);
if ('}' != $lt) {
array_push($cs, $lt);
}
$name = implode('\\', $parts);
$alias = is_null($use->alias) ? array_pop($parts) :
(is_string($use->alias) ? $use->alias : $use->alias->name);
$key = strtolower($alias);
$type = \PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN != $use->type ? $use->type : $node->type;
if (\PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN != $type) {
$this->uses[$key][] = [$name, $type, $line];
$cn = count($cs);
if (1 == $cn) {
$name = $prefix ? $prefix.$cs[0] : $cs[0];
$pos = strrpos($name, '\\');
$alias = false === $pos ? strtolower($name) : strtolower(substr($name, $pos + 1));
$t = null !== $type ? $type : self::TYPE_NORMAL;
$uses[$alias][] = [$name, $t, $line];
} elseif (2 == $cn) {
$name = $prefix ? $prefix.$cs[1] : $cs[1];
$pos = strrpos($name, '\\');
$alias = false === $pos ? strtolower($name) : strtolower(substr($name, $pos + 1));
$t = $this->parseType($cs[0]);
$uses[$alias][] = [$name, $t, $line];
} elseif (3 == $cn) {
if ('as' !== $cs[1]) {
throw new \Exception("invalid use statement: $u");
}
$name = $prefix ? $prefix.$cs[0] : $cs[0];
$alias = strtolower($cs[2]);
$t = null !== $type ? $type : self::TYPE_NORMAL;
$uses[$alias][] = [$name, $t, $line];
} elseif (4 == $cn) {
if ('as' !== $cs[2]) {
throw new \Exception("invalid use statement: $u");
}
$name = $prefix ? $prefix.$cs[1] : $cs[1];
$alias = strtolower($cs[3]);
$t = $this->parseType($cs[0]);
$uses[$alias][] = [$name, $t, $line];
}
}

return $uses;
}

public function parseType($name)
{
if ('function' == $name) {
return self::TYPE_FUNCTION;
} elseif ('const' == $name) {
return self::TYPE_CONSTANT;
} else {
return self::TYPE_NORMAL;
}
}
}
26 changes: 0 additions & 26 deletions src/Pool/Node.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Pool/Use_.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function fromContent($content)
$key = md5($content);
if (! array_key_exists($key, $this->caches)) {
$parser = new \PhpCTags\Parser\Use_();
$nodes = Node::getInstance()->fromContent($content);
$this->caches[$key] = $parser->parse($nodes);
$tokens = Token::getInstance()->fromContent($content);
$this->caches[$key] = $parser->parseToken($tokens);
}

return $this->caches[$key];
Expand Down
62 changes: 32 additions & 30 deletions tests/Parser/UseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class UseTest extends \Tests\BaseTest
{
public function testParse()
public function testParseUse()
{
$cases = [
[
'<?php
use A\B\ClassC;',
[
'classc' => [['A\B\ClassC', \PhpParser\Node\Stmt\Use_::TYPE_NORMAL, 2]],
'classc' => [['A\B\ClassC', \PhpCTags\Parser\Use_::TYPE_NORMAL, 2]],
],
],
[
Expand All @@ -30,8 +30,8 @@ function bar() {
}',
[
'foo' => [
['A\foo', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 9],
['A\bar', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 12],
['A\foo', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 9],
['A\bar', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 12],
],
],
],
Expand All @@ -46,39 +46,41 @@ function bar() {
use function bar\math\sin3, bar\math\cos3, bar\math\cosh3;
use baz\math\{ Math4, const PI4, function sin4, function cos4, function cosh4 };',
[
'sin' => [['foo\math\sin', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 2]],
'cos' => [['foo\math\cos', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 2]],
'cosh' => [['foo\math\cosh', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 2]],
'pi' => [['foo\math\PI', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 3]],
'e' => [['foo\math\E', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 3]],
'gamma' => [['foo\math\GAMMA', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 3]],
'golden_ratio' => [['foo\math\GOLDEN_RATIO', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 3]],
'sin2as' => [['foo\math\sin2', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 4]],
'cos2' => [['foo\math\cos2', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 4]],
'cosh2' => [['foo\math\cosh2', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 4]],
'pi2' => [['foo\math\PI2', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 5]],
'e2' => [['foo\math\E2', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 5]],
'gamma2' => [['foo\math\GAMMA2', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 5]],
'golden_ratio2' => [['foo\math\GOLDEN_RATIO2', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 5]],
'math3' => [['bar\math\Math3', \PhpParser\Node\Stmt\Use_::TYPE_NORMAL, 6]],
'pi3' => [['bar\math\PI3', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 7]],
'sin3' => [['bar\math\sin3', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 8]],
'cos3' => [['bar\math\cos3', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 8]],
'cosh3' => [['bar\math\cosh3', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 8]],
'math4' => [['baz\math\Math4', \PhpParser\Node\Stmt\Use_::TYPE_NORMAL, 9]],
'pi4' => [['baz\math\PI4', \PhpParser\Node\Stmt\Use_::TYPE_CONSTANT, 9]],
'sin4' => [['baz\math\sin4', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 9]],
'cos4' => [['baz\math\cos4', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 9]],
'cosh4' => [['baz\math\cosh4', \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION, 9]],
'sin' => [['foo\math\sin', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 2]],
'cos' => [['foo\math\cos', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 2]],
'cosh' => [['foo\math\cosh', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 2]],
'pi' => [['foo\math\PI', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 3]],
'e' => [['foo\math\E', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 3]],
'gamma' => [['foo\math\GAMMA', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 3]],
'golden_ratio' => [['foo\math\GOLDEN_RATIO', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 3]],
'sin2as' => [['foo\math\sin2', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 4]],
'cos2' => [['foo\math\cos2', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 4]],
'cosh2' => [['foo\math\cosh2', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 4]],
'pi2' => [['foo\math\PI2', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 5]],
'e2' => [['foo\math\E2', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 5]],
'gamma2' => [['foo\math\GAMMA2', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 5]],
'golden_ratio2' => [['foo\math\GOLDEN_RATIO2', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 5]],
'math3' => [['bar\math\Math3', \PhpCTags\Parser\Use_::TYPE_NORMAL, 6]],
'pi3' => [['bar\math\PI3', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 7]],
'sin3' => [['bar\math\sin3', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 8]],
'cos3' => [['bar\math\cos3', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 8]],
'cosh3' => [['bar\math\cosh3', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 8]],
'math4' => [['baz\math\Math4', \PhpCTags\Parser\Use_::TYPE_NORMAL, 9]],
'pi4' => [['baz\math\PI4', \PhpCTags\Parser\Use_::TYPE_CONSTANT, 9]],
'sin4' => [['baz\math\sin4', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 9]],
'cos4' => [['baz\math\cos4', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 9]],
'cosh4' => [['baz\math\cosh4', \PhpCTags\Parser\Use_::TYPE_FUNCTION, 9]],
],
],
];
foreach ($cases as $i => $case) {
$parser = new \PhpCTags\Parser\Use_();
$nodeParser = new \PhpCTags\Parser\Node();
$tokenParser = new \PhpCTags\Parser\Token();
$tokens = $tokenParser->parse($case[0]);

$this->assertEquals(
$case[1],
$parser->parse($nodeParser->parse($case[0])),
$parser->parseToken($tokens),
"case #$i"
);
}
Expand Down

0 comments on commit eed87e2

Please sign in to comment.