question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(php) Anonymous functions without {} block not highlighted correctly

See original GitHub issue

Describe the issue Highlighter seems to expect a title in PHP arrow functions, even though they are intentionally anonymous. Actual: Incorrect title recognized

Which language seems to have the issue? php

Sample Code to Reproduce

<?php
$fn1 = fn($x) => $x + $y;

Expected behavior Expected: Variables

Additional context Found in an Code-Golf answer, but also reproduces in the ungolfed version:

fn( $s ) => 
    ( $l = strlen( $s ) ) > 1 ?             // is input longer than one char?
        max( count_chars( $s ) ) > 1 ?      // has more than 1 of any single char?
            'Bunny'                         // if so, contains dupes so... Bunny it is
        :                                   // else, no dupes
            [
                'T' => [ 'Ka',  'Hu'  ],    // two dimensional associative
                'C' => [ 'Rai', 'Do'  ],    //   array containing handsign 
                'J' => [ 'Hyo',  'Sui' ]    //   and length as indexes
            ]
            [ $s[-1] ]                      // access array by last letter and
            [ $l % 2 ]                      //   length mod 2 (2 == 0, 3 == 1)
            . 'ton'                         //   and append "ton"
    :                                       // finally...
    'Fuma Shuriken'                         // input length was only one char

Context: Code Golf source The comments are colored orange because they are considered title.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
joshgoebelcommented, Feb 13, 2021

Oh, title will match it… so you’d just need a special case rule inside contains to explicitly handle use and mark it as a keyword… I’m assuming use is not a valid function name.

Perhaps:

 contains: [
   { beginKeywords: "use" },
   hljs.UNDERSCORE_TITLE_MODE,
1reaction
il3vencommented, Feb 13, 2021

Oh, title will match it… so you’d just need a special case rule inside contains to explicitly handle use and mark it as a keyword… I’m assuming use is not a valid function name.

This does it.

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Anonymous functions - Manual - PHP
Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the...
Read more >
php - Anonymous Function/Closure and using self:: or static
Show activity on this post. I am working with anonymous functions where I am creating anonymous function outside of the object, and then...
Read more >
PHP return type declaration not highlighted on anonymous ...
In PHP, the return type declaration on an anonymous function (closure) is not highlighted in the same colour as the same type declaration...
Read more >
How To Write Conditional Statements in PHP - DigitalOcean
The first argument is the $products array. The second is an anonymous function which tells the usort function what to do with each...
Read more >
Functions — reusable blocks of code - Learn web development
Bear in mind that some built-in browser functions are not part of the core ... This is called an anonymous function, because it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found