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

nette / latte / 16740523592

05 Aug 2025 04:25AM UTC coverage: 93.791%. Remained the same
16740523592

push

github

dg
removed comment /*readonly*/

5242 of 5589 relevant lines covered (93.79%)

0.94 hits per line

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

82.14
/src/Latte/SourceReference.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

13
/**
14
 * Reference to template source code with optional position information.
15
 */
16
class SourceReference
17
{
18
        /**
19
         * Attempts to map the compiled template file to the source name and position.
20
         */
21
        public static function fromCompiled(string $file, ?int $line = null): ?self
1✔
22
        {
23
                if (Cache::isCacheFile($file)
1✔
24
                        && ($content = file_get_contents($file))
1✔
25
                        && preg_match('#^/\*\* source: (\S.+) \*/#m', $content, $source)
1✔
26
                ) {
27
                        $line && preg_match('~/\* line (\d+) \*/~', explode("\n", $content)[$line - 1], $pos);
1✔
28
                        return new self($source[1], isset($pos[1]) ? (int) $pos[1] : null);
1✔
29
                }
30
                return null;
1✔
31
        }
32

33

34
        /** Tries to guess the position in the template from the backtrace */
35
        public static function fromCallStack(): ?self
36
        {
37
                $trace = debug_backtrace();
1✔
38
                foreach ($trace as $item) {
1✔
39
                        if (isset($item['file']) && ($source = self::fromCompiled($item['file'], $item['line']))) {
1✔
40
                                return $source;
1✔
41
                        }
42
                }
43
                return null;
1✔
44
        }
45

46

47
        public function __construct(
1✔
48
                public string $name,
49
                public ?int $line = null,
50
                public ?int $column = null,
51
                public ?string $code = null,
52
        ) {
53
                if (str_contains($name, "\n")) { // caused by StringLoader
1✔
54
                        $this->name = '';
1✔
55
                }
56
        }
1✔
57

58

59
        public function isFile(): bool
60
        {
61
                return @is_file($this->name); // @ - may trigger error
1✔
62
        }
63

64

65
        public function getCode(): ?string
66
        {
67
                if ($this->code) {
×
68
                        return $this->code;
×
69
                } elseif ($this->isFile()) {
×
70
                        file_get_contents($this->name);
×
71
                }
72
                return null;
×
73
        }
74

75

76
        public function __toString(): string
77
        {
78
                $res = '';
1✔
79
                if ($this->isFile()) {
1✔
80
                        $res = "in '" . str_replace(dirname($this->name, 2), '...', $this->name) . "'";
1✔
81
                }
82

83
                if ($this->line) {
1✔
84
                        $res .= ($res ? ' ' : '') . 'on line ' . $this->line . ($this->column ? ' at column ' . $this->column : '');
1✔
85
                }
86
                return $res;
1✔
87
        }
88
}
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