Render simple php function in twig template
Would suggest doing a pre-process function and stuff in the variables everything the template needs to render. The point of Twig is to push the logic back to the PHP side so that your template does not look like "HTML-in-PHP" or "PHP-in-HTML" (whichever way you look at it) as with previous versions of Drupal.