diff --git a/src/Specs/Builder/ParameterSpecBuilder.php b/src/Specs/Builder/ParameterSpecBuilder.php index 1df38c8..b6e2728 100644 --- a/src/Specs/Builder/ParameterSpecBuilder.php +++ b/src/Specs/Builder/ParameterSpecBuilder.php @@ -30,10 +30,6 @@ class ParameterSpecBuilder implements ParameterSpecBuilderInterface { protected $regexp = '/ ^ - (?: - (?(\w+\b(?:\(.*\))?)(?:\s*\|\s*(?-1))*) - \s* - ) (?: (?\.{3}) \s* @@ -54,6 +50,14 @@ class ParameterSpecBuilder implements ParameterSpecBuilderInterface | true | false | null ) + \s* + )? + (?: + \s* + \: + \s* + (?(\w+\b(?:\(.*\))?)(?:\s*\|\s*(?-1))*) + \s* )? $ /xi'; @@ -102,7 +106,7 @@ public function build(string $definition): ParameterSpecInterface protected function buildVariadicParameterSpec(array $matches): VariadicParameterSpec { - if (isset($matches['default'])) { + if (isset($matches['default']) && '' !== $matches['default']) { throw new ParameterSpecBuilderException('Variadic parameter should have no default value'); } diff --git a/tests/Specs/Builder/FunctionSpecBuilderTest.php b/tests/Specs/Builder/FunctionSpecBuilderTest.php index b0799ca..719582d 100644 --- a/tests/Specs/Builder/FunctionSpecBuilderTest.php +++ b/tests/Specs/Builder/FunctionSpecBuilderTest.php @@ -109,9 +109,9 @@ public function testBuildingSpecWithNoReturnTypeIsTheSameAsWithAnyReturnType() public function testBuildSpecWithParams() { - $this->parameterSpecBuilderShouldBuildOn('param one', 'param two = "default"', 'rest ...params'); + $this->parameterSpecBuilderShouldBuildOn('one: param', 'two = "default": param', '...params: rest'); - $spec = $this->builder->build('(param one, param two = "default", rest ...params)'); + $spec = $this->builder->build('(one: param, two = "default": param, ...params: rest)'); $this->assertInstanceOf(FunctionSpecInterface::class, $spec); $this->assertFalse($spec->needsExecutionContext()); diff --git a/tests/Specs/Builder/ParameterSpecBuilderTest.php b/tests/Specs/Builder/ParameterSpecBuilderTest.php index 4e0bb0b..9c87e84 100644 --- a/tests/Specs/Builder/ParameterSpecBuilderTest.php +++ b/tests/Specs/Builder/ParameterSpecBuilderTest.php @@ -69,7 +69,7 @@ public function testBuildingMandatoryParameter() { $this->extractorDefinitionShouldBuildOn('type'); - $spec = $this->builder->build('type param'); + $spec = $this->builder->build('param: type'); $this->assertInstanceOf(MandatoryParameterSpec::class, $spec); @@ -81,7 +81,7 @@ public function testBuildingMandatoryParameterWithComplexType() { $this->extractorDefinitionShouldBuildOn('instance( Some\Class )'); - $spec = $this->builder->build('instance( Some\Class ) param'); + $spec = $this->builder->build('param : instance( Some\Class )'); $this->assertInstanceOf(MandatoryParameterSpec::class, $spec); @@ -93,7 +93,7 @@ public function testBuildingMandatoryParameterWithVaryingType() { $this->extractorDefinitionShouldBuildOn('foo|bar'); - $spec = $this->builder->build('foo|bar param'); + $spec = $this->builder->build('param: foo|bar'); $this->assertInstanceOf(MandatoryParameterSpec::class, $spec); @@ -111,7 +111,7 @@ public function testBuildingOptionalParameter(string $raw_default, $expected_def { $this->extractorDefinitionShouldBuildOn('type'); - $spec = $this->builder->build('type param = ' . $raw_default); + $spec = $this->builder->build('param = ' . $raw_default . ': type'); $this->assertInstanceOf(OptionalParameterSpec::class, $spec); @@ -124,7 +124,7 @@ public function testBuildingVariadicParameter() { $this->extractorDefinitionShouldBuildOn('type'); - $spec = $this->builder->build('type ...param'); + $spec = $this->builder->build('...param: type'); $this->assertInstanceOf(VariadicParameterSpec::class, $spec); @@ -138,7 +138,7 @@ public function testBuildingVariadicParameter() */ public function testBuildingVariadicParameterWithDefaultValueShouldThrowException() { - $this->builder->build('type ...param = []'); + $this->builder->build('...param = []: type'); } /** @@ -149,7 +149,7 @@ public function testBuildingWhenExtractorFailsShouldAlsoFail() { $this->extractorDefinitionShouldThrowOn('fail'); - $this->builder->build('fail param'); + $this->builder->build('param :fail'); }