在 FuncCallOptimizer 中遇到占位符表达式(如 First-class callable)时跳过优化, 避免参数遍历中的类型处理错误导致运行时异常。pull/14/head
parent
1d2dab4875
commit
4fc0acc47f
5 changed files with 116 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony pattern: array_map with first-class builtin callable |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
function normalizeExpectedTypes(array $expectedTypes): array |
||||||
|
{ |
||||||
|
return $expectedTypes ? array_map(strval(...), $expectedTypes) : $expectedTypes; |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(normalizeExpectedTypes([1, 2.5, true, 'name'])); |
||||||
|
var_dump(normalizeExpectedTypes([])); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
array(4) { |
||||||
|
[0]=> |
||||||
|
string(1) "1" |
||||||
|
[1]=> |
||||||
|
string(3) "2.5" |
||||||
|
[2]=> |
||||||
|
string(1) "1" |
||||||
|
[3]=> |
||||||
|
string(4) "name" |
||||||
|
} |
||||||
|
array(0) { |
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony pattern: context array union keeps left-hand keys |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
class SymfonyLikeSender |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
function logContext(object $message, string $alias, object $sender): array |
||||||
|
{ |
||||||
|
$context = [ |
||||||
|
'class' => $message::class, |
||||||
|
'alias' => 'existing', |
||||||
|
]; |
||||||
|
|
||||||
|
return $context + ['alias' => $alias, 'sender' => $sender::class]; |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(logContext(new stdClass(), 'async', new SymfonyLikeSender())); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
array(3) { |
||||||
|
["class"]=> |
||||||
|
string(8) "stdClass" |
||||||
|
["alias"]=> |
||||||
|
string(8) "existing" |
||||||
|
["sender"]=> |
||||||
|
string(17) "SymfonyLikeSender" |
||||||
|
} |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony pattern: dynamic class instantiation with null coalesce default |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
class SymfonyLikeNamedService |
||||||
|
{ |
||||||
|
public function __construct(public string $name = 'default') |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function createService(?string $class, ?string $name = null): object |
||||||
|
{ |
||||||
|
$class ??= SymfonyLikeNamedService::class; |
||||||
|
|
||||||
|
return new $class($name ?? 'fallback'); |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(createService(null)->name); |
||||||
|
var_dump(createService(SymfonyLikeNamedService::class, 'custom')->name); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(8) "fallback" |
||||||
|
string(6) "custom" |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony pattern: static closure normalizer returns another static closure |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
function buildTranslator(string $domain): Closure |
||||||
|
{ |
||||||
|
$normalizer = static fn (string $message): Closure => static fn () => '['.$domain.'] '.$message; |
||||||
|
|
||||||
|
return $normalizer('upload failed'); |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
$message = buildTranslator('validators'); |
||||||
|
var_dump($message()); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(26) "[validators] upload failed" |
||||||
Loading…
Reference in new issue