Skip to content

Commit

Permalink
Fix nullable parameter type for PHP 8.4 (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 19, 2024
1 parent 6bc475b commit f366917
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 27 deletions.
3 changes: 0 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
'general_phpdoc_annotation_remove' => [
'annotations' => ['author', 'copyright', 'throws'],
],
'nullable_type_declaration_for_default_null_value' => [
'use_nullable_type_declaration' => false,
],

// fn => without curly brackets is less readable,
// also prevent bounding of unwanted variables for GC
Expand Down
6 changes: 3 additions & 3 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class Exception extends \Exception implements SelfDescribing
/** @var list<string> */
private $solutions = [];

/** @var ITranslatorAdapter */
/** @var ITranslatorAdapter|null */
private $translator;

public function __construct(string $message = '', int $code = 0, \Throwable $previous = null)
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);

Expand Down Expand Up @@ -162,7 +162,7 @@ public function getCustomExceptionTitle(): string
*
* @return $this
*/
public function setTranslatorAdapter(ITranslatorAdapter $translator = null): self
public function setTranslatorAdapter(?ITranslatorAdapter $translator = null): self
{
$this->translator = $translator;

Expand Down
6 changes: 4 additions & 2 deletions src/ExceptionRenderer/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected function processStackTrace(): void
$this->output .= <<<'EOF'
\e[1;41m--[ Stack Trace ]\e[0m
EOF . "\n";

EOF;

$this->processStackTraceInternal();
}
Expand All @@ -79,7 +80,8 @@ protected function processStackTraceInternal(): void
{
$text = <<<'EOF'
\e[0m{FILE}\e[0m:\e[0;31m{LINE}\e[0m {OBJECT} {CLASS}{FUNCTION_COLOR}{FUNCTION}{FUNCTION_ARGS}
EOF . "\n";

EOF;

$inAtk = true;
$shortTrace = $this->getStackTrace(true);
Expand Down
4 changes: 2 additions & 2 deletions src/ExceptionRenderer/RendererAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class RendererAbstract

public ?ITranslatorAdapter $adapter;

public function __construct(\Throwable $exception, ITranslatorAdapter $adapter = null, \Throwable $parentException = null)
public function __construct(\Throwable $exception, ?ITranslatorAdapter $adapter = null, ?\Throwable $parentException = null)
{
$this->exception = $exception;
$this->parentException = $parentException;
Expand Down Expand Up @@ -226,7 +226,7 @@ protected function getStackTrace(bool $shorten): array
/**
* @param array<string, mixed> $parameters
*/
public function _(string $message, array $parameters = [], string $domain = null, string $locale = null): string
public function _(string $message, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
return $this->adapter
? $this->adapter->_($message, $parameters, $domain, $locale)
Expand Down
10 changes: 5 additions & 5 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,8 @@ protected function _newObject(string $className, array $ctorArgs): object
* @param array<mixed>|object $seed
* @param array<mixed> $defaults
*/
protected function _factory($seed, array $defaults = null): object
protected function _factory($seed, array $defaults): object
{
if ($defaults === null) { // should be deprecated soon (with [] default value)
$defaults = [];
}

if (!is_array($seed) && !is_object($seed)) { // @phpstan-ignore-line
throw new Exception('Use of non-array (' . gettype($seed) . ') seed is not supported');
}
Expand Down Expand Up @@ -199,6 +195,10 @@ final public static function factory($seed, $defaults = []): object
throw new \Error('Too many method arguments');
}

if ($defaults === null) { // @phpstan-ignore-line should be deprecated soon
$defaults = [];
}

return self::getInstance()->_factory($seed, $defaults);
}
}
9 changes: 6 additions & 3 deletions src/HookTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function onHookDynamicShort(string $spot, \Closure $getFxThisFx, \Closure
* @param ($priorityIsIndex is true ? int : int|null) $priority filter specific priority, null for all
* @param bool $priorityIsIndex filter by index instead of priority
*/
public function hookHasCallbacks(string $spot, int $priority = null, bool $priorityIsIndex = false): bool
public function hookHasCallbacks(string $spot, ?int $priority = null, bool $priorityIsIndex = false): bool
{
if (!isset($this->hooks[$spot])) {
return false;
Expand Down Expand Up @@ -260,7 +260,7 @@ public function hookHasCallbacks(string $spot, int $priority = null, bool $prior
*
* @return static
*/
public function removeHook(string $spot, int $priority = null, bool $priorityIsIndex = false)
public function removeHook(string $spot, ?int $priority = null, bool $priorityIsIndex = false)
{
if ($priority !== null) {
if ($priorityIsIndex) {
Expand Down Expand Up @@ -292,10 +292,13 @@ public function removeHook(string $spot, int $priority = null, bool $priorityIsI
* Execute all closures assigned to $spot.
*
* @param array<int, mixed> $args
* @param mixed $brokenBy
*
* @param-out HookBreaker|null $brokenBy
*
* @return array<int, mixed>|mixed Array of responses indexed by hook indexes or value specified to breakHook
*/
public function hook(string $spot, array $args = [], HookBreaker &$brokenBy = null)
public function hook(string $spot, array $args = [], &$brokenBy = null)
{
$brokenBy = null;

Expand Down
2 changes: 1 addition & 1 deletion src/TranslatableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait TranslatableTrait
*
* @return string The translated string
*/
public function _(string $message, array $parameters = [], string $domain = null, string $locale = null): string
public function _(string $message, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
if (TraitUtil::hasAppScopeTrait($this) && $this->issetApp() && method_exists($this->getApp(), '_')) {
return $this->getApp()->_($message, $parameters, $domain, $locale);
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/Adapter/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Generic implements ITranslatorAdapter
protected array $definitions = [];

#[\Override]
public function _(string $message, array $parameters = [], string $domain = null, string $locale = null): string
public function _(string $message, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
$definition = $this->getDefinition($message, $domain ?? 'atk', $locale ?? 'en');

Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ITranslatorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface ITranslatorAdapter
*
* @return string The translated string
*/
public function _(string $message, array $parameters = [], string $domain = null, string $locale = null): string;
public function _(string $message, array $parameters = [], ?string $domain = null, ?string $locale = null): string;
}
2 changes: 1 addition & 1 deletion src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function getAdapter(): ITranslatorAdapter
*
* @return string The translated string
*/
public function _(string $message, array $parameters = [], string $domain = null, string $locale = null): string
public function _(string $message, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
return $this->getAdapter()->_($message, $parameters, $domain ?? $this->defaultDomain, $locale ?? $this->defaultLocale);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public function testPhpunitSelfDescribing(): void
...
]
]
EOF . "\n", // NL in the string is not parsed by Netbeans, see https://github.com/apache/netbeans/issues/4345

EOF,
$m->toString()
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/HookTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public function testReferences(): void
{
$obj = new HookMock();

$incFx = static function ($obj, &$a) {
++$a;
$incFx = static function ($obj, int &$value) {
++$value;
};

$obj->onHook('inc', $incFx);
Expand Down
6 changes: 4 additions & 2 deletions tests/QuietObjectWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function testDebugInfoQuiet(): void
(
[wrappedClass] => stdClass
)
EOF . "\n", print_r($o, true));

EOF, print_r($o, true));

$o = new QuietObjectWrapper(new class() {
/**
Expand All @@ -59,6 +60,7 @@ public function __debugInfoQuiet(): array
[foo] => 1
[Bar] => 2
)
EOF . "\n", print_r($o, true));

EOF, print_r($o, true));
}
}

0 comments on commit f366917

Please sign in to comment.