Skip to content

Commit a12ac4b

Browse files
committed
extracted name configuration
1 parent 9a5a5ac commit a12ac4b

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/Parameter.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ class Parameter implements
8686
*/
8787
public function __construct(?string $parameter = null)
8888
{
89-
$parameter ??= $this->generateName('param');
90-
91-
self::assertValidName($parameter);
92-
93-
$this->name = $parameter;
89+
$this->configureName($parameter, 'param');
9490
}
9591

9692
/**

src/Traits/HasNameTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ trait HasNameTrait
1111
{
1212
private string $name;
1313

14+
/**
15+
* Returns the length in bytes of the auto-generated part of a name.
16+
*
17+
* @return int
18+
*/
1419
public static function automaticVariableLength(): int
1520
{
1621
return 32;
@@ -44,4 +49,13 @@ private function generateName(string $prefix = 'var', int $length = null): strin
4449

4550
return $prefix . substr(bin2hex(openssl_random_pseudo_bytes(ceil($length / 2))), 0, $length);
4651
}
52+
53+
private function configureName(?string $givenName, string $prefix, int $length = null): void
54+
{
55+
$name = $givenName ?? $this->generateName($prefix, $length);
56+
57+
self::assertValidName($name);
58+
59+
$this->name = $name;
60+
}
4761
}

src/Variable.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ class Variable implements
9494
*/
9595
public function __construct(?string $variable = null)
9696
{
97-
$variable ??= $this->generateName('var');
98-
99-
self::assertValidName($variable);
100-
101-
$this->name = $variable;
97+
$this->configureName($variable, 'var');
10298
}
10399

104100
/**

0 commit comments

Comments
 (0)