Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3b8f4d1
Introduced PHPBench with sample benchmark
dantleech Sep 1, 2017
9749722
Use 10 iterations
dantleech Sep 1, 2017
1108c88
Split benchmark into methods
dantleech Sep 2, 2017
c3940f0
Added phpbecnh to travis
dantleech Sep 2, 2017
461c17f
First brutally simple prototype of an implementation of docblock retu…
Ocramius Sep 2, 2017
3dd1bda
Simplfied `ReflectionFunctionAbstract#inNamespace()`
Ocramius Sep 2, 2017
c69a98e
Using inheritance rather than `instanceof` to decide which types are …
Ocramius Sep 2, 2017
9876e5c
Splitting out a new class `NamespaceNodeToReflectionTypeContext`
Ocramius Sep 2, 2017
d017749
Aligning test case to the new API of `/FindReturnType`
Ocramius Sep 2, 2017
293c25c
Testing aliased type resolution
Ocramius Sep 2, 2017
60dcce9
Removed unused imports
Ocramius Sep 2, 2017
b370f02
Prototyping around: replacing all usages of the `ContextFactory` with…
Ocramius Sep 2, 2017
52075e5
Fixing existing tests to support the new `NamespaceNodeToReflectionTy…
Ocramius Sep 2, 2017
b08f919
Covering aliased types in `FindPropertyType`
Ocramius Sep 2, 2017
ad25740
Covering aliased types in `FindParameterType`
Ocramius Sep 2, 2017
031a655
Automated CS fixes
Ocramius Sep 2, 2017
2785b7e
Basic testing around `NamespaceNodeToReflectionTypeContext`
Ocramius Sep 2, 2017
3b25659
Empty name => empty namespace
Ocramius Sep 2, 2017
a6ecd63
Basic testing around `NamespaceNodeToReflectionTypeContext` and names…
Ocramius Sep 2, 2017
bfb5ffa
Testing complex scenario with type resolutions, imported constants an…
Ocramius Sep 2, 2017
b182de0
Handling grouped use statements and excluding constant/function imports
Ocramius Sep 2, 2017
405cbea
Automatic CS fixes
Ocramius Sep 2, 2017
9ab5967
Adding docblock inspection for parameters and properties to the bench…
Ocramius Sep 2, 2017
aa0e676
Moved phpbench installation to a manual `composer require` to be exec…
Ocramius Sep 2, 2017
b530754
Removing `array_map` and `array_filter` api from extremely sensible A…
Ocramius Sep 2, 2017
c54d5c0
Revert "Removing `array_map` and `array_filter` api from extremely se…
Ocramius Sep 2, 2017
5257e60
Adding minor note about cryptic double-`array_merge([], ...foo)` call
Ocramius Sep 2, 2017
ff93dd1
Removing outdated TODO and comment
Ocramius Sep 2, 2017
8ae8292
Documenting newly introduced BC Breakages
Ocramius Sep 2, 2017
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
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ jobs:
- travis_retry composer require --dev --prefer-dist --prefer-stable phpstan/phpstan:^0.8.3
script: vendor/bin/phpstan analyse -l 5 -c phpstan.neon src

- stage: Run benchmarks
php: 7.1
env: DEPENDENCIES=""
before_script:
- travis_retry composer require --dev --prefer-dist --prefer-stable phpbench/phpbench:^0.13.0
script: ./vendor/bin/phpbench run --progress=dots --iterations=1

- stage: Upload Coverage
php: 7.1
env: DEPENDENCIES=""
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ $classInfo = ReflectionClass::createFromName('Foo\Bar\MyClass');
* `Roave\BetterReflection\Util\FindReflectionOnLine::buildDefaultFinder()` was dropped, please use the newly
introduced `Roave\BetterReflection\BetterReflection#findReflectionOnLine()` instead
* `Roave\BetterReflection\Reflection\Exception\PropertyNotPublic` was dropped, all properties are accessible now
* `Roave\BetterReflection\Reflection\ReflectionMethod::createFromNode()` has additional mandatory constructor arguments
* `Roave\BetterReflection\Reflection\ReflectionParameter::createFromNode()` has additional mandatory constructor arguments
* `Roave\BetterReflection\Reflection\ReflectionProperty::createFromNode()` has additional mandatory constructor arguments

## More documentation

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"autoload-dev": {
"psr-4": {
"Roave\\BetterReflectionTest\\": "test/unit"
"Roave\\BetterReflectionTest\\": "test/unit",
"Roave\\BetterReflectionBenchmark\\": "test/benchmark"
}
},
"suggest": {
Expand Down
4 changes: 4 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bootstrap": "vendor/autoload.php",
"path": "test/benchmark"
}
50 changes: 43 additions & 7 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property as PropertyNode;
use PhpParser\Node\Stmt\Trait_ as TraitNode;
use PhpParser\Node\Stmt\TraitUse;
Expand Down Expand Up @@ -43,7 +44,7 @@ class ReflectionClass implements Reflection, CoreReflector
private $reflector;

/**
* @var NamespaceNode
* @var NamespaceNode|null
*/
private $declaringNamespace;

Expand Down Expand Up @@ -335,15 +336,27 @@ function (ReflectionClass $ancestor) : array {
...\array_map(
function (ReflectionClass $trait) : array {
return \array_map(function (ReflectionMethod $method) use ($trait) : ReflectionMethod {
return ReflectionMethod::createFromNode($this->reflector, $method->getAst(), $trait, $this);
return ReflectionMethod::createFromNode(
$this->reflector,
$method->getAst(),
$this->declaringNamespace,
$trait,
$this
);
}, $trait->getMethods());
},
$this->getTraits()
)
),
\array_map(
function (ClassMethod $methodNode) : ReflectionMethod {
return ReflectionMethod::createFromNode($this->reflector, $methodNode, $this, $this);
return ReflectionMethod::createFromNode(
$this->reflector,
$methodNode,
$this->declaringNamespace,
$this,
$this
);
},
$this->node->getMethods()
)
Expand Down Expand Up @@ -415,7 +428,13 @@ public function getImmediateMethods(?int $filter = null) : array
/** @var \ReflectionMethod[] $methods */
$methods = \array_map(
function (ClassMethod $methodNode) : ReflectionMethod {
return ReflectionMethod::createFromNode($this->reflector, $methodNode, $this, $this);
return ReflectionMethod::createFromNode(
$this->reflector,
$methodNode,
$this->declaringNamespace,
$this,
$this
);
},
$this->node->getMethods()
);
Expand Down Expand Up @@ -649,7 +668,13 @@ public function getImmediateProperties(?int $filter = null) : array
$properties = [];
foreach ($this->node->stmts as $stmt) {
if ($stmt instanceof PropertyNode) {
$prop = ReflectionProperty::createFromNode($this->reflector, $stmt, $this, $this);
$prop = ReflectionProperty::createFromNode(
$this->reflector,
$stmt,
$this->declaringNamespace,
$this,
$this
);
$properties[$prop->getName()] = $prop;
}
}
Expand Down Expand Up @@ -703,8 +728,14 @@ function (ReflectionProperty $property) : bool {
),
...\array_map(
function (ReflectionClass $trait) use ($filter) {
return \array_map(function (ReflectionProperty $property) : ReflectionProperty {
return ReflectionProperty::createFromNode($this->reflector, $property->getAst(), $property->getDeclaringClass(), $this);
return \array_map(function (ReflectionProperty $property) use ($trait) : ReflectionProperty {
return ReflectionProperty::createFromNode(
$this->reflector,
$property->getAst(),
$trait->declaringNamespace,
$property->getDeclaringClass(),
$this
);
}, $trait->getProperties($filter));
},
$this->getTraits()
Expand Down Expand Up @@ -1362,6 +1393,11 @@ public function getAst() : ClassLikeNode
return $this->node;
}

public function getDeclaringNamespaceAst() : ?Namespace_
{
return $this->declaringNamespace;
}

/**
* Set whether this class is final or not
*
Expand Down
5 changes: 4 additions & 1 deletion src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,17 @@ function (ReflectionParameter $p) : bool {
public function getParameters() : array
{
$parameters = [];

foreach ($this->node->params as $paramIndex => $paramNode) {
$parameters[] = ReflectionParameter::createFromNode(
$this->reflector,
$paramNode,
$this->declaringNamespace,
$this,
$paramIndex
);
}

return $parameters;
}

Expand Down Expand Up @@ -432,7 +435,7 @@ public function returnsReference() : bool
*/
public function getDocBlockReturnTypes() : array
{
return (new FindReturnType())->__invoke($this);
return (new FindReturnType())->__invoke($this, $this->declaringNamespace);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Roave\BetterReflection\Reflection;

use PhpParser\Node\Stmt\ClassMethod as MethodNode;
use PhpParser\Node\Stmt\Namespace_;
use ReflectionMethod as CoreReflectionMethod;
use Roave\BetterReflection\Reflector\Reflector;
use RuntimeException;
Expand All @@ -23,22 +24,22 @@ class ReflectionMethod extends ReflectionFunctionAbstract
/**
* @param Reflector $reflector
* @param MethodNode $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
* @param Namespace_|null $namespace
* @param ReflectionClass $declaringClass
* @param ReflectionClass $implementingClass
*/
public static function createFromNode(
Reflector $reflector,
MethodNode $node,
?Namespace_ $namespace,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BC Break to be documented

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented at 8ae8292

ReflectionClass $declaringClass,
ReflectionClass $implementingClass
) : self {
$method = new self();
$method->declaringClass = $declaringClass;
$method->implementingClass = $implementingClass;

// Compat with core reflection means we should NOT pass namespace info
// for ReflectionMethod
$method->populateFunctionAbstract($reflector, $node, $declaringClass->getLocatedSource(), null);
$method->populateFunctionAbstract($reflector, $node, $declaringClass->getLocatedSource(), $namespace);

return $method;
}
Expand Down Expand Up @@ -182,6 +183,11 @@ private function getMethodNode() : MethodNode
return $this->getNode();
}

public function inNamespace() : bool
{
return false;
}

/**
* Is the method abstract.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Reflection/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Builder\Property as PropertyNodeBuilder;
use PhpParser\Node\Stmt\ClassLike as ClassLikeNode;
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property as PropertyNode;
use ReflectionObject as CoreReflectionObject;
use ReflectionProperty as CoreReflectionProperty;
Expand Down Expand Up @@ -139,14 +140,20 @@ private function getRuntimeProperties(?int $filter = null) : array
// the runtime-declared properties :/
$reflectionProperties = (new CoreReflectionObject($this->object))->getProperties();
$runtimeProperties = [];

foreach ($reflectionProperties as $property) {
if ($this->reflectionClass->hasProperty($property->getName())) {
continue;
}

$reflectionProperty = $this->reflectionClass->getProperty($property->getName());

$runtimeProperty = ReflectionProperty::createFromNode(
$this->reflector,
$this->createPropertyNodeFromReflection($property, $this->object),
$reflectionProperty
? $reflectionProperty->getDeclaringClass()->getDeclaringNamespaceAst()
: null,
$this,
$this,
false
Expand All @@ -156,6 +163,7 @@ private function getRuntimeProperties(?int $filter = null) : array
$runtimeProperties[$runtimeProperty->getName()] = $runtimeProperty;
}
}

return $runtimeProperties;
}

Expand Down Expand Up @@ -627,6 +635,14 @@ public function getAst() : ClassLikeNode
return $this->reflectionClass->getAst();
}

/**
* {@inheritdoc}
*/
public function getDeclaringNamespaceAst() : ?Namespace_
{
return $this->reflectionClass->getDeclaringNamespaceAst();
}

/**
* {@inheritdoc}
*/
Expand Down
29 changes: 20 additions & 9 deletions src/Reflection/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param as ParamNode;
use PhpParser\Node\Stmt\Namespace_;
use Reflector as CoreReflector;
use Roave\BetterReflection\NodeCompiler\CompileNodeToValue;
use Roave\BetterReflection\NodeCompiler\CompilerContext;
Expand All @@ -32,6 +33,11 @@ class ReflectionParameter implements CoreReflector
*/
private $node;

/**
* @var Namespace_|null
*/
private $declaringNamespace;

/**
* @var ReflectionFunctionAbstract
*/
Expand Down Expand Up @@ -187,23 +193,28 @@ public function __toString() : string
}

/**
* @param Reflector $reflector
* @param ParamNode $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
* @param Reflector $reflector
* @param ParamNode $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
* @param Namespace_|null $declaringNamespace namespace of the declaring function/method
* @param ReflectionFunctionAbstract $function
* @param int $parameterIndex
* @param int $parameterIndex
*
* @return ReflectionParameter
*/
public static function createFromNode(
Reflector $reflector,
ParamNode $node,
?Namespace_ $declaringNamespace,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BC Break to be documented

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented at 8ae8292

ReflectionFunctionAbstract $function,
int $parameterIndex
) : self {
$param = new self();
$param->reflector = $reflector;
$param->node = $node;
$param->function = $function;
$param->parameterIndex = $parameterIndex;
$param = new self();
$param->reflector = $reflector;
$param->node = $node;
$param->declaringNamespace = $declaringNamespace;
$param->function = $function;
$param->parameterIndex = $parameterIndex;

return $param;
}

Expand Down Expand Up @@ -402,7 +413,7 @@ public function getDocBlockTypeStrings() : array
*/
public function getDocBlockTypes() : array
{
return (new FindParameterType())->__invoke($this->function, $this->node);
return (new FindParameterType())->__invoke($this->function, $this->declaringNamespace, $this->node);
}

/**
Expand Down
Loading