Skip to content

Commit cfe2996

Browse files
committed
Improve preprocessor directives
1 parent 09a9fbe commit cfe2996

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/Directives/Directive/Directive.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ public function getMinArgumentsCount(): int
8282
{
8383
return $this->minArgumentsCount;
8484
}
85+
86+
/**
87+
* @param mixed $result
88+
* @return string
89+
*/
90+
protected function toString(mixed $result): string
91+
{
92+
return match(true) {
93+
$result === true => '1',
94+
\is_null($result), $result === false => '0',
95+
\is_string($result) => \var_export($result, true),
96+
default => (string)$result,
97+
};
98+
}
8599
}

src/Directives/Directive/FunctionDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public function __construct(callable $cb)
3838
*/
3939
public function __invoke(string ...$args): string
4040
{
41-
return (string)($this->callback)(...$args);
41+
return $this->toString(($this->callback)(...$args));
4242
}
4343
}

src/Directives/Directive/FunctionLikeDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ public function __invoke(string ...$args): string
6464
$this->compiled = Compiler::compile($this->body, $this->args);
6565
}
6666

67-
return (string)($this->compiled)(...$args);
67+
return $this->toString(($this->compiled)(...$args));
6868
}
6969
}

0 commit comments

Comments
 (0)