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

nette / latte / 22359082277

24 Feb 2026 04:03PM UTC coverage: 93.959% (+0.05%) from 93.907%
22359082277

push

github

dg
fixed operator ! priority

5273 of 5612 relevant lines covered (93.96%)

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 declare(strict_types=1);
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
namespace Latte;
9

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

13

14
/**
15
 * Latte helpers.
16
 * @internal
17
 */
18
class Helpers
19
{
20
        /**
21
         * Finds the best suggestion.
22
         * @param  string[]  $items
23
         */
24
        public static function getSuggestion(array $items, string $value): ?string
1✔
25
        {
26
                $best = null;
1✔
27
                $min = (strlen($value) / 4 + 1) * 10 + .1;
1✔
28
                foreach (array_unique($items) as $item) {
1✔
29
                        if (($len = levenshtein($item, $value, 10, 11, 10)) > 0 && $len < $min) {
1✔
30
                                $min = $len;
1✔
31
                                $best = $item;
1✔
32
                        }
33
                }
34

35
                return $best;
1✔
36
        }
37

38

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

55

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

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

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

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

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

90
                return $list;
1✔
91
        }
92

93

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

99

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

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