Skip to content

Commit 7035815

Browse files
committed
Improve speed by using parsed nodes
Before we used the phpdoc context factory to create docblock context. But since this was parsing the code over any over again it was very slow. By using the nodes from the php-parser we are way faster. Thanks to @Ocramius who implemented this in Roave/BetterReflection.
1 parent 6913cd9 commit 7035815

File tree

10 files changed

+3562
-56
lines changed

10 files changed

+3562
-56
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: php
2-
php: [ 7.1, 7.2, 7.3, 7.4 nightly ]
2+
php: [ 7.1, 7.2, 7.3, nightly ]
33
sudo: false
44

55
env:

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"phpDocumentor\\": [
1515
"tests/component/",
1616
"tests/unit/phpDocumentor",
17-
"tests/mocks/"
17+
"tests/bench/"
1818
]
1919
}
2020
},
@@ -27,7 +27,7 @@
2727
},
2828
"require-dev": {
2929
"mockery/mockery": "~1.0",
30-
"mikey179/vfsStream": "~1.2"
30+
"mikey179/vfsstream": "~1.2"
3131
},
3232
"extra": {
3333
"branch-alias": {

composer.lock

Lines changed: 99 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phive.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
33
<phar name="phpstan" version="^0.9.1" installed="0.10.7" location="./tools/phpstan" copy="true"/>
4-
<phar name="phpunit" version="^6.0" installed="6.5.13" location="./tools/phpunit" copy="true"/>
4+
<phar name="phpunit" version="^7.0" installed="7.5.7" location="./tools/phpunit" copy="true"/>
5+
<phar name="phpbench" version="^0.16.9" installed="0.16.9" location="./tools/phpbench" copy="true"/>
56
</phive>

phpbench.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"bootstrap": "vendor/autoload.php",
3+
"path": "tests/bench",
4+
"extensions": [
5+
"PhpBench\\Extensions\\XDebug\\XDebugExtension"
6+
]
7+
}

src/phpDocumentor/Reflection/Php/Factory/File.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use phpDocumentor\Reflection\Php\StrategyContainer;
2727
use phpDocumentor\Reflection\Types\Context;
2828
use phpDocumentor\Reflection\Types\ContextFactory;
29+
use phpDocumentor\Reflection\Types\NamespaceNodeToContext;
2930
use PhpParser\Comment\Doc;
3031
use PhpParser\Node;
3132
use PhpParser\Node\Stmt\Class_ as ClassNode;
@@ -98,10 +99,7 @@ private function createFile(CreateCommand $command)
9899
$code = $file->getContents();
99100
$nodes = $this->nodesFactory->create($code);
100101

101-
$contextFactory = new ContextFactory();
102-
$context = $contextFactory->createForNamespace('\\', $code);
103-
104-
$docBlock = $this->createFileDocBlock(null, $command->getStrategies(), $context, $nodes);
102+
$docBlock = $this->createFileDocBlock(null, $command->getStrategies(), null, $nodes);
105103

106104
$result = new FileElement(
107105
$file->md5(),
@@ -110,18 +108,20 @@ private function createFile(CreateCommand $command)
110108
$docBlock
111109
);
112110

113-
$this->createElements(new Fqsen('\\'), $nodes, $result, $command->getStrategies());
111+
$this->createElements($nodes, $result, $command->getStrategies(), null);
114112

115113
return $result;
116114
}
117115

118116
/**
119117
* @param Node[] $nodes
120118
*/
121-
private function createElements(Fqsen $namespace, array $nodes, FileElement $file, StrategyContainer $strategies): void
122-
{
123-
$contextFactory = new ContextFactory();
124-
$context = $contextFactory->createForNamespace((string) $namespace, $file->getSource());
119+
private function createElements(
120+
array $nodes,
121+
FileElement $file,
122+
StrategyContainer $strategies,
123+
?Context $context
124+
): void {
125125
foreach ($nodes as $node) {
126126
switch (get_class($node)) {
127127
case ClassNode::class:
@@ -140,8 +140,9 @@ private function createElements(Fqsen $namespace, array $nodes, FileElement $fil
140140
$file->addInterface($interface);
141141
break;
142142
case NamespaceNode::class:
143+
$context = (new NamespaceNodeToContext())($node);
143144
$file->addNamespace($node->fqsen);
144-
$this->createElements($node->fqsen, $node->stmts, $file, $strategies);
145+
$this->createElements($node->stmts, $file, $strategies, $context);
145146
break;
146147
case TraitNode::class:
147148
$strategy = $strategies->findMatching($node);

0 commit comments

Comments
 (0)