Skip to content

Commit cee0245

Browse files
committed
Parameter now uses Argument enum. Removed unused method.
1 parent f7ed1a0 commit cee0245

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

Src/Parameter.php

+25-49
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
namespace TheWebSolver\Codegarage\Generator;
55

66
use Nette\Utils\Type;
7-
use RuntimeException;
87
use InvalidArgumentException;
98
use Nette\PhpGenerator\Helpers;
109
use Nette\PhpGenerator\Literal;
1110
use Nette\PhpGenerator\PromotedParameter;
11+
use TheWebSolver\Codegarage\Generator\Enum\Argument;
1212
use TheWebSolver\Codegarage\Generator\Error\ParamExtractionException as ExtractionError;
1313

1414
/**
@@ -24,31 +24,6 @@
2424
* }
2525
*/
2626
final class Parameter {
27-
public const REFERENCE = 'isReference';
28-
public const NULLABLE = 'isNullable';
29-
public const VARIADIC = 'isVariadic';
30-
public const PROMOTED = 'isPromoted';
31-
public const POSITION = 'position';
32-
public const DEFAULT = 'defaultValue';
33-
public const TYPE = 'type';
34-
public const NAME = 'name';
35-
36-
/**
37-
* List of constructor arguments with their respective type.
38-
*
39-
* @var array<string,string>
40-
*/
41-
public const CREATION_ARGS = array(
42-
self::REFERENCE => 'bool',
43-
self::NULLABLE => 'bool',
44-
self::VARIADIC => 'bool',
45-
self::PROMOTED => 'bool',
46-
self::POSITION => 'int',
47-
self::DEFAULT => 'mixed',
48-
self::TYPE => '?string',
49-
self::NAME => 'string',
50-
);
51-
5227
private bool $isDefaultValueAvailable = false;
5328
private bool $isDefaultValueConstant = false;
5429
private ?string $invalidValueKey = null;
@@ -153,7 +128,7 @@ public static function extractFrom( string $string, ?callable $validator = null
153128
}
154129

155130
[ $name, $value ] = $pair;
156-
$args[ $name ] = ! isset( self::CREATION_ARGS[ $name ] )
131+
$args[ $name ] = ! Argument::tryFrom( $name )
157132
? self::throwExtractionError( ExtractionError::INVALID_CREATION_ARG )
158133
: $value;
159134
}
@@ -162,7 +137,9 @@ public static function extractFrom( string $string, ?callable $validator = null
162137
self::throwExtractionError( ExtractionError::FROM_VALIDATOR );
163138
}
164139

165-
return isset( $args[ self::NAME ] ) ? $args : self::throwExtractionError( ExtractionError::NO_NAME_ARG );
140+
return ! isset( $args[ Argument::Name->value ] )
141+
? self::throwExtractionError( ExtractionError::NO_NAME_ARG )
142+
: $args;
166143
}
167144

168145
/**
@@ -180,34 +157,33 @@ public static function createFrom( array $args ): self {
180157
);
181158
}
182159

183-
public static function validateCreationArg( string $name ): void {
184-
if ( ! array_key_exists( $name, self::CREATION_ARGS ) ) {
185-
throw new RuntimeException(
186-
sprintf(
187-
'The parameter data must be for one of the creation args: %1$s. "%2$s" given',
188-
implode( ' | ', array_keys( self::CREATION_ARGS ) ),
189-
$name
190-
)
191-
);
192-
}
193-
}
194-
195160
/** @param array{position?:int,defaultValue?:mixed} $newValues */
196161
public function recreateWith( array $newValues ): self {
197162
return self::createFrom( array( ...$this->toArray(), ...$newValues ) );
198163
}
199164

200-
/** @return ArgsAsArray */
165+
/**
166+
* @return array{
167+
* name:string,
168+
* position:int,
169+
* type:?string,
170+
* defaultValue:mixed,
171+
* isReference:bool,
172+
* isVariadic:bool,
173+
* isNullable:bool,
174+
* isPromoted:bool
175+
* }
176+
*/
201177
public function toArray(): array {
202178
return $this->asArray ??= array(
203-
self::REFERENCE => $this->isPassedByReference(),
204-
self::NULLABLE => $this->allowsNull(),
205-
self::VARIADIC => $this->isVariadic(),
206-
self::PROMOTED => $this->isPromoted(),
207-
self::POSITION => $this->getPosition(),
208-
self::DEFAULT => $this->getRawDefaultValue(),
209-
self::TYPE => $this->getRawType(),
210-
self::NAME => $this->getName(),
179+
Argument::Reference->value => $this->isPassedByReference(),
180+
Argument::Nullable->value => $this->allowsNull(),
181+
Argument::Variadic->value => $this->isVariadic(),
182+
Argument::Promoted->value => $this->isPromoted(),
183+
Argument::Position->value => $this->getPosition(),
184+
Argument::Default->value => $this->getRawDefaultValue(),
185+
Argument::Type->value => $this->getRawType(),
186+
Argument::Name->value => $this->getName(),
211187
);
212188
}
213189

0 commit comments

Comments
 (0)