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

nette / latte / 19616305908

23 Nov 2025 07:01PM UTC coverage: 93.946% (+0.03%) from 93.916%
19616305908

push

github

dg
removed comment /*readonly*/

5261 of 5600 relevant lines covered (93.95%)

0.94 hits per line

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

80.39
/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_filter, 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
        /**
23
         * Finds the best suggestion.
24
         * @param  string[]  $items
25
         */
26
        public static function getSuggestion(array $items, string $value): ?string
1✔
27
        {
28
                $best = null;
1✔
29
                $min = (strlen($value) / 4 + 1) * 10 + .1;
1✔
30
                foreach (array_unique($items) as $item) {
1✔
31
                        if (($len = levenshtein($item, $value, 10, 11, 10)) > 0 && $len < $min) {
1✔
32
                                $min = $len;
1✔
33
                                $best = $item;
1✔
34
                        }
35
                }
36

37
                return $best;
1✔
38
        }
39

40

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

57

58
        public static function sortBeforeAfter(array $list): array
1✔
59
        {
60
                foreach ($list as $name => $info) {
1✔
61
                        if (!$info instanceof \stdClass || !($info->before ?? $info->after ?? null)) {
1✔
62
                                continue;
1✔
63
                        }
64

65
                        unset($list[$name]);
1✔
66
                        $names = array_keys($list);
1✔
67
                        $best = null;
1✔
68

69
                        foreach ((array) $info->before as $target) {
1✔
70
                                if ($target === '*') {
1✔
71
                                        $best = 0;
1✔
72
                                } elseif (isset($list[$target])) {
×
73
                                        $pos = array_search($target, $names, true);
×
74
                                        $best = min($pos, $best ?? $pos);
×
75
                                }
76
                        }
77

78
                        foreach ((array) ($info->after ?? null) as $target) {
1✔
79
                                if ($target === '*') {
×
80
                                        $best = count($names);
×
81
                                } elseif (isset($list[$target])) {
×
82
                                        $pos = array_search($target, $names, true);
×
83
                                        $best = max($pos + 1, $best);
×
84
                                }
85
                        }
86

87
                        $list = array_slice($list, 0, $best, true)
1✔
88
                                + [$name => $info]
1✔
89
                                + array_slice($list, $best, null, true);
1✔
90
                }
91

92
                return $list;
1✔
93
        }
94

95

96
        public static function removeNulls(array &$items): void
1✔
97
        {
98
                $items = array_filter($items, fn($item) => $item !== null);
1✔
99
        }
1✔
100

101

102
        /**
103
         * Attempts to map the compiled template to the source.
104
         */
105
        public static function mapCompiledToSource(string $compiledFile, ?int $compiledLine = null): ?array
1✔
106
        {
107
                if (!Cache::isCacheFile($compiledFile)) {
1✔
108
                        return null;
×
109
                }
110

111
                $content = file_get_contents($compiledFile);
1✔
112
                $name = preg_match('#^/\*\* source: (\S.+) \*/#m', $content, $m) ? $m[1] : null;
1✔
113
                $compiledLine && preg_match('~/\* line (\d+) \*/~', explode("\n", $content)[$compiledLine - 1], $pos);
1✔
114
                $line = isset($pos[1]) ? (int) $pos[1] : null;
1✔
115
                return $name || $line ? ['name' => $name, 'line' => $line] : null;
1✔
116
        }
117
}
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