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

nette / latte / 16741624166

05 Aug 2025 05:43AM UTC coverage: 93.79% (-0.001%) from 93.791%
16741624166

push

github

dg
removed comment /*readonly*/

5241 of 5588 relevant lines covered (93.79%)

0.94 hits per line

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

81.48
/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
                $this->name = str_contains($name ?? '', "\n") ? null : $name; // caused by StringLoader
1✔
54
        }
1✔
55

56

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

62

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

73

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

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