Skip to content

Commit 73703c2

Browse files
committed
Improve argument handling
1 parent e3f759e commit 73703c2

13 files changed

+181
-19
lines changed

TODO

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ Project2: \Luigi\Pizza\Valued::BASE_PRICE his namespace is set to \Luigi
3232

3333
Checks to add to example:
3434

35-
1. Variadic arguments
35+
1. Variadic arguments
36+
2. Arguments are not checked in component tests
37+
3. Check why the MethodAssembler has to set the analyzer; shouldn't that be somewhere else?
38+
39+
Things to refactor:
40+
41+
- Caching

docs/filtering.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Filtering
2-
=========
2+
=========
3+
4+
.. info:: The contents for this page are planned but need to be written, please come back later to check for this

docs/incremental-updates.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Incremental updates
22
===================
3+
4+
.. info:: The contents for this page are planned but need to be written, please come back later to check for this

docs/inspecting.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Inspecting
22
==========
33

4+
.. info:: The contents for this page are planned but need to be written, please come back later to check for this
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Integrating with Silex and Cilex
22
================================
33

4+
.. info:: The contents for this page are planned but need to be written, please come back later to check for this

docs/usage.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Usage
22
=====
33

4+
.. info:: The contents for this page are planned but need to be written, please come back later to check for this

src/phpDocumentor/Descriptor/Builder/PhpParser/MethodAssembler.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace phpDocumentor\Descriptor\Builder\PhpParser;
1313

1414
use phpDocumentor\Descriptor\ArgumentDescriptor;
15+
use phpDocumentor\Descriptor\Collection as DescriptorCollection;
16+
use phpDocumentor\Descriptor\Tag\ParamDescriptor;
1517
use phpDocumentor\Reflection\DocBlock\Type\Collection;
1618
use phpDocumentor\Descriptor\MethodDescriptor;
1719
use phpDocumentor\Reflection\ClassReflector\MethodReflector;
@@ -52,8 +54,13 @@ public function create($data)
5254

5355
$this->mapNodeToDescriptor($data, $methodDescriptor);
5456

57+
if ($methodDescriptor->getName() == 'calculatePrice') {
58+
// var_dump($methodDescriptor->getName());
59+
// var_dump($data->params);
60+
}
61+
5562
$this->addArguments($data, $methodDescriptor);
56-
// $this->addVariadicArgument($data, $methodDescriptor);
63+
$this->addVariadicArgument($methodDescriptor);
5764

5865
return $methodDescriptor;
5966
}
@@ -124,33 +131,27 @@ protected function addArgument(Param $argument, MethodDescriptor $descriptor)
124131
* Checks if there is a variadic argument in the `@param` tags and adds it to the list of Arguments in
125132
* the Descriptor unless there is already one present.
126133
*
127-
* @param MethodReflector $data
128134
* @param MethodDescriptor $methodDescriptor
129135
*
130136
* @return void
131137
*/
132-
protected function addVariadicArgument($data, $methodDescriptor)
138+
protected function addVariadicArgument($methodDescriptor)
133139
{
134-
if (!$data->getDocBlock()) {
135-
return;
136-
}
137-
138-
$paramTags = $data->getDocBlock()->getTagsByName('param');
140+
/** @var DescriptorCollection $paramTags */
141+
$paramTags = $methodDescriptor->getParam();
139142

140-
/** @var ParamTag $lastParamTag */
141-
$lastParamTag = end($paramTags);
143+
/** @var ParamDescriptor $lastParamTag */
144+
$lastParamTag = $paramTags->end();
142145
if (!$lastParamTag) {
143146
return;
144147
}
145148

146149
if ($lastParamTag->isVariadic()
147150
&& !in_array($lastParamTag->getVariableName(), array_keys($methodDescriptor->getArguments()->getAll()))
148151
) {
149-
$types = $this->analyzer->analyze(new Collection($lastParamTag->getTypes()));
150-
151152
$argument = new ArgumentDescriptor();
152153
$argument->setName($lastParamTag->getVariableName());
153-
$argument->setTypes($types);
154+
$argument->setTypes($lastParamTag->getTypes());
154155
$argument->setDescription($lastParamTag->getDescription());
155156
$argument->setLine($methodDescriptor->getLine());
156157
$argument->setVariadic(true);

src/phpDocumentor/Descriptor/Builder/Reflector/MethodAssembler.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ protected function addVariadicArgument($data, $methodDescriptor)
132132
return;
133133
}
134134

135+
// convert ParamTag into ParamDescriptor
136+
$lastParamTag = $this->analyzer->analyze($lastParamTag);
137+
135138
if ($lastParamTag->isVariadic()
136139
&& !in_array($lastParamTag->getVariableName(), array_keys($methodDescriptor->getArguments()->getAll()))
137140
) {
138-
$types = $this->analyzer->analyze(new Collection($lastParamTag->getTypes()));
139-
140141
$argument = new ArgumentDescriptor();
141142
$argument->setName($lastParamTag->getVariableName());
142-
$argument->setTypes($types);
143+
$argument->setTypes($lastParamTag->getTypes());
143144
$argument->setDescription($lastParamTag->getDescription());
144145
$argument->setLine($methodDescriptor->getLine());
145146
$argument->setVariadic(true);

src/phpDocumentor/Descriptor/Builder/Reflector/Tags/ParamAssembler.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ public function create($data)
3535
{
3636
$descriptor = new ParamDescriptor($data->getName());
3737
$descriptor->setDescription($data->getDescription());
38-
$descriptor->setVariableName($data->getVariableName());
38+
$variableName = $data->getVariableName();
39+
if (substr($variableName, 1, 3) == '...') {
40+
$variableName = '$' . substr($variableName, 4);
41+
$descriptor->setIsVariadic(true);
42+
}
43+
$descriptor->setVariableName($variableName);
3944

4045
/** @var Collection $types */
4146
$types = $this->analyzer->analyze(new Collection($data->getTypes()));

src/phpDocumentor/Descriptor/Collection.php

+5
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,9 @@ public function merge(Collection $collection)
196196
{
197197
return new Collection(array_merge($this->items, $collection->getAll()));
198198
}
199+
200+
public function end()
201+
{
202+
return end($this->items);
203+
}
199204
}

src/phpDocumentor/Descriptor/Tag/ParamDescriptor.php

+24
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,28 @@
1616
*/
1717
class ParamDescriptor extends BaseTypes\TypedVariableAbstract
1818
{
19+
/** @var bool */
20+
private $isVariadic = false;
21+
22+
/**
23+
* Returns whether this is a variadic parameter.
24+
*
25+
* @return boolean
26+
*/
27+
public function isVariadic()
28+
{
29+
return $this->isVariadic;
30+
}
31+
32+
/**
33+
* Registers whether this is a variadic parameter.
34+
*
35+
* @param boolean $isVariadic
36+
*
37+
* @return void
38+
*/
39+
public function setIsVariadic($isVariadic)
40+
{
41+
$this->isVariadic = $isVariadic;
42+
}
1943
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* phpDocumentor
4+
*
5+
* PHP Version 5.3
6+
*
7+
* @copyright 2010-2013 Mike van Riel / Naenius (http://www.naenius.com)
8+
* @license http://www.opensource.org/licenses/mit-license.php MIT
9+
* @link http://phpdoc.org
10+
*/
11+
12+
namespace phpDocumentor\Descriptor;
13+
14+
class CreateArgumentTest extends BaseComponentTestCase
15+
{
16+
/**
17+
* @dataProvider switchBetweenStrategies
18+
*/
19+
public function testArgumentCanHaveName($projectDescriptor, $filename)
20+
{
21+
$this->projectDescriptor = $projectDescriptor;
22+
$this->filename = $filename;
23+
24+
$method = $this->getMethodWithName('\Luigi\Pizza\PizzaComponentFactory', 'calculatePrice');
25+
26+
$firstArgument = current($method->getArguments()->getAll());
27+
$this->assertSame('$additionalPrices', $firstArgument->getName());
28+
}
29+
30+
/**
31+
* @dataProvider switchBetweenStrategies
32+
*/
33+
public function testArgumentCanHaveDescription($projectDescriptor, $filename)
34+
{
35+
$this->projectDescriptor = $projectDescriptor;
36+
$this->filename = $filename;
37+
38+
$method = $this->getMethodWithName('\Luigi\Pizza\PizzaComponentFactory', 'calculatePrice');
39+
40+
$firstArgument = current($method->getArguments()->getAll());
41+
$this->assertSame('Additional costs may be passed', $firstArgument->getDescription());
42+
}
43+
44+
/**
45+
* @dataProvider switchBetweenStrategies
46+
*/
47+
public function testArgumentCanHaveType($projectDescriptor, $filename)
48+
{
49+
$this->markTestIncomplete();
50+
}
51+
52+
/**
53+
* @dataProvider switchBetweenStrategies
54+
*/
55+
public function testArgumentCanHaveDefault($projectDescriptor, $filename)
56+
{
57+
$this->projectDescriptor = $projectDescriptor;
58+
$this->filename = $filename;
59+
60+
$methodWithArgWithDefault = $this->getMethodWithName('\Luigi\Pizza', 'setSize');
61+
$firstArgument = current($methodWithArgWithDefault->getArguments()->getAll());
62+
$this->assertSame('\Luigi\Pizza\SIZE_20CM', $firstArgument->getDefault());
63+
64+
$methodWithArgWithoutDefault = $this->getMethodWithName('\Luigi\Pizza\PizzaComponentFactory', 'calculatePrice');
65+
$firstArgument = current($methodWithArgWithoutDefault->getArguments()->getAll());
66+
$this->assertNull($firstArgument->getDefault());
67+
}
68+
69+
/**
70+
* @dataProvider switchBetweenStrategies
71+
*/
72+
public function testArgumentCanBeVariadic($projectDescriptor, $filename)
73+
{
74+
$this->projectDescriptor = $projectDescriptor;
75+
$this->filename = $filename;
76+
77+
$methodWithVariadic = $this->getMethodWithName('\Luigi\Pizza\PizzaComponentFactory', 'calculatePrice');
78+
$firstArgument = current($methodWithVariadic->getArguments()->getAll());
79+
$this->assertTrue($firstArgument->isVariadic());
80+
81+
$methodWithoutVariadic = $this->getMethodWithName('\Luigi\Pizza', 'setSize');
82+
$firstArgument = current($methodWithoutVariadic->getArguments()->getAll());
83+
$this->assertFalse($firstArgument->isVariadic());
84+
}
85+
86+
/**
87+
* @dataProvider switchBetweenStrategies
88+
*/
89+
public function testArgumentCanBeByReference($projectDescriptor, $filename)
90+
{
91+
$this->projectDescriptor = $projectDescriptor;
92+
$this->filename = $filename;
93+
94+
$methodWithArgByRef = $this->getMethodWithName('\Luigi\Pizza', 'setSize');
95+
$firstArgument = current($methodWithArgByRef->getArguments()->getAll());
96+
$this->assertTrue($firstArgument->isByReference());
97+
98+
$methodWithArgNotByRef = $this->getMethodWithName('\Luigi\Pizza\PizzaComponentFactory', 'calculatePrice');
99+
$firstArgument = current($methodWithArgNotByRef->getArguments()->getAll());
100+
$this->assertFalse($firstArgument->isByReference());
101+
}
102+
}

tests/example.file.php

+11
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public function add()
9696
{
9797
}
9898

99+
/**
100+
* Calculates the price for this specific component.
101+
*
102+
* @param float[] $...additionalPrices Additional costs may be passed
103+
*
104+
* @return float
105+
*/
99106
abstract protected function calculatePrice();
100107
}
101108

@@ -256,6 +263,10 @@ final public function addTopping(Pizza\Topping $topping)
256263
$this->toppings[] = $topping;
257264
}
258265

266+
public function setSize(&$size = \Luigi\Pizza\SIZE_20CM)
267+
{
268+
}
269+
259270
public function getPrice()
260271
{
261272
}

0 commit comments

Comments
 (0)