Skip to content

Commit 476f62b

Browse files
authored
Added generics to Context (#22)
1 parent e5c8b97 commit 476f62b

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/Barryvdh/Reflection/DocBlock/Context.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class Context
2929

3030
/** @var string Name of the structural element, within the namespace. */
3131
protected $lsen = '';
32-
32+
33+
/** @var string[] List of generics */
34+
protected $generics = array();
35+
3336
/**
3437
* Cteates a new context.
3538
* @param string $namespace The namespace where this DocBlock
@@ -42,13 +45,15 @@ class Context
4245
public function __construct(
4346
$namespace = '',
4447
array $namespace_aliases = array(),
45-
$lsen = ''
48+
$lsen = '',
49+
array $generics = array()
4650
) {
4751
if (!empty($namespace)) {
4852
$this->setNamespace($namespace);
4953
}
5054
$this->setNamespaceAliases($namespace_aliases);
5155
$this->setLSEN($lsen);
56+
$this->setGenerics($generics);
5257
}
5358

5459
/**
@@ -76,6 +81,16 @@ public function getLSEN()
7681
{
7782
return $this->lsen;
7883
}
84+
85+
/**
86+
* Returns the list of generics.
87+
*
88+
* @return string[] List of generics
89+
*/
90+
public function getGenerics()
91+
{
92+
return $this->generics;
93+
}
7994

8095
/**
8196
* Sets a new namespace.
@@ -151,4 +166,17 @@ public function setLSEN($lsen)
151166
$this->lsen = (string)$lsen;
152167
return $this;
153168
}
169+
170+
/**
171+
* Sets a new list of generics.
172+
*
173+
* @param string[] $generics The new list of generics.
174+
*
175+
* @return $this
176+
*/
177+
public function setGenerics(array $generics)
178+
{
179+
$this->generics = $generics;
180+
return $this;
181+
}
154182
}

src/Barryvdh/Reflection/DocBlock/Type/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __construct(
7878
array $generics = array()
7979
) {
8080
$this->context = null === $context ? new Context() : $context;
81-
$this->generics = $generics;
81+
$this->generics = array_merge($this->context->getGenerics(), $generics);
8282

8383
foreach ($types as $type) {
8484
$this->add($type);

tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function testAddWithGenerics($fixture, $expected)
157157
{
158158
$collection = new Collection(
159159
array(),
160-
new Context('\My\Space', array('Alias' => '\My\Space\Aliasing')),
160+
new Context('\My\Space', array('Alias' => '\My\Space\Aliasing'), '', array('TParent')),
161161
array('TValue')
162162
);
163163
$collection->add($fixture);
@@ -344,6 +344,7 @@ public function provideTypesToExpandWithGenerics($method, $namespace = '\My\Spac
344344
array('TValue', array('TValue')),
345345
array('TValue[]', array('TValue[]')),
346346
array('TValue|DocBlock', array('TValue', $namespace . 'DocBlock')),
347+
array('TParent', array('TParent')),
347348
);
348349
}
349350
}

0 commit comments

Comments
 (0)