• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / latte / 16317387179

16 Jul 2025 10:46AM UTC coverage: 93.699% (-0.1%) from 93.84%
16317387179

push

github

dg
added Helpers::stringOrNull() [Closes #401]

6 of 6 new or added lines in 2 files covered. (100.0%)

57 existing lines in 19 files now uncovered.

5160 of 5507 relevant lines covered (93.7%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.5
/src/Latte/Helpers.php
1
<?php
2

3
/**
4
 * This file is part of the Latte (https://latte.nette.org)
5
 * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Latte;
11

12
use function array_keys, array_search, array_slice, array_unique, count, is_array, is_object, is_string, levenshtein, max, min, strlen, strpos;
13
use const PHP_VERSION_ID;
14

15

16
/**
17
 * Latte helpers.
18
 * @internal
19
 */
20
class Helpers
21
{
22
        /** @var array<string, int>  empty (void) HTML elements */
23
        public static array $emptyElements = [
24
                'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1, 'source' => 1, 'base' => 1,
25
                'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1, 'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
26
        ];
27

28

29
        /**
30
         * Finds the best suggestion.
31
         * @param  string[]  $items
32
         */
33
        public static function getSuggestion(array $items, string $value): ?string
1✔
34
        {
35
                $best = null;
1✔
36
                $min = (strlen($value) / 4 + 1) * 10 + .1;
1✔
37
                foreach (array_unique($items) as $item) {
1✔
38
                        if (($len = levenshtein($item, $value, 10, 11, 10)) > 0 && $len < $min) {
1✔
39
                                $min = $len;
1✔
40
                                $best = $item;
1✔
41
                        }
42
                }
43

44
                return $best;
1✔
45
        }
46

47

48
        /** intentionally without callable typehint, because it generates bad error messages */
49
        public static function toReflection($callable): \ReflectionFunctionAbstract
50
        {
51
                if (is_string($callable) && strpos($callable, '::')) {
1✔
52
                        return PHP_VERSION_ID < 80300
1✔
UNCOV
53
                                ? new \ReflectionMethod($callable)
×
54
                                : \ReflectionMethod::createFromMethodName($callable);
1✔
55
                } elseif (is_array($callable)) {
1✔
56
                        return new \ReflectionMethod($callable[0], $callable[1]);
1✔
57
                } elseif (is_object($callable) && !$callable instanceof \Closure) {
1✔
58
                        return new \ReflectionMethod($callable, '__invoke');
1✔
59
                } else {
60
                        return new \ReflectionFunction($callable);
1✔
61
                }
62
        }
63

64

65
        public static function sortBeforeAfter(array $list): array
1✔
66
        {
67
                foreach ($list as $name => $info) {
1✔
68
                        if (!$info instanceof \stdClass || !($info->before ?? $info->after ?? null)) {
1✔
69
                                continue;
1✔
70
                        }
71

72
                        unset($list[$name]);
1✔
73
                        $names = array_keys($list);
1✔
74
                        $best = null;
1✔
75

76
                        foreach ((array) $info->before as $target) {
1✔
77
                                if ($target === '*') {
1✔
78
                                        $best = 0;
1✔
79
                                } elseif (isset($list[$target])) {
×
80
                                        $pos = array_search($target, $names, true);
×
81
                                        $best = min($pos, $best ?? $pos);
×
82
                                }
83
                        }
84

85
                        foreach ((array) ($info->after ?? null) as $target) {
1✔
UNCOV
86
                                if ($target === '*') {
×
UNCOV
87
                                        $best = count($names);
×
UNCOV
88
                                } elseif (isset($list[$target])) {
×
UNCOV
89
                                        $pos = array_search($target, $names, true);
×
UNCOV
90
                                        $best = max($pos + 1, $best);
×
91
                                }
92
                        }
93

94
                        $list = array_slice($list, 0, $best, true)
1✔
95
                                + [$name => $info]
1✔
96
                                + array_slice($list, $best, null, true);
1✔
97
                }
98

99
                return $list;
1✔
100
        }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc