Skip to content

Commit

Permalink
Merge pull request #28 from xefi/feature/nullable-doc-block-parameter
Browse files Browse the repository at this point in the history
✨ nullable doc block method parameter type
  • Loading branch information
GautierDele authored Nov 5, 2024
2 parents 1c2a4d6 + 1525e94 commit b151350
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/Container/Traits/HasLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ trait HasLocale
/**
* The current locale format (BCP 47 Code).
*
* @var string
* @var ?string
*/
protected string|null $locale;
protected ?string $locale;

/**
* Get the current locale.
*
* @return string|null
* @return ?string
*/
public function getLocale(): string|null
public function getLocale(): ?string
{
return $this->locale ?? null;
}

/**
* Change the locale.
*
* @param string|null $locale
* @param ?string $locale
*
* @return self
*/
public function locale(string|null $locale): self
public function locale(?string $locale): self
{
$this->locale = $locale;

Expand Down
6 changes: 3 additions & 3 deletions src/Extensions/FinancialExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class FinancialExtension extends Extension
use HasLocale;

/**
* @param string|null $countryCode
* @param string|null $format [{d} => digit, {l} => letter, {a} => any]
* @param ?string $countryCode
* @param ?string $format [{d} => digit, {l} => letter, {a} => any]
*
* @return string
*/
public function iban(string $countryCode = null, string $format = null): string
public function iban(?string $countryCode = null, ?string $format = null): string
{
if ($format === null) {
$format = str_repeat('{d}', 24);
Expand Down
6 changes: 3 additions & 3 deletions src/Extensions/PersonExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ class PersonExtension extends Extension
'Professor',
];

public function name(string|null $gender = null): string
public function name(?string $gender = null): string
{
return sprintf('%s %s', $this->firstName($gender), $this->lastName());
}

public function firstName(string|null $gender = null): string
public function firstName(?string $gender = null): string
{
if ($gender === static::GENDER_MALE) {
return $this->pickArrayRandomElement($this->firstNameMale);
Expand All @@ -89,7 +89,7 @@ public function lastName(): string
return $this->pickArrayRandomElement($this->lastName);
}

public function title($gender = null)
public function title(?string $gender = null)
{
if ($gender === static::GENDER_MALE) {
return $this->pickArrayRandomElement($this->titleMale);
Expand Down
4 changes: 2 additions & 2 deletions src/Extensions/Traits/HasLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ trait HasLocale
/**
* The extension locale (BCP 47 Code).
*
* @return string | null
* @return ?string
*/
public function getLocale(): string|null
public function getLocale(): ?string
{
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Faker
/**
* The current locale format (BCP 47 Code).
*
* @var string|null
* @var ?string
*/
protected string|null $locale;
protected ?string $locale;

public function __construct(string|null $locale = null)
public function __construct(?string $locale = null)
{
$this->locale = $locale;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Manifests/ContainerMixinManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Intersection;
use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\Nullable;

class ContainerMixinManifest
{
Expand Down Expand Up @@ -36,10 +37,10 @@ class ContainerMixinManifest
/**
* Create a new package manifest instance.
*
* @param string $basePath
* @param string|null $containerMixinPath
* @param string $basePath
* @param ?string $containerMixinPath
*/
public function __construct(string $basePath, string $containerMixinPath = null)
public function __construct(string $basePath, ?string $containerMixinPath = null)
{
$this->basePath = $basePath;
$this->vendorPath = $basePath.'/vendor';
Expand Down Expand Up @@ -104,6 +105,10 @@ public function build(array $extensionMethods, array $extensions)
} else {
$type = $typeResolver->resolve($type->getName());
}

if ($parameter->allowsNull()) {
$type = new Nullable($type);
}
}

$parameters[] = new DocBlock\Tags\MethodParameter(
Expand Down
4 changes: 2 additions & 2 deletions src/Manifests/PackageManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class PackageManifest
/**
* The manifest path.
*
* @var string|null
* @var ?string
*/
public string|null $manifestPath;
public ?string $manifestPath;

/**
* The base path.
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Extensions/EnEnExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getName(): string
return 'locale-extension-test';
}

public function getLocale(): string|null
public function getLocale(): ?string
{
return 'en_EN';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Extensions/EnUsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getName(): string
return 'locale-extension-test';
}

public function getLocale(): string|null
public function getLocale(): ?string
{
return 'en_US';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Extensions/FrFrExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getName(): string
return 'locale-extension-test';
}

public function getLocale(): string|null
public function getLocale(): ?string
{
return 'fr_FR';
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/Extensions/MixinTestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function withStringReturnType(): string
{
return 'string';
}

public function withNullableParameter(?string $one = 'default value'): string
{
return 'string';
}
}
5 changes: 5 additions & 0 deletions tests/Unit/ContainerMixinManifestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function testContainerMixinBuild()
$containerMixinContent
);

$this->assertStringContainsString(
'@method string withNullableParameter(?string $one)',
$containerMixinContent
);

unlink('/tmp/ContainerMixin.php');
}

Expand Down

0 comments on commit b151350

Please sign in to comment.