Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 344583c

Browse files
committed
Migrate to postfix type signature/annotation
1 parent 90d8244 commit 344583c

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/Specs/Builder/ParameterSpecBuilder.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ class ParameterSpecBuilder implements ParameterSpecBuilderInterface
3030
{
3131
protected $regexp = '/
3232
^
33-
(?:
34-
(?<type>(\w+\b(?:\(.*\))?)(?:\s*\|\s*(?-1))*)
35-
\s*
36-
)
3733
(?:
3834
(?<rest>\.{3})
3935
\s*
@@ -54,6 +50,14 @@ class ParameterSpecBuilder implements ParameterSpecBuilderInterface
5450
|
5551
true | false | null
5652
)
53+
\s*
54+
)?
55+
(?:
56+
\s*
57+
\:
58+
\s*
59+
(?<type>(\w+\b(?:\(.*\))?)(?:\s*\|\s*(?-1))*)
60+
\s*
5761
)?
5862
$
5963
/xi';
@@ -102,7 +106,7 @@ public function build(string $definition): ParameterSpecInterface
102106

103107
protected function buildVariadicParameterSpec(array $matches): VariadicParameterSpec
104108
{
105-
if (isset($matches['default'])) {
109+
if (isset($matches['default']) && '' !== $matches['default']) {
106110
throw new ParameterSpecBuilderException('Variadic parameter should have no default value');
107111
}
108112

tests/Specs/Builder/FunctionSpecBuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public function testBuildingSpecWithNoReturnTypeIsTheSameAsWithAnyReturnType()
109109

110110
public function testBuildSpecWithParams()
111111
{
112-
$this->parameterSpecBuilderShouldBuildOn('param one', 'param two = "default"', 'rest ...params');
112+
$this->parameterSpecBuilderShouldBuildOn('one: param', 'two = "default": param', '...params: rest');
113113

114-
$spec = $this->builder->build('(param one, param two = "default", rest ...params)');
114+
$spec = $this->builder->build('(one: param, two = "default": param, ...params: rest)');
115115

116116
$this->assertInstanceOf(FunctionSpecInterface::class, $spec);
117117
$this->assertFalse($spec->needsExecutionContext());

tests/Specs/Builder/ParameterSpecBuilderTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testBuildingMandatoryParameter()
6969
{
7070
$this->extractorDefinitionShouldBuildOn('type');
7171

72-
$spec = $this->builder->build('type param');
72+
$spec = $this->builder->build('param: type');
7373

7474
$this->assertInstanceOf(MandatoryParameterSpec::class, $spec);
7575

@@ -81,7 +81,7 @@ public function testBuildingMandatoryParameterWithComplexType()
8181
{
8282
$this->extractorDefinitionShouldBuildOn('instance( Some\Class )');
8383

84-
$spec = $this->builder->build('instance( Some\Class ) param');
84+
$spec = $this->builder->build('param : instance( Some\Class )');
8585

8686
$this->assertInstanceOf(MandatoryParameterSpec::class, $spec);
8787

@@ -93,7 +93,7 @@ public function testBuildingMandatoryParameterWithVaryingType()
9393
{
9494
$this->extractorDefinitionShouldBuildOn('foo|bar');
9595

96-
$spec = $this->builder->build('foo|bar param');
96+
$spec = $this->builder->build('param: foo|bar');
9797

9898
$this->assertInstanceOf(MandatoryParameterSpec::class, $spec);
9999

@@ -111,7 +111,7 @@ public function testBuildingOptionalParameter(string $raw_default, $expected_def
111111
{
112112
$this->extractorDefinitionShouldBuildOn('type');
113113

114-
$spec = $this->builder->build('type param = ' . $raw_default);
114+
$spec = $this->builder->build('param = ' . $raw_default . ': type');
115115

116116
$this->assertInstanceOf(OptionalParameterSpec::class, $spec);
117117

@@ -124,7 +124,7 @@ public function testBuildingVariadicParameter()
124124
{
125125
$this->extractorDefinitionShouldBuildOn('type');
126126

127-
$spec = $this->builder->build('type ...param');
127+
$spec = $this->builder->build('...param: type');
128128

129129
$this->assertInstanceOf(VariadicParameterSpec::class, $spec);
130130

@@ -138,7 +138,7 @@ public function testBuildingVariadicParameter()
138138
*/
139139
public function testBuildingVariadicParameterWithDefaultValueShouldThrowException()
140140
{
141-
$this->builder->build('type ...param = []');
141+
$this->builder->build('...param = []: type');
142142
}
143143

144144
/**
@@ -149,7 +149,7 @@ public function testBuildingWhenExtractorFailsShouldAlsoFail()
149149
{
150150
$this->extractorDefinitionShouldThrowOn('fail');
151151

152-
$this->builder->build('fail param');
152+
$this->builder->build('param :fail');
153153
}
154154

155155

0 commit comments

Comments
 (0)