Skip to content

Commit ec92571

Browse files
committed
move some logic into private methods in order to do closer type checking
1 parent 1462915 commit ec92571

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src/TypeResolver.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ private function resolveTypedObject($type, Context $context = null)
398398
*
399399
* @param \ArrayIterator $tokens
400400
* @param Type $classType
401-
* @param Context|null $context
401+
* @param Context $context
402402
* @return Array_|Collection
403403
*/
404-
private function resolveCollection(\ArrayIterator $tokens, Type $classType, Context $context = null) {
404+
private function resolveCollection(\ArrayIterator $tokens, Type $classType, Context $context) {
405405

406406
$isArray = ('array' == (string) $classType);
407407

@@ -419,7 +419,7 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
419419
$keyType = null;
420420

421421
if ($tokens->current() == ',') {
422-
// if we have a coma, then we just parsed the key type, not the value type
422+
// if we have a comma, then we just parsed the key type, not the value type
423423
$keyType = $valueType;
424424
if ($isArray) {
425425
// check the key type for an "array" collection. We allow only
@@ -463,8 +463,20 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
463463
if ($isArray) {
464464
return new Array_($valueType, $keyType);
465465
}
466-
else {
467-
return new Collection($classType->getFqsen(), $valueType, $keyType);
466+
467+
if ($classType instanceof Object_) {
468+
return $this->makeCollectionFromObject($classType, $valueType, $keyType);
468469
}
469470
}
471+
472+
/**
473+
* @param Object_ $object
474+
* @param Type $valueType
475+
* @param Type|null $keyType
476+
* @return Collection
477+
*/
478+
private function makeCollectionFromObject(Object_ $object, Type $valueType, Type $keyType = null)
479+
{
480+
return new Collection($object->getFqsen(), $valueType, $keyType);
481+
}
470482
}

src/Types/Collection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@
2929
final class Collection extends AbstractList
3030
{
3131

32-
/** @var Fqsen */
32+
/** @var Fqsen|null */
3333
private $fqsen;
3434

3535
/**
3636
* Initializes this representation of an array with the given Type or Fqsen.
3737
*
38+
* @param \phpDocumentor\Reflection\Fqsen|null $fqsen
3839
* @param Type $valueType
3940
* @param Type $keyType
4041
*/
41-
public function __construct(Fqsen $fqsen, Type $valueType, Type $keyType = null)
42+
public function __construct(Fqsen $fqsen = null, Type $valueType, Type $keyType = null)
4243
{
4344
parent::__construct($valueType, $keyType);
4445

@@ -48,7 +49,7 @@ public function __construct(Fqsen $fqsen, Type $valueType, Type $keyType = null)
4849
/**
4950
* Returns the FQSEN associated with this object.
5051
*
51-
* @return Fqsen
52+
* @return Fqsen|null
5253
*/
5354
public function getFqsen()
5455
{

src/Types/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class Context
4444
public function __construct($namespace, array $namespaceAliases = [])
4545
{
4646
$this->namespace = ('global' !== $namespace && 'default' !== $namespace)
47-
? trim((string)$namespace, '\\')
47+
? trim($namespace, '\\')
4848
: '';
4949

5050
foreach ($namespaceAliases as $alias => $fqnn) {

src/Types/ContextFactory.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,32 @@ final class ContextFactory
4040
*/
4141
public function createFromReflector(\Reflector $reflector)
4242
{
43-
if (method_exists($reflector, 'getDeclaringClass')) {
44-
$reflector = $reflector->getDeclaringClass();
43+
if ($reflector instanceof \ReflectionMethod) {
44+
return $this->createFromReflectionMethod($reflector);
4545
}
4646

47-
$fileName = $reflector->getFileName();
48-
$namespace = $reflector->getNamespaceName();
47+
if ($reflector instanceof \ReflectionClass) {
48+
return $this->createFromReflectionClass($reflector);
49+
}
50+
}
51+
52+
/**
53+
* @param \ReflectionMethod $method
54+
* @return Context
55+
*/
56+
private function createFromReflectionMethod(\ReflectionMethod $method)
57+
{
58+
return $this->createFromReflectionClass($method->getDeclaringClass());
59+
}
60+
61+
/**
62+
* @param \ReflectionClass $class
63+
* @return Context
64+
*/
65+
private function createFromReflectionClass(\ReflectionClass $class)
66+
{
67+
$fileName = $class->getFileName();
68+
$namespace = $class->getNamespaceName();
4969

5070
if (is_string($fileName) && file_exists($fileName)) {
5171
return $this->createForNamespace($namespace, file_get_contents($fileName));
@@ -177,7 +197,7 @@ private function skipToNextStringOrNamespaceSeparator(\ArrayIterator $tokens)
177197
*
178198
* @param \ArrayIterator $tokens
179199
*
180-
* @return string
200+
* @return array
181201
*/
182202
private function extractUseStatement(\ArrayIterator $tokens)
183203
{

0 commit comments

Comments
 (0)