Skip to content

Match support in latte #267

@helvete

Description

@helvete

Version: 2.10.3

Latte engine seems to incorrectly handle (inlined) PHP8's match expression. It converts default fallback-branch keyword into 'default' which catches a string 'default' only.

Steps To Reproduce

Any match expression that contains default keyword. Please note, the other branches are not impacted by this by definition - so to reproduce this behaviour, the default branch has to match.

Something like the following snippet should trigger the error.:

{var $var = match(7) {8 => true, default => false,};}

Expected Behaviour

default fallback branch works.. It's even worse for match than for switch, since the match actually has to match :-)

Actual Behaviour

The process ends with UnhandledMatchError and message: Unhandled match value of type int. Demo.

Debugging

To verify what happened I tried to render this template snippet:

<html>                                                                          
{var $promoCodeSize = match(strlen('1234567')){13,14,15,16 => 24, 8,9,10,11,12 => 32, default => 48,};}
<head>  

Then dumped the $code var before the call to eval in Latte\Engine.

The rendered template result:

<?php                                                                           
                                                                                
use Latte\Runtime as LR;                                                        
                                                                                
/** source: /htdocs/src/Bonus/Voucher/../Templates/voucher.latte */          
final class Template8ba8aeabd9 extends Latte\Runtime\Template                   
{                                                                               
                                                                                
    public function main(): array                                               
    {                                                                           
        extract($this->params);                                                 
        echo '<html>                                                            
';                                                                              
        $promoCodeSize = match(strlen('1234567')){13,14,15,16 => 24, 8,9,10,11,12 => 32, 'default' => 48,};
        /* line 2 */;                                                           
        echo '<head>           
        ...                

Please do not hesitate to request any additional debug info.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions