Skip to content

Commit b488642

Browse files
authored
Merge pull request #132 from jaapio/feature/single-type-arguments
Arguments are now single typed
2 parents 87e3801 + f7c34b0 commit b488642

File tree

8 files changed

+58
-42
lines changed

8 files changed

+58
-42
lines changed

phive.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
3-
<phar name="phpstan" version="^0.9.1" installed="0.10.2" location="./tools/phpstan" copy="true"/>
4-
<phar name="phpunit" version="^6.0" installed="6.5.5" location="./tools/phpunit" copy="true"/>
3+
<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"/>
55
</phive>

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
parameters:
22
ignoreErrors:
3+
4+
- '#Method phpDocumentor\\Reflection\\File\\LocalFile::\md5\(\) should return string but returns string\|false\.#'
35
#
46
# all these $fqsen errors indicate the need for a decorator class around PhpParser\Node to hold the public $fqsen that Reflection is giving it)
57
#

src/phpDocumentor/Reflection/File/LocalFile.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ final class LocalFile implements File
3232
*/
3333
public function __construct(string $path)
3434
{
35+
if (!file_exists($path)) {
36+
throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $path));
37+
}
38+
3539
$this->path = $path;
3640
}
3741

src/phpDocumentor/Reflection/Php/Argument.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace phpDocumentor\Reflection\Php;
1616

17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\Mixed_;
19+
1720
/**
1821
* Descriptor representing a single Argument of a method or function.
1922
*/
@@ -22,37 +25,42 @@ final class Argument
2225
/**
2326
* @var string name of the Argument
2427
*/
25-
private $name = null;
28+
private $name;
2629

2730
/**
28-
* @var mixed[] an array of normalized types that should be in this Argument
31+
* @var Type a normalized type that should be in this Argument
2932
*/
30-
private $types = [];
33+
private $type;
3134

3235
/**
3336
* @var string|null the default value for an argument or null if none is provided
3437
*/
35-
private $default = null;
38+
private $default;
3639

3740
/**
3841
* @var bool whether the argument passes the parameter by reference instead of by value
3942
*/
40-
private $byReference = false;
43+
private $byReference;
4144

4245
/**
4346
* @var boolean Determines if this Argument represents a variadic argument
4447
*/
45-
private $isVariadic = false;
48+
private $isVariadic;
4649

4750
/**
4851
* Initializes the object.
4952
*/
50-
public function __construct(string $name, ?string $default = null, bool $byReference = false, bool $isVariadic = false)
53+
public function __construct(string $name, ?Type $type = null, ?string $default = null, bool $byReference = false, bool $isVariadic = false)
5154
{
5255
$this->name = $name;
5356
$this->default = $default;
5457
$this->byReference = $byReference;
5558
$this->isVariadic = $isVariadic;
59+
if ($type === null) {
60+
$type = new Mixed_();
61+
}
62+
63+
$this->type = $type;
5664
}
5765

5866
/**
@@ -63,21 +71,9 @@ public function getName(): string
6371
return $this->name;
6472
}
6573

66-
/**
67-
* @return mixed[]
68-
*/
69-
public function getTypes(): array
70-
{
71-
return $this->types;
72-
}
73-
74-
/**
75-
* Add a type.
76-
* @param mixed $type
77-
*/
78-
public function addType($type): void
74+
public function getType(): ?Type
7975
{
80-
$this->types[] = $type;
76+
return $this->type;
8177
}
8278

8379
public function getDefault(): ?string

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ protected function doCreate($object, StrategyContainer $strategies, ?Context $co
7171
$default = $this->valueConverter->prettyPrintExpr($object->default);
7272
}
7373

74-
$argumentDescriptor = new ArgumentDescriptor((string) $object->var->name, $default, $object->byRef, $object->variadic);
75-
74+
$type = null;
7675
if (!empty($object->type)) {
77-
$argumentDescriptor->addType($this->createType($object));
76+
$type = $this->createType($object);
7877
}
7978

80-
return $argumentDescriptor;
79+
return new ArgumentDescriptor((string) $object->var->name, $type, $default, $object->byRef, $object->variadic);
8180
}
8281

8382
private function createType(Param $arg, ?Context $context = null): Type

tests/component/ProjectCreationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testWithNamespacedClass()
105105
);
106106

107107
$this->assertEquals('style', $methods['\\Luigi\\Pizza::__construct()']->getArguments()[0]->getName());
108-
$this->assertEquals([new Object_(new Fqsen('\\Luigi\\Pizza\Style'))], $methods['\\Luigi\\Pizza::__construct()']->getArguments()[0]->getTypes());
108+
$this->assertEquals(new Object_(new Fqsen('\\Luigi\\Pizza\Style')), $methods['\\Luigi\\Pizza::__construct()']->getArguments()[0]->getType());
109109
}
110110

111111
public function testDocblockOfMethodIsProcessed()

tests/unit/phpDocumentor/Reflection/File/LocalFileTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public function testMd5()
3838
$this->assertEquals(md5_file(__FILE__), $file->md5());
3939
}
4040

41+
/**
42+
* @covers ::__construct
43+
* @expectedException \InvalidArgumentException
44+
*/
45+
public function testNotExistingFileThrowsException()
46+
{
47+
new LocalFile('aa');
48+
}
49+
4150
/**
4251
* @covers ::path
4352
*/

tests/unit/phpDocumentor/Reflection/Php/ArgumentTest.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,32 @@
1212

1313
namespace phpDocumentor\Reflection\Php;
1414

15+
use phpDocumentor\Reflection\Types\Mixed_;
16+
use phpDocumentor\Reflection\Types\String_;
1517
use PHPUnit\Framework\TestCase;
1618

1719
/**
1820
* Tests the functionality for the Argument class.
19-
* @coversDefaultClass phpDocumentor\Reflection\Php\Argument
21+
* @coversDefaultClass \phpDocumentor\Reflection\Php\Argument
2022
*/
2123
class ArgumentTest extends TestCase
2224
{
2325
/**
24-
* @covers ::getTypes
25-
* @covers ::addType
26+
* @covers ::getType
2627
*/
2728
public function testGetTypes()
2829
{
29-
$argument = new Argument('myArgument', 'myDefaultValue', true, true);
30-
$this->assertSame([], $argument->getTypes());
30+
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
31+
$this->assertInstanceOf(Mixed_::class, $argument->getType());
3132

32-
$argument->addType(1);
33-
34-
$this->assertSame([1], $argument->getTypes());
33+
$argument = new Argument(
34+
'myArgument',
35+
new String_(),
36+
'myDefaultValue',
37+
true,
38+
true
39+
);
40+
$this->assertEquals(new String_(), $argument->getType());
3541
}
3642

3743
/**
@@ -50,10 +56,10 @@ public function testGetName()
5056
*/
5157
public function testGetDefault()
5258
{
53-
$argument = new Argument('myArgument', 'myDefaultValue', true, true);
59+
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
5460
$this->assertEquals('myDefaultValue', $argument->getDefault());
5561

56-
$argument = new Argument('myArgument', null, true, true);
62+
$argument = new Argument('myArgument', null, null, true, true);
5763
$this->assertNull($argument->getDefault());
5864
}
5965

@@ -63,10 +69,10 @@ public function testGetDefault()
6369
*/
6470
public function testGetWhetherArgumentIsPassedByReference()
6571
{
66-
$argument = new Argument('myArgument', 'myDefaultValue', true, true);
72+
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
6773
$this->assertTrue($argument->isByReference());
6874

69-
$argument = new Argument('myArgument', null, false, true);
75+
$argument = new Argument('myArgument', null, null, false, true);
7076
$this->assertFalse($argument->isByReference());
7177
}
7278

@@ -76,10 +82,10 @@ public function testGetWhetherArgumentIsPassedByReference()
7682
*/
7783
public function testGetWhetherArgumentisVariadic()
7884
{
79-
$argument = new Argument('myArgument', 'myDefaultValue', true, true);
85+
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
8086
$this->assertTrue($argument->isVariadic());
8187

82-
$argument = new Argument('myArgument', 'myDefaultValue', true, false);
88+
$argument = new Argument('myArgument', null, 'myDefaultValue', true, false);
8389
$this->assertFalse($argument->isVariadic());
8490
}
8591
}

0 commit comments

Comments
 (0)