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

tempestphp / tempest-framework / 14024978163

23 Mar 2025 05:55PM UTC coverage: 79.391% (-0.05%) from 79.441%
14024978163

push

github

web-flow
feat(view): cache Blade and Twig templates in internal storage (#1061)

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

912 existing lines in 110 files now uncovered.

10478 of 13198 relevant lines covered (79.39%)

91.09 hits per line

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

94.44
/src/Tempest/Generation/src/SimplifiesClassNames.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Generation;
6

7
use Nette\PhpGenerator\PhpFile;
8

9
use function Tempest\Support\str;
10

11
/**
12
 * @internal
13
 */
14
trait SimplifiesClassNames
15
{
16
    private bool $simplifyImplements = true;
17

18
    private bool $simplifyClassNamesInBodies = true;
19

20
    private array $aliases = [];
21

22
    public function simplifyClassNamesInMethodBodies(bool $simplify = true): self
2✔
23
    {
24
        $this->simplifyClassNamesInBodies = $simplify;
2✔
25

26
        return $this;
2✔
27
    }
28

29
    public function simplifyImplements(bool $simplify = true): self
4✔
30
    {
31
        $this->simplifyImplements = $simplify;
4✔
32

33
        return $this;
4✔
34
    }
35

UNCOV
36
    public function addUse(string $use): self
×
37
    {
UNCOV
38
        $this->aliases[$use] = null;
×
39

UNCOV
40
        return $this;
×
41
    }
42

43
    public function setAlias(string $class, string $alias): self
1✔
44
    {
45
        if (isset($this->aliases[$class])) {
1✔
UNCOV
46
            unset($this->aliases[$class]);
×
47
        }
48

49
        $this->aliases[$class] = $alias;
1✔
50

51
        return $this;
1✔
52
    }
53

54
    private function simplifyClassNames(PhpFile $file): PhpFile
62✔
55
    {
56
        foreach ($file->getNamespaces() as $namespace) {
62✔
57
            foreach ($namespace->getClasses() as $class) {
62✔
58
                $types = [];
62✔
59
                $functions = [];
62✔
60

61
                if ($this->simplifyImplements) {
62✔
62
                    foreach ($class->getImplements() as $implement) {
60✔
63
                        $types[] = $implement;
38✔
64
                    }
65
                }
66

67
                foreach ($class->getAttributes() as $attribute) {
62✔
68
                    $types[] = $attribute->getName();
12✔
69
                }
70

71
                foreach ($class->getTraits() as $trait) {
62✔
72
                    $types[] = $trait->getName();
19✔
73
                }
74

75
                foreach ($class->getMethods() as $method) {
62✔
76
                    $types[] = $method->getReturnType(true);
52✔
77

78
                    foreach ($method->getParameters() as $parameter) {
52✔
79
                        $types[] = $parameter->getType(true);
26✔
80

81
                        foreach ($parameter->getAttributes() as $attribute) {
26✔
82
                            $types[] = $attribute->getName();
8✔
83
                        }
84
                    }
85

86
                    foreach ($method->getAttributes() as $attribute) {
52✔
87
                        $types[] = $attribute->getName();
12✔
88
                    }
89

90
                    if ($this->simplifyClassNamesInBodies) {
52✔
91
                        $methodBody = $method->getBody();
50✔
92
                        $fqcnMatches = $this->extractFqcnFromBody($methodBody);
50✔
93

94
                        foreach (array_filter($fqcnMatches) as $fqcn) {
50✔
95
                            if (str_contains($methodBody, "/*(f*/\\{$fqcn}")) {
27✔
96
                                $namespace->addUseFunction($fqcn);
6✔
97

98
                                continue;
6✔
99
                            }
100

101
                            if (str_contains($methodBody, "/*(n*/\\{$fqcn}")) {
22✔
102
                                $namespace->addUse($fqcn);
22✔
103

104
                                continue;
22✔
105
                            }
106

107
                            $methodBody = str_replace(
2✔
108
                                search: '\\' . $fqcn,
2✔
109
                                replace: (string) str($fqcn)->afterLast('\\'),
2✔
110
                                subject: $methodBody,
2✔
111
                            );
2✔
112

113
                            $namespace->addUse($fqcn);
2✔
114
                        }
115

116
                        $method->setBody($methodBody);
50✔
117
                    }
118
                }
119

120
                array_map(
62✔
121
                    function ($param) use (&$types): void {
62✔
122
                        $types[] = $param->getType(true);
4✔
123
                    },
62✔
124
                    $class->getProperties(),
62✔
125
                );
62✔
126

127
                foreach ($this->aliases as $class => $alias) {
62✔
128
                    $namespace->addUse($class, $alias);
1✔
129
                }
130

131
                foreach (array_filter($types) as $type) {
62✔
132
                    if (is_string($type)) {
57✔
133
                        $namespace->addUse($type);
52✔
134

135
                        continue;
52✔
136
                    }
137

138
                    foreach ($type->getTypes() as $subtype) {
51✔
139
                        if ($subtype->isClass() && ! $subtype->isClassKeyword()) {
51✔
140
                            $namespace->addUse((string) $subtype);
40✔
141
                        }
142
                    }
143
                }
144
            }
145
        }
146

147
        return $file;
62✔
148
    }
149

150
    private function extractFqcnFromBody(string $body): array
50✔
151
    {
152
        preg_match_all('/(?:\\\\?[A-Za-z_][\w\d_]*\\\\)+[A-Za-z_][\w\d_]*/', $body, $matches);
50✔
153

154
        return array_filter(array_unique(
50✔
155
            array_map(fn (string $fqcn) => rtrim(ltrim($fqcn, '\\'), ':'), $matches[0]),
50✔
156
        ));
50✔
157
    }
158
}
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